本文主要是介绍根据 Shp 文件 输出 GeoJson 文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 根据 Shp 文件 输出 GeoJson 文件
根据 Shp 文件 输出 GeoJson 文件
- pom
<repositories><repository><id>osgeo</id><name>OSGeo Release Repository</name><url>https://repo.osgeo.org/repository/release/</url><snapshots><enabled>false</enabled></snapshots><releases><enabled>true</enabled></releases></repository><repository><id>osgeo-snapshot</id><name>OSGeo Snapshot Repository</name><url>https://repo.osgeo.org/repository/snapshot/</url><snapshots><enabled>true</enabled></snapshots><releases><enabled>false</enabled></releases></repository></repositories><properties><java.version>1.8</java.version><geotools.version>21.4</geotools.version></properties><dependencies><!--GeoTools--><dependency><groupId>org.geotools</groupId><artifactId>gt-shapefile</artifactId><version>${geotools.version}</version></dependency><dependency><groupId>org.geotools</groupId><artifactId>gt-geojson</artifactId><version>${geotools.version}</version></dependency></dependencies>
- 根据 Shp 文件 输出 GeoJson 文件
public void generateGeoJsonFileFromShp() {File shpFile = new File("D:\\data\\test.shp");File geoJsonFile = new File("D:\\data\\test.geojson");String charset = "GB2312";try (BufferedWriter writer = new BufferedWriter(new FileWriter(geoJsonFile))) {ShapefileDataStore shapefileDataStore = new ShapefileDataStore(shpFile.toURI().toURL());shapefileDataStore.setCharset(Charset.forName(charset));ContentFeatureSource featureSource = shapefileDataStore.getFeatureSource();ContentFeatureCollection features = featureSource.getFeatures();FeatureJSON featureJSON = new FeatureJSON();featureJSON.writeFeatureCollection(features, writer);} catch (Exception e) {e.printStackTrace();}}
Mr.superbeyone
这篇关于根据 Shp 文件 输出 GeoJson 文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!