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 {
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 ;
}
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 ;
}
@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