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

你可能感兴趣的:(GIS,java,gis,shp,坐标系)