Java GeoJSON可以轉換為其他格式,如Shapefile、KML、GeoRSS等。以下是一種將GeoJSON轉換為Shapefile的方法:
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>22.3</version>
</dependency>
import org.geotools.feature.FeatureCollection;
import org.geotools.geojson.feature.FeatureJSON;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class GeoJSONToShapefileConverter {
public static void main(String[] args) throws IOException {
File geoJSONFile = new File("path/to/geojson/file.geojson");
FileInputStream fis = new FileInputStream(geoJSONFile);
FeatureJSON featureJSON = new FeatureJSON();
FeatureCollection featureCollection = featureJSON.readFeatureCollection(fis);
// Convert FeatureCollection to Shapefile
// TODO: add code to convert FeatureCollection to Shapefile
}
}
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.geojson.feature.FeatureJSON;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class GeoJSONToShapefileConverter {
public static void main(String[] args) throws IOException {
File geoJSONFile = new File("path/to/geojson/file.geojson");
FileInputStream fis = new FileInputStream(geoJSONFile);
FeatureJSON featureJSON = new FeatureJSON();
FeatureCollection featureCollection = featureJSON.readFeatureCollection(fis);
File shapefile = new File("path/to/output/shapefile.shp");
Map<String, Object> params = new HashMap<>();
params.put(ShapefileDataStoreFactory.URLP.key, shapefile.toURI().toURL());
ShapefileDataStore dataStore = (ShapefileDataStore) DataStoreFinder.getDataStore(params);
SimpleFeatureType featureType = featureCollection.getSchema();
dataStore.createSchema(featureType);
FeatureIterator<SimpleFeature> iterator = featureCollection.features();
while (iterator.hasNext()) {
SimpleFeature feature = iterator.next();
dataStore.addFeature(feature);
}
iterator.close();
}
}
通過以上步驟,您可以將Java GeoJSON轉換為Shapefile格式。您可以根據需要進行調整和修改以適應不同的轉換需求。