@Test public void testSchema() throws Exception { File file = copyShapefiles("shapes/archsites.shp"); tempDir = file.getParentFile(); DataStore dds = new DirectoryDataStore(tempDir, getFileStoreFactory()); assertEquals(1, dds.getTypeNames().length); assertEquals("archsites", dds.getTypeNames()[0]); dds.dispose(); }
public MapContext buildContext() { RandomStyleFactory RANDOM_STYLE_FACTORY = new RandomStyleFactory(); MapContext context = null; MapLayer layer; try { context = new DefaultMapContext(DefaultGeographicCRS.WGS84); DataStore store = DataStoreFinder.getDataStore( new SingletonMap( "url", Demo_ContextTree.class.getResource( "/org/geotools/gui/swing/demo/shape/test_polygon.shp"))); FeatureSource<SimpleFeatureType, SimpleFeature> fs = store.getFeatureSource(store.getTypeNames()[0]); Style style = RANDOM_STYLE_FACTORY.createRandomVectorStyle(fs); layer = new DefaultMapLayer(fs, style); layer.setTitle("demo_polygon.shp"); context.addLayer(layer); store = DataStoreFinder.getDataStore( new SingletonMap( "url", Demo_ContextTree.class.getResource( "/org/geotools/gui/swing/demo/shape/test_ligne.shp"))); fs = store.getFeatureSource(store.getTypeNames()[0]); style = RANDOM_STYLE_FACTORY.createRandomVectorStyle(fs); layer = new DefaultMapLayer(fs, style); layer.setTitle("demo_line.shp"); context.addLayer(layer); store = DataStoreFinder.getDataStore( new SingletonMap( "url", Demo_ContextTree.class.getResource( "/org/geotools/gui/swing/demo/shape/test_point.shp"))); fs = store.getFeatureSource(store.getTypeNames()[0]); style = RANDOM_STYLE_FACTORY.createRandomVectorStyle(fs); layer = new DefaultMapLayer(fs, style); layer.setTitle("demo_point.shp"); context.addLayer(layer); context.setTitle("DemoContext"); } catch (Exception ex) { ex.printStackTrace(); } return context; }
protected DataStore createSourceDataStore(FileSystemEvent fileEvent) throws IOException, ActionException { updateTask("Connecting to source DataStore"); String fileType = getFileType(fileEvent); FeatureConfiguration sourceFeature = configuration.getSourceFeature(); if (fileType.equals("xml")) { InputStream inputXML = null; try { inputXML = new FileInputStream(fileEvent.getSource()); sourceFeature = FeatureConfiguration.fromXML(inputXML); } catch (Exception e) { throw new IOException("Unable to load input XML", e); } finally { IOUtils.closeQuietly(inputXML); } } else if (fileType.equals("shp")) { sourceFeature.getDataStore().put("url", DataUtilities.fileToURL(fileEvent.getSource())); } DataStore source = createDataStore(sourceFeature.getDataStore()); // if no typeName is configured, takes the first one registered in store if (sourceFeature.getTypeName() == null) { sourceFeature.setTypeName(source.getTypeNames()[0]); } // if no CRS is configured, takes if from the feature if (sourceFeature.getCrs() == null) { sourceFeature.setCoordinateReferenceSystem( source.getSchema(sourceFeature.getTypeName()).getCoordinateReferenceSystem()); } configuration.setSourceFeature(sourceFeature); return source; }
@Test public void testCreateSchema() throws Exception { File dir = File.createTempFile("foo", "shp", new File("target")); dir.delete(); dir.mkdir(); DataStore ds = new DirectoryDataStore(dir, getFileStoreFactory()); assertEquals(0, ds.getTypeNames().length); SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.setName("foo"); tb.add("geom", Polygon.class); tb.add("bar", Integer.class); ds.createSchema(tb.buildFeatureType()); SimpleFeatureType ft = ds.getSchema("foo"); assertNotNull(ft); // clean up ds.dispose(); for (File f : dir.listFiles()) { f.delete(); } dir.delete(); }
public static void main(String[] args) { System.out.println("WFS Demo"); try { // URL url = new // URL("http://www2.dmsolutions.ca/cgi-bin/mswfs_gmap?version=1.0.0&request=getcapabilities&service=wfs"); URL url = new URL("http://www.refractions.net:8080/geoserver/wfs?REQUEST=GetCapabilities"); Map m = new HashMap(); m.put(WFSDataStoreFactory.URL.key, url); m.put(WFSDataStoreFactory.TIMEOUT.key, new Integer(10000)); m.put(WFSDataStoreFactory.PROTOCOL.key, Boolean.FALSE); DataStore wfs = (new WFSDataStoreFactory()).createNewDataStore(m); Query query = new DefaultQuery(wfs.getTypeNames()[1]); FeatureReader<SimpleFeatureType, SimpleFeature> ft = wfs.getFeatureReader(query, Transaction.AUTO_COMMIT); int count = 0; while (ft.hasNext()) if (ft.next() != null) count++; System.out.println("Found " + count + " features"); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchElementException e) { e.printStackTrace(); } catch (IllegalAttributeException e) { e.printStackTrace(); } }
@SuppressWarnings("unchecked") private SimpleFeatureSource getFeatureSource(final URL shpfile) throws IOException { Map params = new HashMap<String, String>(); params.put(ShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key, "false"); params.put(ShapefileDataStoreFactory.URLP.key, shpfile); DataStore ds = DataStoreFinder.getDataStore(params); return ds.getFeatureSource(ds.getTypeNames()[0]); }
@Override public String getTypeName(URL url) throws IOException { DataStore ds = createDataStore(url); String[] names = ds.getTypeNames(); assert names.length == 1 : "Invalid number of type names for csv file store"; ds.dispose(); return names[0]; }
/** * test that a ArcSDEDataStore that connects to de configured test database contains the tables * defined by the parameters "point_table", "line_table" and "polygon_table", wether ot not * they're defined as single table names or as full qualified sde table names (i.e. * SDE.SDE.TEST_POINT) * * @throws IOException * @throws SeException */ @Test public void testGetTypeNames() throws IOException, SeException { String[] featureTypes = store.getTypeNames(); assertNotNull(featureTypes); // if (LOGGER.isLoggable(Level.FINE)) { // for (int i = 0; i < featureTypes.length; i++) // System.out.println(featureTypes[i]); // } testTypeExists(featureTypes, testData.getTempTableName()); }
@Test public void testTypeNames() throws Exception { copyShapefiles("shapes/archsites.shp"); File f = copyShapefiles("shapes/bugsites.shp"); tempDir = f.getParentFile(); DataStore store = new DirectoryDataStore(tempDir, getFileStoreFactory()); List<String> typeNames = Arrays.asList(store.getTypeNames()); assertEquals(2, typeNames.size()); assertTrue(typeNames.contains("archsites")); assertTrue(typeNames.contains("bugsites")); store.dispose(); }
/** * Builds a FeatureStore for the output Feature. * * @param store * @param schema * @return * @throws IOException */ protected FeatureStore<SimpleFeatureType, SimpleFeature> createOutputWriter( DataStore store, SimpleFeatureType schema, Transaction transaction) throws IOException { String destTypeName = schema.getTypeName(); boolean createSchema = true; for (String typeName : store.getTypeNames()) { if (typeName.equalsIgnoreCase(destTypeName)) { createSchema = false; destTypeName = typeName; } } // check for case changing in typeName if (createSchema) { store.createSchema(schema); for (String typeName : store.getTypeNames()) { if (!typeName.equals(destTypeName) && typeName.equalsIgnoreCase(destTypeName)) { destTypeName = typeName; } } } FeatureStore<SimpleFeatureType, SimpleFeature> result = (FeatureStore<SimpleFeatureType, SimpleFeature>) store.getFeatureSource(destTypeName); result.setTransaction(transaction); return result; }
public void update(Object updatedObject) { if (updatedObject instanceof List) { String text = null; List layers = (List) updatedObject; for (Object layer : layers) { if (layer instanceof JGrassMapGeoResource) { JGrassMapGeoResource rasterMapResource = (JGrassMapGeoResource) layer; try { text = rasterMapResource.getInfo(null).getName(); JGrassRegion fileWindow = rasterMapResource.getFileWindow(); if (fileWindow != null) { setWidgetsToWindow(fileWindow); } } catch (IOException e1) { return; } } else if (layer instanceof DataStore || layer instanceof UDIGFeatureStore) { try { DataStore store = ((DataStore) layer); FeatureSource featureStore = store.getFeatureSource(store.getTypeNames()[0]); Envelope envelope = featureStore.getBounds(); ActiveRegionStyle style = getActiveRegionStyle(); JGrassRegion activeWindow = new JGrassRegion( style.west, style.east, style.south, style.north, style.rows, style.cols); JGrassRegion newWindow = JGrassRegion.adaptActiveRegionToEnvelope(envelope, activeWindow); northText.setText(String.valueOf(newWindow.getNorth())); southText.setText(String.valueOf(newWindow.getSouth())); eastText.setText(String.valueOf(newWindow.getEast())); westText.setText(String.valueOf(newWindow.getWest())); textModified(bound_type); } catch (IOException e1) { e1.printStackTrace(); return; } } else { return; } } if (text == null) { return; } } }
@Test public void testFeatureReaderWithFilterBBoxPOST() throws NoSuchElementException, IllegalAttributeException, IOException, SAXException, IllegalFilterException { if (url == null) return; Map m = new HashMap(); m.put(WFSDataStoreFactory.URL.key, url); m.put(WFSDataStoreFactory.TIMEOUT.key, new Integer(100000)); DataStore post = (WFS_1_0_0_DataStore) (new WFSDataStoreFactory()).createDataStore(m); String typeName = post.getTypeNames()[0]; Envelope bbox = post.getFeatureSource(typeName).getBounds(); WFSDataStoreReadTest.doFeatureReaderWithBBox(url, true, false, 0, bbox); }
@Override public <T> T resolve(Class<T> adaptee, IProgressMonitor monitor) throws IOException { if (null == adaptee) { return null; } if (adaptee.isAssignableFrom(FeatureSource.class)) { DataStore ds = this.service.resolve(DataStore.class, monitor); String[] a = ds.getTypeNames(); String featureName = a[0]; FeatureSource<SimpleFeatureType, SimpleFeature> fs = ds.getFeatureSource(featureName); return adaptee.cast(fs); } return super.resolve(adaptee, monitor); }
/** * This test is made with mock objects because the property data store does not generate fids in * the <type>.<id> form */ @SuppressWarnings("unchecked") @Test public void testAppend() throws Exception { SimpleFeatureType type = DataUtilities.createType("trees", "the_geom:Point,FID:String,NAME:String"); SimpleFeatureStore fs = createMock(SimpleFeatureStore.class); expect(fs.addFeatures(isA(FeatureCollection.class))) .andReturn(Collections.singletonList((FeatureId) (new FeatureIdImpl("trees.105")))); replay(fs); DataStore ds = createMock(DataStore.class); expect(ds.getTypeNames()).andReturn(new String[] {"trees"}).anyTimes(); expect(ds.getSchema("trees")).andReturn(type).anyTimes(); expect(ds.getFeatureSource("trees")).andReturn(fs); replay(ds); RetypingDataStore rts = new RetypingDataStore(ds) { @Override protected String transformFeatureTypeName(String originalName) { return "oaks"; } }; SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(type); WKTReader reader = new WKTReader(); sfb.set("the_geom", reader.read("POINT (0.002 0.0008)")); sfb.set("FID", "023"); sfb.set("NAME", "Old oak"); SimpleFeature feature = sfb.buildFeature(null); SimpleFeatureCollection fc = DataUtilities.collection(feature); SimpleFeatureStore store = (SimpleFeatureStore) rts.getFeatureSource("oaks"); List<FeatureId> ids = store.addFeatures(fc); assertEquals(1, ids.size()); String id = ((FeatureId) ids.iterator().next()).getID(); assertTrue("Id does not start with " + "oaks" + " it's " + id, id.startsWith("oaks")); }
private String[] checkPreconditions() { if (dataStore == null) { throw new GeoToolsOpException(StatusCode.DATASTORE_NOT_DEFINED); } if ((table == null || table.isEmpty()) && !(all)) { throw new GeoToolsOpException(StatusCode.TABLE_NOT_DEFINED); } if (table != null && !table.isEmpty() && all) { throw new GeoToolsOpException(StatusCode.ALL_AND_TABLE_DEFINED); } String[] typeNames; if (all) { try { typeNames = dataStore.getTypeNames(); } catch (Exception e) { throw new GeoToolsOpException(StatusCode.UNABLE_TO_GET_NAMES); } if (typeNames.length == 0) { throw new GeoToolsOpException(StatusCode.NO_FEATURES_FOUND); } } else { SimpleFeatureType schema; try { schema = dataStore.getSchema(table); } catch (IOException e) { throw new GeoToolsOpException(StatusCode.TABLE_NOT_FOUND); } Preconditions.checkNotNull(schema); typeNames = new String[] {table}; } if (typeNames.length > 1 && alter && all) { throw new GeoToolsOpException(StatusCode.ALTER_AND_ALL_DEFINED); } return typeNames; }
/** * Extract the data as defined in the request object. * * @return the directory that contains the extracted file */ public File extract(ExtractorLayerRequest request) throws IOException, TransformException, FactoryException { if (request._owsType != OWSType.WFS) { throw new IllegalArgumentException(request._owsType + "must be WFS for the WfsExtractor"); } Map<String, Serializable> params = new HashMap<String, Serializable>(); params.put(WFSDataStoreFactory.URL.key, request.capabilitiesURL("WFS", "1.0.0")); params.put(WFSDataStoreFactory.LENIENT.key, true); params.put(WFSDataStoreFactory.PROTOCOL.key, true); params.put(WFSDataStoreFactory.TIMEOUT.key, Integer.valueOf(60000)); params.put(WFSDataStoreFactory.MAXFEATURES.key, Integer.valueOf(0)); // HACK I want unrestricted access to layers. // Security check takes place in ExtractorThread if (_secureHost.equalsIgnoreCase(request._url.getHost()) || "127.0.0.1".equalsIgnoreCase(request._url.getHost()) || "localhost".equalsIgnoreCase(request._url.getHost())) { LOG.debug( "WfsExtractor.extract - Secured Server: Adding extractionUserName to connection params"); if (_adminUsername != null) params.put(WFSDataStoreFactory.USERNAME.key, _adminUsername); if (_adminPassword != null) params.put(WFSDataStoreFactory.PASSWORD.key, _adminPassword); } else { LOG.debug("WfsExtractor.extract - Non Secured Server"); } DataStore sourceDs = DataStoreFinder.getDataStore(params); // WFS-ng: we need to convert the schema name String typeName = request.getWFSName(); SimpleFeatureType sourceSchema = null; // prefixed typeName if (typeName.contains(":")) { typeName = typeName.replaceFirst(":", "_"); sourceSchema = sourceDs.getSchema(typeName); } // Not prefixed one (mapserver ?) else { // Recreating the datastore forcing wfs 1.1.0, so that (presuming // the remote server is actually powered by MapServer), we would // have a typename prefixed with the same convention as before. params.put(WFSDataStoreFactory.URL.key, request.capabilitiesURL("WFS", "1.1.0")); // params.put(WFSDataStoreFactory.WFS_STRATEGY.key, "mapserver"); sourceDs = DataStoreFinder.getDataStore(params); String[] typeNames = sourceDs.getTypeNames(); for (String s : typeNames) { if (s.contains(typeName)) { typeName = s; sourceSchema = sourceDs.getSchema(s); // replace the expected typename in the request break; } } if (sourceSchema == null) { throw new IOException("Unable to find the remote layer " + typeName); } } Query query = createQuery(request, sourceSchema); SimpleFeatureCollection features = sourceDs.getFeatureSource(typeName).getFeatures(query); ProgressListener progressListener = new NullProgressListener() { @Override public void exceptionOccurred(Throwable exception) { throw new RuntimeException(exception); } }; File basedir = request.createContainingDir(_basedir); basedir.mkdirs(); FeatureWriterStrategy featuresWriter; BBoxWriter bboxWriter; LOG.debug("Number of features returned : " + features.size()); if ("shp".equalsIgnoreCase(request._format)) { featuresWriter = new ShpFeatureWriter(progressListener, sourceSchema, basedir, features); bboxWriter = new BBoxWriter( request._bbox, basedir, OGRFeatureWriter.FileFormat.shp, request._projection, progressListener); } else if ("mif".equalsIgnoreCase(request._format)) { // featuresWriter = new MifFeatureWriter(progressListener, sourceSchema, basedir, features); featuresWriter = new OGRFeatureWriter( progressListener, sourceSchema, basedir, OGRFeatureWriter.FileFormat.mif, features); bboxWriter = new BBoxWriter( request._bbox, basedir, OGRFeatureWriter.FileFormat.mif, request._projection, progressListener); } else if ("tab".equalsIgnoreCase(request._format)) { featuresWriter = new OGRFeatureWriter( progressListener, sourceSchema, basedir, OGRFeatureWriter.FileFormat.tab, features); bboxWriter = new BBoxWriter( request._bbox, basedir, OGRFeatureWriter.FileFormat.tab, request._projection, progressListener); } else if ("kml".equalsIgnoreCase(request._format)) { featuresWriter = new OGRFeatureWriter( progressListener, sourceSchema, basedir, OGRFeatureWriter.FileFormat.kml, features); bboxWriter = new BBoxWriter( request._bbox, basedir, OGRFeatureWriter.FileFormat.kml, request._projection, progressListener); } else { throw new IllegalArgumentException(request._format + " is not a recognized vector format"); } // generates the feature files and bbox file featuresWriter.generateFiles(); bboxWriter.generateFiles(); return basedir; }
/** Executes the export command using the provided options. */ @Override protected final void runInternal(GeogigCLI cli) throws IOException { if (args.isEmpty()) { printUsage(cli); throw new CommandFailedException(); } String path = args.get(0); String tableName = args.get(1); checkParameter(tableName != null && !tableName.isEmpty(), "No table name specified"); DataStore dataStore = getDataStore(); ObjectId featureTypeId = null; if (!Arrays.asList(dataStore.getTypeNames()).contains(tableName)) { SimpleFeatureType outputFeatureType; if (sFeatureTypeId != null) { // Check the feature type id string is a correct id Optional<ObjectId> id = cli.getGeogig().command(RevParse.class).setRefSpec(sFeatureTypeId).call(); checkParameter(id.isPresent(), "Invalid feature type reference", sFeatureTypeId); TYPE type = cli.getGeogig().command(ResolveObjectType.class).setObjectId(id.get()).call(); checkParameter( type.equals(TYPE.FEATURETYPE), "Provided reference does not resolve to a feature type: ", sFeatureTypeId); outputFeatureType = (SimpleFeatureType) cli.getGeogig() .command(RevObjectParse.class) .setObjectId(id.get()) .call(RevFeatureType.class) .get() .type(); featureTypeId = id.get(); } else { try { SimpleFeatureType sft = getFeatureType(path, cli); outputFeatureType = new SimpleFeatureTypeImpl( new NameImpl(tableName), sft.getAttributeDescriptors(), sft.getGeometryDescriptor(), sft.isAbstract(), sft.getRestrictions(), sft.getSuper(), sft.getDescription()); } catch (GeoToolsOpException e) { throw new CommandFailedException("No features to export.", e); } } try { dataStore.createSchema(outputFeatureType); } catch (IOException e) { throw new CommandFailedException("Cannot create new table in database", e); } } else { if (!overwrite) { throw new CommandFailedException("The selected table already exists. Use -o to overwrite"); } } SimpleFeatureSource featureSource = dataStore.getFeatureSource(tableName); if (!(featureSource instanceof SimpleFeatureStore)) { throw new CommandFailedException("Can't write to the selected table"); } SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource; if (overwrite) { try { featureStore.removeFeatures(Filter.INCLUDE); } catch (IOException e) { throw new CommandFailedException("Error truncating table: " + e.getMessage(), e); } } ExportOp op = cli.getGeogig() .command(ExportOp.class) .setFeatureStore(featureStore) .setPath(path) .setFilterFeatureTypeId(featureTypeId) .setAlter(alter); if (defaultType) { op.exportDefaultFeatureType(); } try { op.setProgressListener(cli.getProgressListener()).call(); } catch (IllegalArgumentException iae) { throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae); } catch (GeoToolsOpException e) { switch (e.statusCode) { case MIXED_FEATURE_TYPES: throw new CommandFailedException( "The selected tree contains mixed feature types. Use --defaulttype or --featuretype <feature_type_ref> to export.", e); default: throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e); } } cli.getConsole().println(path + " exported successfully to " + tableName); }
private SimpleFeatureIterator getIterator() throws Exception { String typeName = dataStore.getTypeNames()[0]; SimpleFeatureCollection collection = dataStore.getFeatureSource(typeName).getFeatures(); return collection.features(); }