一、環境准備
裝配GeoTools有兩種方式,一種是配置maven工程的pom文件(配置方式參考官網),另一種是下載geotools的jar包到本地導入依賴。
1. 下載jar的方式,下載路徑:https://sourceforge.net/projects/geotools/files/
2. maven
<dependency> <groupId>org.geotools</groupId> <artifactId>gt-epsg-extension</artifactId> <version>${geotools.version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-referencing</artifactId> <version>${geotools.version}</version> </dependency>
二、實現功能
從shape文件中讀取坐標系信息。shape文件並不是一個文件而是一堆文件,坐標系信息存儲在*.prj文件中。這是一個文本文件,可以用記事本打開。文件中存儲的是WKT格式的坐標系信息。
PROJCS["WGS 84 / Pseudo-Mercator", GEOGCS["WGS 84", DATUM["World Geodetic System 1984", SPHEROID["WGS 84", 6378137.0, 298.257223563, AUTHORITY["EPSG","7030"]], AUTHORITY["EPSG","6326"]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Geodetic longitude", EAST], AXIS["Geodetic latitude", NORTH], AUTHORITY["EPSG","4326"]], PROJECTION["Popular Visualisation Pseudo Mercator"], PARAMETER["semi_minor", 6378137.0], PARAMETER["latitude_of_origin", 0.0], PARAMETER["central_meridian", 0.0], PARAMETER["scale_factor", 1.0], PARAMETER["false_easting", 0.0], PARAMETER["false_northing", 0.0], UNIT["m", 1.0], AXIS["Easting", EAST], AXIS["Northing", NORTH], AUTHORITY["EPSG","3857"]]
三、樣例代碼
import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.nio.charset.Charset; import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.data.shapefile.ShapefileDataStoreFactory; import org.geotools.referencing.CRS; import org.opengis.referencing.crs.CoordinateReferenceSystem; public class ShapeUtils { /** * 獲取Shape文件的坐標系信息。 * * @param shpFilePath shp文件路徑 * @return 坐標系的code ,例如3857,4326等等 */ public static Integer getEpsgCode(String shpFilePath) { ShapefileDataStore dataStore = buildDataStore(shpFilePath); Integer code = null; try { String wkt = dataStore.getSchema().getCoordinateReferenceSystem().toWKT(); CoordinateReferenceSystem crsTarget = CRS.parseWKT(wkt); code = CRS.lookupEpsgCode(crsTarget,true); } catch (UnsupportedOperationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { dataStore.dispose(); } return code; } /** * 構建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; } }
測試
import java.io.File; import java.util.List; import com.elon.model.ShapeFieldInfo; import com.elon.model.gismodel.GisMultiPolygon; import com.elon.shape.ShapeUtils; public class StartupGeoTools { public static void main(String[] args) { String workSpacePath = System.getProperty("user.dir"); String shpFilePath = workSpacePath + File.separator + "shape/ne_50m_admin_0_countries/ne_50m_admin_0_countries.shp"; Integer code = ShapeUtils.getEpsgCode(shpFilePath); System.out.println("code:" + code); } }
參考網址
http://support.supermap.com.cn/DataWarehouse/WebDocHelp/iPortal/Appendix/CoordSystem/EPSGCode.htm