当前位置: 首页 > article >正文

java获取shp文件坐标系

1,pom依赖

<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-referencing</artifactId>
    <version>24.0</version>
</dependency>

2,单元测试

import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.referencing.CRS;
import org.junit.Test;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.Charset;

public class ShpUtils {

    /**
     * 获取Shape文件的坐标系信息。
     *
     * @param shpFilePath shp文件路径
     * @return 坐标系的WKT形式
     */
    public static Integer getCoordinateSystemWKT(String shpFilePath) throws FactoryException {
        ShapefileDataStore dataStore = buildDataStore(shpFilePath);
        try {
            CoordinateReferenceSystem srs = dataStore.getSchema().getCoordinateReferenceSystem();
            System.out.println(srs.toWKT());
            Integer epsg = CRS.lookupEpsgCode(srs, true);
            return epsg;
        } catch (UnsupportedOperationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            dataStore.dispose();
        }
        return 4490;
    }

    /**
     * 构建ShapeDataStore对象。
     *
     * @param shpFilePath shape文件路径。
     * @return
     */
    public static ShapefileDataStore buildDataStore(String shpFilePath) {
        ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
        try {
            ShapefileDataStore dataStore = (ShapefileDataStore) factory
                    .createDataStore(new File(shpFilePath).toURI().toURL());
            if (dataStore != null) {
                dataStore.setCharset(Charset.forName("UTF-8"));
            }
            return dataStore;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询shp文件的坐标系
     *
     */
    @Test
    public void test() throws FactoryException {
        String pathName = "D:\\测试文件\\shp\\xxxxx\\绿地.shp";
        Integer epsg = getCoordinateSystemWKT(pathName);
        System.out.println("EPSG:" + epsg);
    }
}

3,执行结果

GEOGCS["China Geodetic Coordinate System 2000", 
  DATUM["D_China_2000", 
    SPHEROID["CGCS2000", 6378137.0, 298.257222101]], 
  PRIMEM["Greenwich", 0.0], 
  UNIT["degree", 0.017453292519943295], 
  AXIS["Longitude", EAST], 
  AXIS["Latitude", NORTH]]
EPSG:4490

http://www.kler.cn/news/137006.html

相关文章:

  • 2.1 > Shell 是什么、如何更熟练的使用 Bash Shell
  • github文件上传与更新
  • 落实“双碳”行动,深兰科技推动分子能源技术在AI硬件产品领域的应用及产业化进程
  • stm32 nor flash fats 文件系统加载失败问题
  • 干货|基于React和xlsx库导出表格数据到excel(前端操作)
  • Linux系列-Linux的常见指令(三)
  • 计算机类编程课学生编写的代码应该如何管理
  • 一个完备的手游地形实现方案
  • “三面一体”的业务调度方案在运营商订单运营的实践
  • 一个美观且功能丰富的 .NET 控制台应用程序开源库
  • 报错!Jupyter notebook 500 : Internal Server Error
  • hologres 索引与查询优化
  • MFC项目添加CUDA支持
  • leetcode:914. 卡牌分组(python3解法)
  • 如何快速让苹果TF上架
  • 安卓毕业设计:基于安卓android微信小程序的超市购物系统
  • CSDN每日一题学习训练——Python版(N皇后 II、买卖股票的最佳时机 II、编程通过键盘输入每一位运动员)
  • uniapp和vue3+ts创建自定义下拉选择框组件
  • Mysql之多表查询下篇
  • CSS中2种复合选择器
  • 【Linux网络】从原理到实操,感受PXE无人值守自动化高效批量网络安装系统
  • OpenAI与微软合作,构建 ChatGPT 5 模型;10天准确天气预报
  • 配置中心
  • 如何在Linux以docker-compose方式快速部署运行StackEdit,并实现公网访问
  • 了解JVM的相关概述,发现很多全忘了(文末福利)
  • 【腾讯云云上实验室-向量数据库】TAI时代的数据枢纽-向量数据库 VectorDB