Как создать и заполнить shp-файл с помощью GeoTools API - PullRequest
0 голосов
/ 14 мая 2018

Мне нужно создать пустой файл формы и заполнить его данными из моей коллекции Java.Может кто-нибудь показать мне пример того, как этого добиться?Заранее спасибо.

1 Ответ

0 голосов
/ 15 мая 2018

Для получения полной информации см. Учебник от CSV до Shapefile .

По сути, вам нужно определить столбцы Shapefiles в объекте SimpleFeatureType, самый простой способ сделать это - использовать SimpleFeatureTypeBuilder.Здесь генерируется непосредственно с помощью служебного метода для экономии времени.

    final SimpleFeatureType TYPE = 
        DataUtilities.createType("Location",
            "location:Point:srid=4326," + // <- the geometry attribute: Point type
                    "name:String," + // <- a String attribute
                    "number:Integer" // a number attribute
    );

Теперь вы можете создать Shapefile:

    /*
     * Get an output file name and create the new shapefile
     */
    File newFile = getNewShapeFile(file);

    ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();

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

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

    /*
     * You can comment out this line if you are using the createFeatureType method (at end of
     * class file) rather than DataUtilities.createType
     */
    newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);

И, наконец, записать коллекцию Features вэто:

    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);
            transaction.commit();

        } catch (Exception problem) {
            problem.printStackTrace();
            transaction.rollback();

        } finally {
            transaction.close();
        }
        System.exit(0); // success!
    } else {
        System.out.println(typeName + " does not support read/write access");
        System.exit(1);
    }
...