我正在尝试编写一个形状文件:
File shapefile = new File(....);
Map<String, Serializable> map = new HashMap();
map.put("url", shapefile.toURI().toURL());
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName("MultiPolygonFeatureType");
CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84
builder.setCRS(crs);
builder.add("location", MultiPolygon.class);
builder.add("name", String.class);
SimpleFeatureType featureType = builder.buildFeatureType();
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
ShapefileDataStore dataStore = (ShapefileDataStore) factory.createNewDataStore(map);
dataStore.createSchema(featureType);
SimpleFeatureWriter writer = (SimpleFeatureWriter)
dataStore.getFeatureWriterAppend(dataStore.getTypeNames()[0], new DefaultTransaction());
但这给了我错误:
java.lang.ClassCastException:类 org.geotools.data.InProcessLockingManager$1 无法转换为类 org.geotools.data.simple.SimpleFeatureWriter (org.geotools.data.InProcessLockingManager$1 和 org.geotools.data.simple.SimpleFeatureWriter 位于未命名模块中 加载器“应用程序”)
我在这里缺少什么部分?
基本上,正如错误所示,您无法转换为
SimpleFeatureWriter
。所以
你应该这样做:
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = dataStore.getFeatureWriterAppend(dataStore.getTypeNames()[0], new DefaultTransaction());
正如我在 shapefile 的评论中所说,你必须调用几何属性
the_geom
:
builder.add("the_geom", MultiPolygon.class);
通过这 2 个更改,以下代码确实可以工作:
File shapefile = new File("/tmp/ian.shp");
HashMap<String, Serializable> map = new HashMap();
map.put("url", URLs.fileToUrl(shapefile));
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName("MultiPolygonFeatureType");
CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
builder.setCRS(crs);
builder.add("the_geom", MultiPolygon.class);
builder.add("name", String.class);
SimpleFeatureType featureType = builder.buildFeatureType();
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
ShapefileDataStore dataStore = (ShapefileDataStore) factory.createNewDataStore(map);
dataStore.createSchema(featureType);
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = dataStore.getFeatureWriterAppend(dataStore.getTypeNames()[0], new DefaultTransaction());