Мне нужно создать пустой файл формы и заполнить его данными из моей коллекции Java. Может ли кто-нибудь показать мне пример того, как этого добиться? Заранее спасибо.
Как создать и заполнить файл shp с помощью GeoTools API
Ответы (1)
Полную информацию см. в руководстве по преобразованию CSV в шейп-файл.
По сути, вам нужно определить столбцы шейп-файлов в объекте 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
);
Теперь вы можете создать шейп-файл:
/*
* 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);
}
person
Ian Turton
schedule
15.05.2018