在使用geotools 生成shape 文件的过程。示例代码如下:
public static void createShpFileByannotation(String filePath, String type, Integer srid, LinkedHashMap<String, ShapeMeta> fieldAnnotation){
File file = new File(filePath);
Map<String, Serializable> params = new HashMap<>();
ShapefileDataStore ds = null;
try{
params.put("url",file.toURI().toURL());
params.put("create spatial index",Boolean.TRUE);
ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
//设置投影坐标系
tb.setCRS(CRS.decode("EPSG:"+srid));
tb.setName("shapefile");
switch (type) {
case POINT:
tb.add("the_geom", Point.class);
break;
case LINESTRING:
tb.add("the_geom", LineString.class);
break;
case POLYGON:
tb.add("the_geom", Polygon.class);
break;
case MULTY_POLYGON:
tb.add("the_geom", MultiPolygon.class);
default:
tb.add("the_geom", Polygon.class);
break;
}
for(Map.Entry<String,ShapeMeta> item:fieldAnnotation.entrySet()){
ShapeMeta shapeMeta = item.getValue();
if(shapeMeta!=null){
switch (shapeMeta.type()){
case "Long":
tb.length(shapeMeta.length()).add(shapeMeta.fieldName(),Long.class);
break;
case "Float":
tb.add(shapeMeta.fieldName(),Float.class);
break;
case "String":
tb.length(shapeMeta.length()).add(shapeMeta.fieldName(),String.class);
break;
case "Date":
tb.length(shapeMeta.length()).add(shapeMeta.fieldName(),Date.class);
break;
case "Double":
tb.length(shapeMeta.length()).add(shapeMeta.fieldName(),Double.class);
break;
case "Geom":
tb.add(shapeMeta.fieldName().toUpperCase(),MultiPolygon.class);
break;
default:
tb.length(shapeMeta.length()).add(shapeMeta.fieldName(),String.class);
break;
}
}
}
ds.createSchema(tb.buildFeatureType());
ds.setCharset(Charset.forName("GBK"));
}catch(IOException e){
e.printStackTrace();
} catch (NoSuchAuthorityCodeException e) {
e.printStackTrace();
} catch (FactoryException e) {
e.printStackTrace();
}
生成的prj 文件 是墨卡托系的。在国家2000 坐标系下分带都是高斯克吕。在esri 的wkt 描述符中,只是 平台标准不一。如果硬性需要处理的话,通过重写prj wkt 描述符即可
EPSG 的描述查询地址为:https://epsg.io/
定义想查询的EPSG WKT 描述文本:
以2000 4527 为例:
PROJCS["CGCS2000 / 3-degree Gauss-Kruger zone 39",GEOGCS["China Geodetic Coordinate System 2000",DATUM["China_2000",SPHEROID["CGCS2000",6378137,298.257222101,AUTHORITY["EPSG","1024"]],AUTHORITY["EPSG","1043"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4490"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",117],PARAMETER["scale_factor",1],PARAMETER["false_easting",39500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","4527"]]
文本覆盖写入prj 文件即可。
通过把shp 导入arcmap 查看。基本属性坐标系正常即可