GeoTools:将自定义多边形插入现有的.shp文件中。

huangapple go评论64阅读模式
英文:

GeoTools: insert custom Polygons into existiong .shp file

问题

我是新手使用Geotools。现在我想在奥地利的一个Shapefile中插入一个自定义区域(多边形)。
我的代码:

public static void main(String[] args) throws IOException {
    File file = new File("src/main/java/org/geotools/austria.shp");
    Map<String, Object> map = new HashMap<>();
    map.put("url", file.toURI().toURL());

    DataStore dataStore = DataStoreFinder.getDataStore(map);
    String typeName = dataStore.getTypeNames()[0];
    FeatureSource<SimpleFeatureType, SimpleFeature> source =
            dataStore.getFeatureSource(typeName);

    MapContent showmap = new MapContent();
    showmap.setTitle("Austria");

    Style style = SLD.createSimpleStyle(source.getSchema());
    Layer layer = new FeatureLayer(source, style);
    showmap.addLayer(layer);

    // 显示地图
    JMapFrame.showMap(showmap);
}

我的当前结果:

GeoTools:将自定义多边形插入现有的.shp文件中。

这个图像显示了我的当前输出。我绘制了一个红色的六边形,以展示我未来想要的内容。如何将此多边形插入并显示在Shapefile中?

英文:

I'm new to Geotools. Now I want to insert a custom area (Polygon) in a Shapefile of Austria.
My code:

 public static void main(String[] args) throws IOException {
        File file = new File(&quot;src/main/java/org/geotools/austria.shp&quot;);
        Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();
        map.put(&quot;url&quot;, file.toURI().toURL());

        DataStore dataStore = DataStoreFinder.getDataStore(map);
        String typeName = dataStore.getTypeNames()[0];
        FeatureSource&lt;SimpleFeatureType, SimpleFeature&gt; source =
                dataStore.getFeatureSource(typeName);

        MapContent showmap = new MapContent();
        showmap.setTitle(&quot;Austria&quot;);

        Style style = SLD.createSimpleStyle(source.getSchema());
        Layer layer = new FeatureLayer(source, style);
        showmap.addLayer(layer);

        // display the map
        JMapFrame.showMap(showmap);
    }

My current result:

GeoTools:将自定义多边形插入现有的.shp文件中。

This image shows my current output. I drew a red hexagon to show what I want to have in future.
How can I insert and display this Polygon into a Shapefile?

答案1

得分: 0

首先,您需要创建一个新的Shapefile(您可以覆盖旧的文件,但这样容易丢失数据)。

SimpleFeatureType TYPE = dataStore.getSchema(typeName);
File newFile = new File("output.shp");
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();

Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put("url", URLs.fileToURL(newFile));
params.put("create spatial index", Boolean.TRUE);

ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
newDataStore.createSchema(TYPE);

然后,您需要将现有的多边形复制到新文件(假设它们在名为collectionSimpleFeatureCollection中),然后是新的要素:

Transaction transaction = new DefaultTransaction("create");

String typeName = newDataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);

if (featureSource instanceof SimpleFeatureStore) {
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;

    featureStore.setTransaction(transaction);
    try {
        featureStore.addFeatures(collection);
        
        // 现在添加六边形
        featureStore.addFeatures(DataUtilities.collection(hexagon));
        transaction.commit();
     } catch (Exception problem) {
        problem.printStackTrace();
        transaction.rollback();
        System.exit(-1);
     } finally {
        transaction.close();
     }
} else {
    System.out.println(typeName + " does not support read/write access");
    System.exit(1);
}
英文:

First you need to create a new Shapefile (you could overwrite the old one but it is easy to lose your data that way).

SimpleFeatureType TYPE = dataStore.getSchema(typeName);
File newFile = new File(&quot;output.shp&quot;);
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();

Map&lt;String, Serializable&gt; params = new HashMap&lt;String, Serializable&gt;();
params.put(&quot;url&quot;, URLs.fileToURL(newFile));
params.put(&quot;create spatial index&quot;, Boolean.TRUE);

ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
newDataStore.createSchema(TYPE);

Then you need to copy the existing polygons to the new file (I'm assuming they are in a SimpleFeatureCollection called collection) followed by the new feature(s):

Transaction transaction = new DefaultTransaction(&quot;create&quot;);

String typeName = newDataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);

if (featureSource instanceof SimpleFeatureStore) {
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;

    featureStore.setTransaction(transaction);
    try {
        featureStore.addFeatures(collection);
        
        // Now add the hexagon
        featureStore.addFeatures(DataUtilities.collection(hexagon));
        transaction.commit();
     } catch (Exception problem) {
        problem.printStackTrace();
        transaction.rollback();
        System.exit(-1);
     } finally {
        transaction.close();
     }
} else {
    System.out.println(typeName + &quot; does not support read/write access&quot;);
    System.exit(1);
}

huangapple
  • 本文由 发表于 2020年8月5日 16:36:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63261359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定