geotools文件转换

8 篇文章 1 订阅

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();
	}
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值