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格式。您可以根据需要进行调整和修改以适应不同的转换需求。