坐标系Geometry CRS

依赖

<dependencies>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</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>
  <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>

验证是否存在无效数据(如边界超出范围)

// Rather than use an iterator, create a FeatureVisitor to check each fature
    static class ValidationVisitor implements FeatureVisitor {
        public int numInvalidGeometries = 0;
        @Override
        public void visit(Feature f) {
            SimpleFeature feature = (SimpleFeature) f;
            Geometry geom = (Geometry) feature.getDefaultGeometry();
            // 这里是核心,直接调用方法判断geom是否有效
            if (geom != null && !geom.isValid()) {
                numInvalidGeometries++;
                System.out.println("Invalid Geoemtry: " + feature.getID());
            }
        }
    }
public static void main(String[] args) throws IOException {
		// 读取数据
        File file = new File("E:\\DataofGeotools\\data.shp");
        FileDataStore store = FileDataStoreFinder.getDataStore(file);
        FeatureSource featureSource = store.getFeatureSource();
        SimpleFeatureCollection simpleFeatureCollection = (SimpleFeatureCollection) featureSource.getFeatures();
        ValidationVisitor visitor = new ValidationVisitor();
        // 验证
        // Visit the contents of a feature collection.
        simpleFeatureCollection.accepts(visitor,null);
        System.out.println(visitor.numInvalidGeometries);
    }

坐标转换

1、过程:
读取data.shp,进行投影转换之后写出

public class Reproject {
    public static void main(String[] args) throws IOException, FactoryException {
        File file = new File("E:\\DataofGeotools\\data.shp");
        FileDataStore store = FileDataStoreFinder.getDataStore(file);
        FeatureSource featureSource = store.getFeatureSource();
        SimpleFeatureType type = (SimpleFeatureType) featureSource.getSchema();
        // 源坐标
        CoordinateReferenceSystem sourceCRS = type.getCoordinateReferenceSystem();
        // 目标坐标
        CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4214");
        // allow for some error due to different datums
        boolean lenient = true;
        MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, lenient);
        // 重新写文件
        // 获取到要素集合
        SimpleFeatureCollection featureCollection = (SimpleFeatureCollection) featureSource.getFeatures();
        ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
        Map<String, Serializable> create = new HashMap<>();
        File  newShpFile = new File("E:\\DataofGeotools\\dataproject11.shp");
        create.put("url", newShpFile.toURI().toURL());
        create.put("create spatial index", Boolean.TRUE);
        DataStore dataStore = dataStoreFactory.createNewDataStore(create);
        SimpleFeatureType featureType = SimpleFeatureTypeBuilder.retype(type, targetCRS);
        dataStore.createSchema(featureType);
        // Get the name of the new Shapefile, which will be used to open the FeatureWriter
        String createdName = dataStore.getTypeNames()[0];
        Transaction transaction = new DefaultTransaction("Reproject");
        try (FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
                     dataStore.getFeatureWriterAppend(createdName, transaction);
             SimpleFeatureIterator iterator = featureCollection.features()) {
            while (iterator.hasNext()) {
                // copy the contents of each feature and transform the geometry
                SimpleFeature feature = iterator.next();
                SimpleFeature copy = writer.next();
                copy.setAttributes(feature.getAttributes());
                Geometry geometry = (Geometry) feature.getDefaultGeometry();
                Geometry geometry2 = JTS.transform(geometry, transform);
                copy.setDefaultGeometry(geometry2);
                writer.write();

            }
            // 事务提交以前一定要记得关闭资源
            writer.close();
            transaction.commit();
        } catch (Exception problem) {
            problem.printStackTrace();
            transaction.rollback();
        } finally {
            transaction.close();

        }
    }
}
// 可以筛选你想要的要素进行投影转换输出
Query query = new Query(typeName);
query.setCoordinateSystemReproject(map.getCoordinateReferenceSystem());
SimpleFeatureCollection featureCollection = featureSource.getFeatures(query);

2、常用坐标:
EPSG:4326
EPSG Projection 4326 - WGS 84

EPSG: 3785
Popular Visualization CRS / Mercator

