shp影像数据转坐标系

import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.WKTReader;
import org.opengis.feature.GeometryAttribute;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;
readShpToES("C:\\Users\\EDZ\\Desktop\\库管系统\\POI\\宾馆.shp", "EPSG:3857");
public void readShpToES(String filePath, String transformType) throws Exception {

		ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
		WKTReader reader = new WKTReader();
		File file = new File(filePath);
		ShapefileDataStore sds = (ShapefileDataStore) dataStoreFactory.createDataStore(file.toURI().toURL());
		sds.setCharset(Charset.forName("GBK"));
		SimpleFeatureSource featureSource = sds.getFeatureSource();
		SimpleFeatureIterator iterator = featureSource.getFeatures().features();

		Property per = null;// org.opengis.feature;
		StringBuffer transformBuffer = new StringBuffer();
		while (iterator.hasNext()) {
			SimpleFeature feature = iterator.next();
			// geometry属性
			GeometryAttribute geometryAttribute = feature.getDefaultGeometryProperty();
			// 获取坐标参考系信息
			CoordinateReferenceSystem crsSource = geometryAttribute.getDescriptor().getCoordinateReferenceSystem();
			Iterator<Property> it = feature.getProperties().iterator();
			Map<String, String> properties = new HashMap<>();
			while (it.hasNext()) {
				per = it.next();
				String key = per.getName().toString();
				properties.put(key, per.getValue().toString());
			}
			// System.out.println(properties.get("the_geom"));
			org.locationtech.jts.geom.Geometry geom = reader.read(properties.get("the_geom"));
			Geometry transform = transform(geom, crsSource, transformType);
			// System.out.println(geom.toText());
			// System.out.println(transform.toText());
			transformBuffer.append(transform.toText());
		}
		// 文件写入指定路径
		// 创建字符输出流对象,负责向文件内写入
//		FileWriter fw = new FileWriter(path + chunk.getFilename() + chunk.getTransform() + ".txt");
		FileWriter fw = null;
		try {
			fw = new FileWriter(uploadFolder + "/" + chunk.getIdentifier() + "3857.txt", true);
			// 将str里面的内容读取到fw所指定的文件中
			fw.write(transformBuffer.toString());
			System.out.println("###########" + transformBuffer.toString());
			System.out.println("*************:" + uploadFolder + "/" + chunk.getIdentifier() + "3857.txt");
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			fw.flush();
			fw.close();
		}
	}
public Geometry transform(Geometry g, CoordinateReferenceSystem source, String transformString) {
		try {
			//CoordinateReferenceSystem crsTarget = CRS.decode("EPSG:3857");
			CoordinateReferenceSystem crsTarget = CRS.decode(transformString);
			MathTransform transform = CRS.findMathTransform(source, crsTarget);
			return JTS.transform(g, transform);
		} catch (NoSuchAuthorityCodeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FactoryException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

shp文件demo(C:\Users\EDZ\Desktop\库管系统\POI\宾馆.shp"):POI.zip
如果报某类型比如EPSG:3857等没找到,需要引入:

<!-- https://mvnrepository.com/artifact/org.geotools/gt-epsg-hsql -->
		<dependency>
		    <groupId>org.geotools</groupId>
		    <artifactId>gt-epsg-hsql</artifactId>
		    <version>24-SNAPSHOT</version>
		</dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值