@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(); }
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; }
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(); } }
@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]; }
@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]); }
@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(); }
@Test public void testDataStoreFinderFindsIt() throws IOException { DataStore sdeDs = null; DataStoreFinder.scanForPlugins(); sdeDs = DataStoreFinder.getDataStore(testData.getConProps()); assertNotNull(sdeDs); String failMsg = sdeDs + " is not an ArcSDEDataStore"; assertTrue(failMsg, (sdeDs instanceof ArcSDEDataStore)); LOGGER.fine("testFinder OK :" + sdeDs.getClass().getName()); }
private SimpleFeatureSource getLatLonFeatureSource() { try { DataStore ds = LATLON == null ? null : LATLON.get(); if (ds == null) { ds = createLatLonDataStore(); LATLON = new WeakReference<DataStore>(ds); } return ds.getFeatureSource("latlon"); } catch (Exception e) { throw new RuntimeException(e); } }
@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(); }
@Test @Ignore // this test is skipped, it checks something that formally is what we should expect, // but for the moment this breaks big time the shapefile renderer optimizations // until we get rid of the latter let's be a little lax on this one... public void testFeatureSource() throws Exception { File file = copyShapefiles("shapes/archsites.shp"); tempDir = file.getParentFile(); DataStore dds = new DirectoryDataStore(tempDir, getFileStoreFactory()); FeatureSource fs = dds.getFeatureSource("archsites"); assertNotNull(fs); assertSame(dds, fs.getDataStore()); dds.dispose(); }
public void testNamespace() throws Exception { OGRDataStoreFactory factory = new OGRDataStoreFactory(); Map map = new HashMap(); URI namespace = new URI("http://jesse.com"); map.put(OGRDataStoreFactory.NAMESPACEP.key, namespace); map.put(OGRDataStoreFactory.OGR_NAME.key, getAbsolutePath(STATE_POP)); DataStore store = factory.createDataStore(map); assertEquals( namespace.toString(), store .getSchema( STATE_POP.substring(STATE_POP.lastIndexOf('/') + 1, STATE_POP.lastIndexOf('.'))) .getName() .getNamespaceURI()); }
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); }
@SuppressWarnings({"rawtypes", "unchecked"}) private FeatureSource getFeatureSource(String typeName) { FeatureSource featureSource; try { featureSource = dataStore.getFeatureSource(typeName); } catch (Exception e) { throw new GeoToolsOpException(StatusCode.UNABLE_TO_GET_FEATURES); } return new ForwardingFeatureSource(featureSource) { @Override public FeatureCollection getFeatures(Query query) throws IOException { final FeatureCollection features = super.getFeatures(query); return new ForwardingFeatureCollection(features) { @Override public FeatureIterator features() { final FeatureType featureType = getSchema(); final String fidPrefix = featureType.getName().getLocalPart() + "."; FeatureIterator iterator = delegate.features(); return new FidPrefixRemovingIterator(iterator, fidPrefix); } }; } }; }
private void save() throws Exception { FeatureStore fs = (FeatureStore) ds.getFeatureSource("poly_county"); FeatureType ft = fs.getSchema(); MemoryDataStore memorystore = new MemoryDataStore(); ArrayList polys = new ArrayList(resultPolygon); Geometry gfinal = null; if (polys.size() == 1) { gfinal = (Polygon) polys.get(0); // POLYGON } else { GeometryFactory gf = ((Polygon) polys.get(0)).getFactory(); gfinal = new MultiPolygon((Polygon[]) polys.toArray(new Polygon[polys.size()]), gf); } gfinal = gfinal.buffer(0); // for topologic problems. Object[] values = new Object[5]; values[ft.find("module")] = MODULE; values[ft.find("gen_full")] = gfinal; values[ft.find("gen_1")] = generalize(gfinal, tolerance1); ; values[ft.find("gen_2")] = generalize(gfinal, tolerance1); ; values[ft.find("gen_3")] = generalize(gfinal, tolerance1); ; Feature f = ft.create(values); memorystore.addFeature(f); fs.addFeatures(memorystore.getFeatureReader("poly_county")); }
@Test public void testModifyIssue() throws Exception { String newDescription = "new modified description"; // $NON-NLS-1$ list.clear(); list.add(IssuesListTestHelper.createFeatureIssue("test")); // $NON-NLS-1$ list.refresh(); assertEquals(1, list.size()); IIssue issue = list.get(0); issue.setDescription(newDescription); ((IRemoteIssuesList) list).save(issue); FilterFactory factory = CommonFactoryFinder.getFilterFactory(GeoTools.getDefaultHints()); Expression expr2 = factory.literal(issue.getId()); Expression expr1 = factory.property(IssuesListTestHelper.ISSUE_ID_ATTR); Filter filter = factory.equals(expr1, expr2); SimpleFeature next = store .getFeatureSource(featureType.getName().getLocalPart()) .getFeatures(filter) .features() .next(); assertEquals(newDescription, next.getAttribute(IssuesListTestHelper.DESCRIPTION_ATTR)); }
/** * Creates the source datastore reader. * * @param sourceDataStore * @param transaction * @param query * @return * @throws IOException */ protected FeatureStore<SimpleFeatureType, SimpleFeature> createSourceReader( DataStore sourceDataStore, final Transaction transaction, Query query) throws IOException { FeatureStore<SimpleFeatureType, SimpleFeature> featureReader = (FeatureStore<SimpleFeatureType, SimpleFeature>) sourceDataStore.getFeatureSource(query.getTypeName()); featureReader.setTransaction(transaction); return featureReader; }
@Test public void testGet() { ServiceInfo info = store.getInfo(); assertNotNull(info); assertNotNull(info.getTitle()); assertNotNull(info.getDescription()); assertNotNull(info.getSchema()); }
@Override protected void setUpInternal(SystemTestData data) throws Exception { // run all the tests against a store that can do native paging (h2) and one that // can't (property) Catalog cat = getCatalog(); DataStoreInfo ds = cat.getFactory().createDataStore(); ds.setName("foo"); ds.setWorkspace(cat.getDefaultWorkspace()); Map params = ds.getConnectionParameters(); params.put("dbtype", "h2"); params.put("database", getTestData().getDataDirectoryRoot().getAbsolutePath()); cat.add(ds); FeatureSource fs1 = getFeatureSource(SystemTestData.FIFTEEN); FeatureSource fs2 = getFeatureSource(SystemTestData.SEVEN); DataStore store = (DataStore) ds.getDataStore(null); SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.init((SimpleFeatureType) fs1.getSchema()); tb.add("num", Integer.class); tb.remove("boundedBy"); store.createSchema(tb.buildFeatureType()); tb.init((SimpleFeatureType) fs2.getSchema()); tb.add("num", Integer.class); tb.remove("boundedBy"); store.createSchema(tb.buildFeatureType()); CatalogBuilder cb = new CatalogBuilder(cat); cb.setStore(ds); FeatureStore fs = (FeatureStore) store.getFeatureSource("Fifteen"); addFeatures(fs, fs1.getFeatures()); FeatureTypeInfo ft = cb.buildFeatureType(fs); cat.add(ft); fs = (FeatureStore) store.getFeatureSource("Seven"); addFeatures(fs, fs2.getFeatures()); ft = cb.buildFeatureType(fs); cat.add(ft); }
@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); }
@Test public void testRemoveInt() throws Exception { list.remove(1); assertEquals(3, list.size()); assertEquals("0", list.get(0).getId()); // $NON-NLS-1$ assertEquals("2", list.get(1).getId()); // $NON-NLS-1$ assertEquals("3", list.get(2).getId()); // $NON-NLS-1$ assertEquals( 3, store.getFeatureSource(featureType.getName().getLocalPart()).getCount(Query.ALL)); }
@Test public void testInitialLoad() throws Exception { assertEquals(4, list.size()); assertEquals("0", list.get(0).getId()); // $NON-NLS-1$ assertEquals("1", list.get(1).getId()); // $NON-NLS-1$ assertEquals("2", list.get(2).getId()); // $NON-NLS-1$ assertEquals("3", list.get(3).getId()); // $NON-NLS-1$ assertEquals( 4, store.getFeatureSource(featureType.getName().getLocalPart()).getCount(Query.ALL)); }
protected void closeResource(DataStore dataStore) { if (dataStore != null) { try { dataStore.dispose(); } catch (Throwable t) { if (LOGGER.isErrorEnabled()) { LOGGER.error("Error closing datastore connection"); } } } }
/** * 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()); }
@Override public void dispose(IProgressMonitor monitor) { super.dispose(monitor); // clean up members if (dataStore != null) { dataStore.dispose(); dataStore = null; } if (resources != null) { resources = null; } }
/** * Test that a filter query issued. * * @param filter filter to be passed in the query to determine the subset of features to be * returned, or null for all features * @param expectedFeatureIds integer id for returned features, matching the expected row ids * @throws Exception */ private void runFilterTest(Filter filter, int[] expectedFeatureIds) throws Exception { DataStore datastore = null; try { datastore = DataStoreFinder.getDataStore(getParams()); assertNotNull(datastore); Query query = new DefaultQuery(TEST_TABLE_NAME, filter); FeatureReader<SimpleFeatureType, SimpleFeature> reader = null; try { /* * List of all the integer feature ids seen in the features returned for this query. */ List<Integer> featureIds = new ArrayList<Integer>(); reader = datastore.getFeatureReader(query, Transaction.AUTO_COMMIT); for (SimpleFeature feature = null; reader.hasNext(); ) { feature = reader.next(); /* * Convert long feature id of the form test_table_name.1234 to the numeric id * used when creating the row. This relies on the behaviour of the postgis fid * mapper. */ Integer id = Integer.valueOf(feature.getID().replace(TEST_TABLE_NAME + ".", "")); featureIds.add(id); } /* * The query succeeded as expected if and only if the sorted lists of returned and * expected ids are equal. The expected ids are converted to a List of Integers to * leverage Collections comparison. */ assertEquals(sortedList(expectedFeatureIds), sorted(featureIds)); } finally { if (reader != null) { reader.close(); } } } finally { if (datastore != null) { datastore.dispose(); } } }
/** * 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; }
/** * Executes the describe command using the provided options. * * @param cli * @see * org.geogit.geotools.cli.porcelain.AbstractOracleCommand#runInternal(org.geogit.cli.GeogitCLI) */ @Override protected void runInternal(GeogitCLI cli) throws IOException { DataStore dataStore = getDataStore(); try { cli.getConsole().println("Fetching table..."); Optional<Map<String, String>> propertyMap = cli.getGeogit().command(DescribeOp.class).setTable(table).setDataStore(dataStore).call(); if (propertyMap.isPresent()) { cli.getConsole().println("Table : " + table); cli.getConsole().println("----------------------------------------"); for (Entry<String, String> entry : propertyMap.get().entrySet()) { cli.getConsole().println("\tProperty : " + entry.getKey()); cli.getConsole().println("\tType : " + entry.getValue()); cli.getConsole().println("----------------------------------------"); } } else { throw new CommandFailedException("Could not find the specified table."); } } catch (GeoToolsOpException e) { switch (e.statusCode) { case TABLE_NOT_DEFINED: throw new CommandFailedException("No table supplied.", e); case UNABLE_TO_GET_FEATURES: throw new CommandFailedException("Unable to read the feature source.", e); case UNABLE_TO_GET_NAMES: throw new CommandFailedException("Unable to read feature types.", e); default: throw new CommandFailedException("Exception: " + e.statusCode.name(), e); } } finally { dataStore.dispose(); cli.getConsole().flush(); } }
@Test public void testAddIIssue() throws Exception { FeatureIssue issue = IssuesListTestHelper.createFeatureIssue("new"); // $NON-NLS-1$ list.add(issue); assertEquals(5, list.size()); assertEquals("new", list.get(4).getId()); // $NON-NLS-1$ list.refresh(); assertEquals(5, list.size()); assertEquals("new", list.get(4).getId()); // $NON-NLS-1$ assertEquals("groupID", list.get(4).getGroupId()); // $NON-NLS-1$ assertEquals("test description", list.get(4).getDescription()); // $NON-NLS-1$ assertEquals( 5, store.getFeatureSource(featureType.getName().getLocalPart()).getCount(Query.ALL)); }