EPSG:3005
NAD83 / BC Albers
坐标查询地址:https://epsg.io/
在这里插入图片描述

常见坐标获取及创建

1、通过simplefetatureType获取

SimpleFeatureType type = (SimpleFeatureType) featureSource.getSchema();
CoordinateReferenceSystem sourceCRS = type.getCoordinateReferenceSystem();

2、通过CRS

 CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4214");

3、通过CRS Factory

CRSAuthorityFactory   factory = CRS.getAuthorityFactory(true);
CoordinateReferenceSystem crs = factory.createCoordinateReferenceSystem("EPSG:4326");

4、DefaultGeographicCRS

 CoordinateReferenceSystem sourceCRS = DefaultGeographicCRS.WGS84;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
温州2000坐标系转换为WGS84坐标系可以使用以下方法: 1. 使用Proj4库进行转换: ```java import org.osgeo.proj4j.CoordinateReferenceSystem; import org.osgeo.proj4j.CoordinateTransform; import org.osgeo.proj4j.CRSFactory; import org.osgeo.proj4j.ProjCoordinate; public class CoordinateConversion { public static void main(String[] args) { // 定义温州2000坐标系的EPSG代码 String wenzhou2000EPSG = "EPSG:4490"; // 定义WGS84坐标系的EPSG代码 String wgs84EPSG = "EPSG:4326"; // 创建坐标系工厂 CRSFactory crsFactory = new CRSFactory(); // 根据EPSG代码获取坐标系对象 CoordinateReferenceSystem wenzhou2000CRS = crsFactory.createFromName(wenzhou2000EPSG); CoordinateReferenceSystem wgs84CRS = crsFactory.createFromName(wgs84EPSG); // 创建坐标转换对象 CoordinateTransform transform = new CoordinateTransform(wenzhou2000CRS, wgs84CRS); // 定义温州2000坐标系中的坐标点 ProjCoordinate wenzhou2000Point = new ProjCoordinate(120.7059, 27.9944); // 创建用于存储转换结果的坐标点对象 ProjCoordinate wgs84Point = new ProjCoordinate(); // 进行坐标转换 transform.transform(wenzhou2000Point, wgs84Point); // 输出转换后的WGS84坐标系中的坐标点 System.out.println("WGS84坐标系中的坐标点:"); System.out.println("经度:" + wgs84Point.x); System.out.println("纬度:" + wgs84Point.y); } } ``` 2. 使用GeoTools库进行转换: ```java import org.geotools.geometry.jts.JTSFactoryFinder; import org.geotools.referencing.CRS; import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.opengis.referencing.operation.MathTransform; import org.opengis.referencing.operation.TransformException; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; public class CoordinateConversion { public static void main(String[] args) throws Exception { // 定义温州2000坐标系的EPSG代码 String wenzhou2000EPSG = "EPSG:4490"; // 定义WGS84坐标系的EPSG代码 String wgs84EPSG = "EPSG:4326"; // 根据EPSG代码获取坐标系对象 CoordinateReferenceSystem wenzhou2000CRS = CRS.decode(wenzhou2000EPSG); CoordinateReferenceSystem wgs84CRS = CRS.decode(wgs84EPSG); // 创建坐标转换对象 MathTransform transform = CRS.findMathTransform(wenzhou2000CRS, wgs84CRS); // 定义温州2000坐标系中的坐标点 Coordinate wenzhou2000Point = new Coordinate(120.7059, 27.9944); // 创建几何对象工厂 GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); // 创建温州2000坐标系中的点几何对象 Geometry wenzhou2000Geometry = geometryFactory.createPoint(wenzhou2000Point); // 进行坐标转换 Geometry wgs84Geometry = JTS.transform(wenzhou2000Geometry, transform); // 获取转换后的WGS84坐标系中的坐标点 Coordinate wgs84Point = wgs84Geometry.getCoordinate(); // 输出转换后的WGS84坐标系中的坐标点 System.out.println("WGS84坐标系中的坐标点:"); System.out.println("经度:" + wgs84Point.x); System.out.println("纬度:" + wgs84Point.y); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值