1、pom.xml中添加依赖
<properties>
<java.version>1.8</java.version>
<geotools.version>26-SNAPSHOT</geotools.version>
</properties>
<dependencies>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools.jdbc</groupId>
<artifactId>gt-jdbc-postgis</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-swing</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>${geotools.version}</version>
</dependency>
</dependencies>
2、geojson转shp
public class Json2Shp {
public static void main(String[] args) throws Exception {
String jsonPath = "C:\\Users\\Administrator\\Desktop\\shp\\beijing.geojson";
String shpPath = "C:\\Users\\Administrator\\Desktop\\shp\\1.shp";
geoJson2Shp(jsonPath, shpPath);
}
public static void geoJson2Shp(String geojsonPath, String shpPath) throws Exception {
FileInputStream in = new FileInputStream(geojsonPath);
GeometryJSON gjson = new GeometryJSON();
FeatureJSON fjson = new FeatureJSON(gjson);
SimpleFeatureCollection featureCollection = (SimpleFeatureCollection) fjson.readFeatureCollection(in);
SimpleFeatureType schemaSource = featureCollection.getSchema();
List<AttributeDescriptor> attributeSource = featureCollection.getSchema().getAttributeDescriptors();
List<AttributeDescriptor> attributeTarget = new ArrayList<>();
GeometryType geomType = null;
for (AttributeDescriptor attrib : attributeSource) {
AttributeType type = attrib.getType();
if (type instanceof GeometryType) {
geomType = (GeometryType) type;
} else {
attributeTarget.add(attrib);
}
}
SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
tb.setCRS(DefaultGeographicCRS.WGS84);
tb.setName("shapefile");
tb.add("the_geom", geomType.getBinding());
tb.addAll(attributeTarget);
SimpleFeatureType outSchema = tb.buildFeatureType();
List<SimpleFeature> outFeatures = new ArrayList<>();
SimpleFeatureIterator featureIterator = featureCollection.features();
while (featureIterator.hasNext()) {
SimpleFeature featureSource = featureIterator.next();
SimpleFeature featureTarget = DataUtilities.reType(outSchema, featureSource, true);
featureTarget.setAttribute(outSchema.getGeometryDescriptor().getName(),
featureSource.getAttribute(schemaSource.getGeometryDescriptor().getName()));
outFeatures.add(featureTarget);
}
saveFeaturesToShp(outFeatures, outSchema, shpPath);
}
public static void saveFeaturesToShp(List<SimpleFeature> outFeatures, SimpleFeatureType outSchema, String shpPath)
throws Exception {
File shpFile = new File(shpPath);
Map<String, Serializable> params = new HashMap<>();
params.put(ShapefileDataStoreFactory.URLP.key, shpFile.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
ShapefileDataStore newDataStore = (ShapefileDataStore) new ShapefileDataStoreFactory()
.createNewDataStore(params);
newDataStore.setCharset(Charset.forName("GBK"));
newDataStore.createSchema(outSchema);
Transaction transaction = Transaction.AUTO_COMMIT;// new DefaultTransaction("create");
String typeName = newDataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
SimpleFeatureCollection collection = new ListFeatureCollection(outSchema, outFeatures);
featureStore.setTransaction(transaction);
featureStore.addFeatures(collection);
transaction.commit();
transaction.close();
System.out.println("完成");
}
}
3、shp转geojson
public class Shp2Json {
public static void main(String[] args) throws Exception {
String shpPath = "C:\\Users\\Administrator\\Desktop\\shp\\beijing.shp";
String geojsonPath = "C:\\Users\\Administrator\\Desktop\\shp\\beijing.geojson";
shp2geojson(shpPath, geojsonPath);
}
public static void shp2geojson(String shpPath, String geojsonPath) throws Exception {
File file = new File(shpPath);
FileDataStore shpDataStore = FileDataStoreFinder.getDataStore(file);
// ShapefileDataStore shpDataStore = new ShapefileDataStore(file.toURI().toURL());
// shpDataStore.setCharset(Charset.forName("GBK"));
SimpleFeatureSource featureSource = shpDataStore.getFeatureSource();
SimpleFeatureCollection featureCollection = featureSource.getFeatures();
String stringGeoJSON = new FeatureJSON().toString(featureCollection);
bufferedWriter(geojsonPath, stringGeoJSON);
}
public static void bufferedWriter(String filePath, String content) throws Exception {
FileWriter fileWriter = new FileWriter(filePath);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(content);
bufferedWriter.close();
fileWriter.close();
}
}