本文整理汇总了Java中org.geotools.data.shapefile.ShapefileDataStoreFactory类的典型用法代码示例。如果您正苦于以下问题:Java ShapefileDataStoreFactory类的具体用法?Java ShapefileDataStoreFactory怎么用?Java ShapefileDataStoreFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ShapefileDataStoreFactory类属于org.geotools.data.shapefile包,在下文中一共展示了ShapefileDataStoreFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: exportFeaturesToShapeFile
import org.geotools.data.shapefile.ShapefileDataStoreFactory; //导入依赖的package包/类
public static void exportFeaturesToShapeFile(File fileOutput,FeatureCollection<SimpleFeatureType,SimpleFeature> featureCollection){
DataStore data =null;
try {
if (!fileOutput.exists()){
fileOutput.createNewFile();
fileOutput.setWritable(true);
}
FileDataStoreFactorySpi factory = new ShapefileDataStoreFactory();
data = factory.createDataStore( fileOutput.toURI().toURL() );
data.createSchema(featureCollection.getSchema());
exportToShapefile(data,featureCollection);
} catch (Exception e) {
logger.error("Export to shapefile failed",e );
}finally{
if(data!=null)
data.dispose();
}
}
示例2: makeLineLayer
import org.geotools.data.shapefile.ShapefileDataStoreFactory; //导入依赖的package包/类
private void makeLineLayer( IEpanetType[] types, File baseFolder, CoordinateReferenceSystem mapCrs )
throws MalformedURLException, IOException {
SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
String shapefileName = types[0].getShapefileName();
String typeName = types[0].getName();
b.setName(typeName);
b.setCRS(mapCrs);
b.add("the_geom", LineString.class);
for( IEpanetType type : types ) {
b.add(type.getAttributeName(), type.getClazz());
}
SimpleFeatureType tanksType = b.buildFeatureType();
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
File file = new File(baseFolder, shapefileName);
Map<String, Serializable> create = new HashMap<String, Serializable>();
create.put("url", file.toURI().toURL());
ShapefileDataStore newDataStore = (ShapefileDataStore) factory.createNewDataStore(create);
newDataStore.createSchema(tanksType);
Transaction transaction = new DefaultTransaction();
SimpleFeatureStore featureStore = (SimpleFeatureStore) newDataStore.getFeatureSource();
featureStore.setTransaction(transaction);
try {
featureStore.addFeatures(new DefaultFeatureCollection());
transaction.commit();
} catch (Exception problem) {
problem.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
}
}
示例3: writeShapefile
import org.geotools.data.shapefile.ShapefileDataStoreFactory; //导入依赖的package包/类
private void writeShapefile(MemoryDataStore memoryDataStore, File outputFile) throws Exception {
SimpleFeatureSource featureSource = memoryDataStore.getFeatureSource(memoryDataStore.getTypeNames()[0]);
SimpleFeatureType featureType = featureSource.getSchema();
Map<String, java.io.Serializable> creationParams = new HashMap<String, java.io.Serializable>();
creationParams.put("url", DataUtilities.fileToURL(outputFile));
ShapefileDataStoreFactory factory = (ShapefileDataStoreFactory) FileDataStoreFinder.getDataStoreFactory("shp");
ShapefileDataStore dataStore = (ShapefileDataStore) factory.createNewDataStore(creationParams);
dataStore.setCharset(Charset.forName("UTF-8"));
dataStore.createSchema(featureType);
SimpleFeatureStore featureStore = (SimpleFeatureStore) dataStore.getFeatureSource(dataStore.getTypeNames()[0]);
Transaction transaction = new DefaultTransaction();
try {
SimpleFeatureCollection collection = featureSource.getFeatures();
featureStore.addFeatures(collection);
transaction.commit();
} catch (IOException e) {
try {
transaction.rollback();
throw new Exception("Failed to commit data to feature store", e);
} catch (IOException e2 ) {
logger.error("Failed to commit data to feature store", e);
throw new Exception("Transaction rollback failed", e2);
}
} finally {
transaction.close();
}
}
示例4: main
import org.geotools.data.shapefile.ShapefileDataStoreFactory; //导入依赖的package包/类
public static void main(String[] args) throws MalformedURLException,
IOException, NoSuchAuthorityCodeException, FactoryException {
if (args.length < 3) {
System.err.println("Usage: ReprojectShapeFile in.shp out.shp epsg:code");
System.exit(3);
}
File in = new File(args[0]);
File out = new File(args[1]);
CoordinateReferenceSystem crs = CRS.decode(args[2]);
ShapefileDataStoreFactory fac = new ShapefileDataStoreFactory();
FileDataStore inStore = fac.createDataStore(in.toURI().toURL());
FileDataStore outStore = fac.createDataStore(out.toURI().toURL());
SimpleFeatureCollection inFeatures = inStore.getFeatureSource()
.getFeatures();
ReprojectShapeFile reprojector = new ReprojectShapeFile();
SimpleFeatureCollection outFeatures = reprojector
.reproject(inFeatures, crs);
outStore.createSchema(outFeatures.getSchema());
Transaction transaction = new DefaultTransaction("create");
String outName = outStore.getNames().get(0).getLocalPart();
SimpleFeatureSource featureSource = outStore.getFeatureSource(outName);
FeatureStore featureStore = (FeatureStore) featureSource;
featureStore.setTransaction(transaction);
featureStore.addFeatures(outFeatures);
transaction.commit();
outStore.dispose();
}
示例5: setup
import org.geotools.data.shapefile.ShapefileDataStoreFactory; //导入依赖的package包/类
@Override
public void setup(
final Configuration configuration,
final List<ColumnInterface> columnList) throws IOException
{
final URL url = getUrl(configuration);
final ShapefileDataStoreFactory fac = new ShapefileDataStoreFactory();
final Map params = new HashMap();
params.put(ShapefileDataStoreFactory.URLP.key, url);
params.put(ShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key, Boolean.TRUE);
params.put(ShapefileDataStoreFactory.ENABLE_SPATIAL_INDEX, Boolean.TRUE);
params.put(ShapefileDataStoreFactory.MEMORY_MAPPED, Boolean.TRUE);
m_dataStore = fac.createDataStore(params);
final String[] typeNames = m_dataStore.getTypeNames();
m_featureSource = m_dataStore.getFeatureSource(typeNames[0]);
m_geometryName = m_featureSource.getSchema().getGeometryDescriptor().getLocalName();
m_filterFactory = CommonFactoryFinder.getFilterFactory2(null);
m_progressListener = new NullProgressListener();
m_buffer = configuration.getFloat(GeoEnrichmentJob.KEY_BUFFER, 0.000001F);
}
示例6: mergeShapeFile2
import org.geotools.data.shapefile.ShapefileDataStoreFactory; //导入依赖的package包/类
/**
*
* @param collectionsLayer
* @param shpInput
* @param transform
* @param bbox
* @return
* @throws Exception
*/
public static GeometryImage mergeShapeFile2(SimpleFeatureCollection collectionsLayer, File shpInput,GeoTransform transform,Polygon bbox)throws Exception {
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put("url", shpInput.toURI().toURL());
//create a DataStore object to connect to the physical source
DataStore dataStore = DataStoreFinder.getDataStore(params);
//retrieve a FeatureSource to work with the feature data
SimpleFeatureSource shape2 = (SimpleFeatureSource) dataStore.getFeatureSource(dataStore.getTypeNames()[0]);
ClipProcess clip=new ClipProcess();
SimpleFeatureCollection collectionsShape2=shape2.getFeatures();
SimpleFeatureCollection fc=clip.execute(collectionsShape2, bbox,true);
SimpleFeatureSource source = new CollectionFeatureSource(fc);
SimpleFeatureCollection result=joinFeaures(source, shape2);
//create new datastore to save the new shapefile
FileDataStoreFactorySpi factory = new ShapefileDataStoreFactory();
File tmp=new File(SumoPlatform.getApplication().getCachePath()+"\\tmpshape_"+System.currentTimeMillis()+".shp");
Map<String, Serializable> params2 = new HashMap<String, Serializable>();
params2.put("url", tmp.toURI().toURL());
ShapefileDataStore newds=(ShapefileDataStore)factory.createNewDataStore(params2);
String geoName = collectionsLayer.getSchema().getGeometryDescriptor().getType().getName().toString();
//String nameOutput="merge_"+shpInput.getName()+"_"+LayerManager.getIstanceManager().getCurrentImageLayer().getName();
//from here create the new GeometricLayer
Collection<PropertyDescriptor>descriptorsMerge=new ArrayList<>();
descriptorsMerge.addAll(shape2.getSchema().getDescriptors());
descriptorsMerge.addAll(collectionsLayer.getSchema().getDescriptors());
String[] schema = createSchema(descriptorsMerge);
String[] types = createTypes(descriptorsMerge);
GeometryImage out=GeometryImage.createLayerFromFeatures(geoName, newds, result, schema, types,true,transform);
return out;
}
示例7: write
import org.geotools.data.shapefile.ShapefileDataStoreFactory; //导入依赖的package包/类
public static <T> void write(File file,
Consumer<SimpleFeatureTypeBuilder> fun1,
BiConsumer<SimpleFeatureBuilder, T> fun2,
List<T> geometries) {
try {
SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
typeBuilder.setName("MyFeatureType");
typeBuilder.setCRS(DefaultGeographicCRS.WGS84);
fun1.accept(typeBuilder);
SimpleFeatureType type = typeBuilder.buildFeatureType();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(type);
AtomicInteger id = new AtomicInteger(0);
List<SimpleFeature> geoms = geometries.stream().map(g -> {
fun2.accept(featureBuilder, g);
return featureBuilder.buildFeature(String.valueOf(id.getAndIncrement()));
}).collect(toList());
ShapefileDataStoreFactory datastoreFactory = new ShapefileDataStoreFactory();
Map<String, Serializable> params = newHashMap();
params.put("url", file.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
ShapefileDataStore datastore = (ShapefileDataStore) datastoreFactory.createNewDataStore(params);
datastore.createSchema(type);
Transaction transaction = new DefaultTransaction("create");
String typeName = datastore.getTypeNames()[0];
SimpleFeatureSource featureSource = datastore.getFeatureSource(typeName);
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
SimpleFeatureCollection collection = new ListFeatureCollection(type, geoms);
featureStore.setTransaction(transaction);
featureStore.addFeatures(collection);
transaction.commit();
transaction.close();
datastore.dispose();
}
catch (IOException e) {
throw propagate(e);
}
}
示例8: openInputShapefile
import org.geotools.data.shapefile.ShapefileDataStoreFactory; //导入依赖的package包/类
/**
* Open the input shape file and load it into memory.
*/
public void openInputShapefile(String inputShapefile) throws IOException {
File file = new File(inputShapefile);
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
Map<String, Serializable> params = new HashMap<>();
params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
params.put(ShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key, Boolean.TRUE);
params.put(ShapefileDataStoreFactory.ENABLE_SPATIAL_INDEX.key, Boolean.TRUE);
params.put(ShapefileDataStoreFactory.MEMORY_MAPPED.key, Boolean.TRUE);
params.put(ShapefileDataStoreFactory.CACHE_MEMORY_MAPS.key, Boolean.TRUE);
ShapefileDataStore store = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
featureSource = store.getFeatureSource();
// determine the correct case of the tz attribute because its case has been
// changed from efele to evansiroky
SimpleFeatureType schema = featureSource.getSchema();
List<AttributeDescriptor> attributeDescriptorList = schema.getAttributeDescriptors();
for (AttributeDescriptor attributeDescriptor : attributeDescriptorList) {
if ("tzid".equalsIgnoreCase(attributeDescriptor.getLocalName())) {
tzidAttr = attributeDescriptor.getLocalName();
}
}
filterFactory = CommonFactoryFinder.getFilterFactory2(GeoTools.getDefaultHints());
geometryFactory = JTSFactoryFinder.getGeometryFactory();
}
示例9: featureStore
import org.geotools.data.shapefile.ShapefileDataStoreFactory; //导入依赖的package包/类
/**
* Opens a Shapefile as a FeatureStore.
* @param url location of .shp file
* @return a ShapefileDataStore representing the file
* @throws IOException if there is an issue opening the file
*/
public static ShapefileDataStore featureStore(URL url) throws IOException {
HashMap<String, Serializable> dataStoreParams = new HashMap<String, Serializable>();
dataStoreParams.put(ShapefileDataStoreFactory.URLP.key, url);
dataStoreParams.put(ShapefileDataStoreFactory.MEMORY_MAPPED.key, Boolean.TRUE);
dataStoreParams.put(ShapefileDataStoreFactory.CACHE_MEMORY_MAPS.key, Boolean.FALSE);
dataStoreParams.put(ShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key, Boolean.FALSE);
DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory();
ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createDataStore(dataStoreParams);
dataStore.setStringCharset(Charset.forName("UTF-8"));
return dataStore;
}
示例10: init
import org.geotools.data.shapefile.ShapefileDataStoreFactory; //导入依赖的package包/类
@Override
public void init() {
LOGGER.info("Output as shapefile: " + outFile);
file = new File(outFile);
try {
// Define Schema
final SimpleFeatureType TYPE = createFeatureType();
featureBuilder = new SimpleFeatureBuilder(TYPE);
geomF = new GeometryFactory();
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
// define parameter for ShapeFileDataStore object
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put("url", file.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
dataStore.createSchema(TYPE);
// define transaction
transaction = new DefaultTransaction("create");
String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
if (featureSource instanceof SimpleFeatureStore) {
featureStore = (SimpleFeatureStore) featureSource;
featureStore.setTransaction(transaction);
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
示例11: getFeatureCollection
import org.geotools.data.shapefile.ShapefileDataStoreFactory; //导入依赖的package包/类
/**
* @param filename shp file to be opened
* @param encoding encoding of opened shp file
* @return a feature collection of opened shp file
* @throws IOException
* @throws FactoryException
* @throws NoSuchAuthorityCodeException
*/
public static SimpleFeatureCollection getFeatureCollection(String filename, String encoding)
throws IOException, NoSuchAuthorityCodeException, FactoryException {
// Open file
File infile = new File(filename);
// Parameters of ShapefileDatastore
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put(ShapefileDataStoreFactory.URLP.key, infile.toURI().toURL());
// params.put("create spatial index", Boolean.TRUE);
params.put(ShapefileDataStoreFactory.DBFCHARSET.key, encoding);
// -- Create ShapefileDatastore -- //
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
ShapefileDataStore shapefile = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
// shapefile.setStringCharset(Charset.forName("ISO8859-7"));
// DataStore shapefile = new ShapefileDataStore( infile.toURI().toURL(), false);
// Get feature collection
String[] typeName = shapefile.getTypeNames();
SimpleFeatureSource featureSource = shapefile.getFeatureSource(typeName[0]);
// SimpleFeatureCollection collection;
SimpleFeatureCollection featureCollection = featureSource.getFeatures();
// SimpleFeatureIterator clcFeatureIterator =
// clcFeatureCollection.features();
return featureCollection;
}
示例12: collectionToShapeFile
import org.geotools.data.shapefile.ShapefileDataStoreFactory; //导入依赖的package包/类
/**
* <p>
* The easy way to create a shapefile from attributes and geometries
* </p>
* <p>
* <b>NOTE: this doesn't support date attributes</b>
* </p>
*
* @param shapeFilePath the shapefile name
* @param crs the destination crs
* @param fet the featurecollection
* @throws IOException
*/
public static boolean collectionToShapeFile( String shapeFilePath, CoordinateReferenceSystem crs,
SimpleFeatureCollection fet ) throws IOException {
// Create the file you want to write to
File file = null;
if (shapeFilePath.toLowerCase().endsWith(".shp")) { //$NON-NLS-1$
file = new File(shapeFilePath);
} else {
file = new File(shapeFilePath + ".shp"); //$NON-NLS-1$
}
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
Map<String, Serializable> create = new HashMap<String, Serializable>();
create.put("url", file.toURI().toURL());
ShapefileDataStore newDataStore = (ShapefileDataStore) factory.createNewDataStore(create);
newDataStore.createSchema(fet.getSchema());
if (crs != null)
newDataStore.forceSchemaCRS(crs);
Transaction transaction = new DefaultTransaction();
SimpleFeatureStore featureStore = (SimpleFeatureStore) newDataStore.getFeatureSource();
featureStore.setTransaction(transaction);
try {
featureStore.addFeatures(fet);
transaction.commit();
} catch (Exception problem) {
problem.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
}
return true;
}
示例13: createShapeFileDatastore
import org.geotools.data.shapefile.ShapefileDataStoreFactory; //导入依赖的package包/类
/**
* @param name the shapefile name
* @param fieldsSpec to create other fields you can use a string like : <br>
* "geom:MultiLineString,FieldName:java.lang.Integer" <br>
* field name can not be over 10 characters use a ',' between each field <br>
* field types can be : java.lang.Integer, java.lang.Long, // java.lang.Double,
* java.lang.String or java.util.Date
* @return
* @throws Exception
*/
public static ShapefileDataStore createShapeFileDatastore( String name, String fieldsSpec, CoordinateReferenceSystem crs )
throws Exception {
// Create the file you want to write to
File file = null;
if (name.toLowerCase().endsWith(".shp")) {
file = new File(name);
} else {
file = new File(name + ".shp");
}
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
Map<String, Serializable> create = new HashMap<String, Serializable>();
create.put("url", file.toURI().toURL());
ShapefileDataStore myData = (ShapefileDataStore) factory.createNewDataStore(create);
// Tell this shapefile what type of data it will store
// Shapefile handle only : Point, MultiPoint, MultiLineString,
// MultiPolygon
SimpleFeatureType featureType = DataUtilities.createType(name, fieldsSpec);
// Create the Shapefile (empty at this point)
myData.createSchema(featureType);
// Tell the DataStore what type of Coordinate Reference System (CRS)
// to use
myData.forceSchemaCRS(crs);
return myData;
}
示例14: makePointLayer
import org.geotools.data.shapefile.ShapefileDataStoreFactory; //导入依赖的package包/类
private void makePointLayer( IEpanetType[] types, File baseFolder, CoordinateReferenceSystem mapCrs )
throws MalformedURLException, IOException {
SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
String shapefileName = types[0].getShapefileName();
String typeName = types[0].getName();
b.setName(typeName);
b.setCRS(mapCrs);
b.add("the_geom", Point.class);
for( IEpanetType type : types ) {
b.add(type.getAttributeName(), type.getClazz());
}
SimpleFeatureType tanksType = b.buildFeatureType();
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
File file = new File(baseFolder, shapefileName);
Map<String, Serializable> create = new HashMap<String, Serializable>();
create.put("url", file.toURI().toURL());
ShapefileDataStore newDataStore = (ShapefileDataStore) factory.createNewDataStore(create);
newDataStore.createSchema(tanksType);
Transaction transaction = new DefaultTransaction();
SimpleFeatureStore featureStore = (SimpleFeatureStore) newDataStore.getFeatureSource();
featureStore.setTransaction(transaction);
try {
featureStore.addFeatures(new DefaultFeatureCollection());
transaction.commit();
} catch (Exception problem) {
problem.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
}
}
示例15: writeEsriShapefile
import org.geotools.data.shapefile.ShapefileDataStoreFactory; //导入依赖的package包/类
static void writeEsriShapefile(Class<?> geomType, List<SimpleFeature> features, File file) throws IOException {
String geomName = geomType.getSimpleName();
String basename = file.getName();
if (basename.endsWith(FILE_EXTENSION_SHAPEFILE)) {
basename = basename.substring(0, basename.length() - 4);
}
File file1 = new File(file.getParentFile(), basename + "_" + geomName + FILE_EXTENSION_SHAPEFILE);
SimpleFeature simpleFeature = features.get(0);
SimpleFeatureType simpleFeatureType = changeGeometryType(simpleFeature.getType(), geomType);
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
Map<String, Serializable> map = Collections.singletonMap("url", file1.toURI().toURL());
ShapefileDataStore dataStore = (ShapefileDataStore) factory.createNewDataStore(map);
dataStore.createSchema(simpleFeatureType);
String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
DefaultTransaction transaction = new DefaultTransaction("X");
if (featureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
SimpleFeatureCollection collection = new ListFeatureCollection(simpleFeatureType, features);
featureStore.setTransaction(transaction);
// I'm not sure why the next line is necessary (mp/20170627)
// Without it is not working, the wrong feature type is used for writing
// But it is not mentioned in the tutorials
dataStore.getEntry(featureSource.getName()).getState(transaction).setFeatureType(simpleFeatureType);
try {
featureStore.addFeatures(collection);
transaction.commit();
} catch (Exception problem) {
transaction.rollback();
throw new IOException(problem);
} finally {
transaction.close();
}
} else {
throw new IOException(typeName + " does not support read/write access");
}
}