Examples of ShapefileDataStore


Examples of org.geotools.data.shapefile.ShapefileDataStore

        URL targetShapefileAsUrl = targetShapefile.toURI().toURL();
        params.put(ShapefileDataStoreFactory.URLP.key, targetShapefileAsUrl);
        params.put(ShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key, Boolean.FALSE);
        params.put(ShapefileDataStoreFactory.ENABLE_SPATIAL_INDEX.key, Boolean.FALSE);

        ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory
                .createNewDataStore(params);

        SimpleFeatureType outputFeatureType;
        ObjectId featureTypeId;
        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 {
                outputFeatureType = getFeatureType(path, cli);
                featureTypeId = null;
            } catch (GeoToolsOpException e) {
                cli.getConsole().println("No features to export.");
                return;
            }
        }
        dataStore.createSchema(outputFeatureType);

        final String typeName = dataStore.getTypeNames()[0];
        final SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
        if (!(featureSource instanceof SimpleFeatureStore)) {
            throw new CommandFailedException("Could not create feature store.");
        }

        Function<Feature, Optional<Feature>> function = getTransformingFunction(dataStore
                .getSchema());

        final SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
        ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(featureStore)
                .setPath(path).setFilterFeatureTypeId(featureTypeId).setAlter(alter)
View Full Code Here

Examples of org.geotools.data.shapefile.ShapefileDataStore

        SimpleFeatureCollection contourFeatures = makeContourFeatures(computeIsochrone());
        /* Output the staged features to Shapefile */
        final File shapeDir = Files.createTempDir();
        File shapeFile = new File(shapeDir, shpName + ".shp");
        LOG.debug("writing out shapefile {}", shapeFile);
        ShapefileDataStore outStore = new ShapefileDataStore(shapeFile.toURI().toURL());
        outStore.createSchema(contourSchema);
        SimpleFeatureStore featureStore = (SimpleFeatureStore) outStore.getFeatureSource();
        featureStore.addFeatures(contourFeatures);
        shapeDir.deleteOnExit(); // Note: the order is important
        for (File f : shapeDir.listFiles())
            f.deleteOnExit();
        /* Zip up the shapefile components */
 
View Full Code Here

Examples of org.geotools.data.shapefile.ShapefileDataStore

        if (shapefile == null) {
            throw new IllegalStateException("shapefile path is not set");
        }

        // get feature results
        ShapefileDataStore store = new ShapefileDataStore(new File(shapefile).toURI().toURL());
        String name = store.getTypeNames()[0];
        FeatureSource source = store.getFeatureSource(name);
        collection = source.getFeatures();
    }
View Full Code Here

Examples of org.geotools.data.shapefile.ShapefileDataStore

        SimpleFeatureCollection contourFeatures = makeContourFeatures();
        /* Output the staged features to Shapefile */
        final File shapeDir = Files.createTempDir();
        File shapeFile = new File(shapeDir, shpName + ".shp");
        LOG.debug("writing out shapefile {}", shapeFile);
        ShapefileDataStore outStore = new ShapefileDataStore(shapeFile.toURI().toURL());
        outStore.createSchema(contourSchema);
        /* "FeatureSource is used to read features, the subclass FeatureStore is used for
         * read/write access. The way to tell if a File can be written to in GeoTools is to use an
         * instanceof check. */
        SimpleFeatureStore featureStore = (SimpleFeatureStore) outStore.getFeatureSource();
        featureStore.addFeatures(contourFeatures);
        // close?
        shapeDir.deleteOnExit(); // Note: the order is important
        for (File f : shapeDir.listFiles())
            f.deleteOnExit();
View Full Code Here

Examples of org.geotools.data.shapefile.ShapefileDataStore

    @Override
    public FeatureSource<SimpleFeatureType, SimpleFeature> getFeatureSource() {

        try {
            dataStore = new ShapefileDataStore(_path.toURI().toURL());

            String typeNames[] = dataStore.getTypeNames();
            String typeName = typeNames[0];

            return dataStore.getFeatureSource(typeName);
View Full Code Here

Examples of org.geotools.data.shapefile.ShapefileDataStore

        Utilities.ensureNonNull("footprintSummaryFile", footprintSummaryFile);
        Utilities.ensureNonNull("indexFile", indexFile);
        Utilities.ensureNonNull("footprintsLocationGeometryMap", footprintsLocationGeometryMap);
        if (footprintsLocationGeometryMap.isEmpty())
          return;
        final ShapefileDataStore store = new ShapefileDataStore(indexFile.toURI().toURL());

        if (footprintsLocationGeometryMap.isEmpty())
          return;
       
        FileWriter footprintWriter = null;
        FeatureIterator<SimpleFeature> it=null;       
        try {
            final String[] typeNames = store.getTypeNames();
            if (typeNames.length <= 0) {
                throw new IllegalArgumentException("Problems when opening the shapefile, no typenames for the schema are defined");
            }
            final String typeName = typeNames[0];
           
            final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = store.getFeatureSource(typeName);
            final FeatureCollection<SimpleFeatureType, SimpleFeature> features = featureSource.getFeatures();

            if (features == null) {
                if (LOGGER.isLoggable(Level.FINE)) {
                    LOGGER.fine("No features found in the shapefile");
                }
                return;
            }

            // load the feature from the shapefile
            it = features.features();
            if (!it.hasNext())
                throw new IllegalArgumentException("The provided FeatureCollection<SimpleFeatureType, SimpleFeature>  or empty, it's impossible to create an index!");

            footprintWriter = new FileWriter(footprintSummaryFile);
            final BufferedWriter writer = new BufferedWriter(footprintWriter);
            final WKTWriter geometryWriter = new WKTWriter();

            // Scan the index shapefile to get granules location
            while (it.hasNext()) {
                final SimpleFeature feature = it.next();
                final String location = (String) feature.getAttribute("location");
                if (location != null && location.trim().length() > 0) {
                    final String locationKey = location;

                    // Check if a footprint exist in the map for this granule
                    if (footprintsLocationGeometryMap.containsKey(locationKey)) {

                        // Build a featureID=Geometry pair and write it in
                        // the Footprint summary file
                        final String idGeometryPair = FootprintUtils.buildIDGeometryPair(footprintsLocationGeometryMap, feature.getID(), locationKey,geometryWriter);
                        writer.write(idGeometryPair);
                    }
                }
            }
            writer.flush();
            writer.close();
        } catch (Throwable e) {
            // ignore exception
            if (LOGGER.isLoggable(Level.FINEST))
                LOGGER.log(Level.FINEST, e.getLocalizedMessage(), e);
        } finally {
            try {
                if (it != null) {
                   it.close();
                }
            } catch (Throwable e) {
                if (LOGGER.isLoggable(Level.FINEST))
                    LOGGER.log(Level.FINEST, e.getLocalizedMessage(), e);
            }
           
           
            try {
                if (footprintWriter != null) {
                    footprintWriter.flush();

                    footprintWriter.close();
                }
            } catch (Throwable e) {
                if (LOGGER.isLoggable(Level.FINEST))
                    LOGGER.log(Level.FINEST, e.getLocalizedMessage(), e);
                IOUtils.closeQuietly(footprintWriter);
            }
            footprintWriter = null;
           
           
            try{
              store.dispose();
            }catch (Throwable e) {
                if (LOGGER.isLoggable(Level.FINEST))
                    LOGGER.log(Level.FINEST, e.getLocalizedMessage(), e);
            }
        }
View Full Code Here

Examples of org.geotools.data.shapefile.ShapefileDataStore

    @Test
    public void testFindAllFids() throws Exception {
        int expectedCount = 0;
        Set<String> expectedFids = new LinkedHashSet<String>();
        {
            ShapefileDataStore ds = new ShapefileDataStore(backshp.toURI().toURL());
            SimpleFeatureSource featureSource = ds.getFeatureSource();
            SimpleFeatureIterator features = featureSource.getFeatures().features();
            while (features.hasNext()) {
                SimpleFeature next = features.next();
                expectedCount++;
                expectedFids.add(next.getID());
            }
            features.close();
            ds.dispose();
        }

        assertTrue(expectedCount > 0);
        assertEquals(expectedCount, reader.getCount());
View Full Code Here

Examples of org.geotools.data.shapefile.ShapefileDataStore

    @Test
    public void testFindAllFidsReverseOrder() throws Exception {
        int expectedCount = 0;
        Set<String> expectedFids = new TreeSet<String>(Collections.reverseOrder());
        {
            ShapefileDataStore ds = new ShapefileDataStore(backshp.toURI().toURL());
            SimpleFeatureSource featureSource = ds.getFeatureSource();
            SimpleFeatureIterator features = featureSource.getFeatures().features();
            while (features.hasNext()) {
                SimpleFeature next = features.next();
                expectedCount++;
                expectedFids.add(next.getID());
            }
            features.close();
            ds.dispose();
        }

        assertTrue(expectedCount > 0);
        assertEquals(expectedCount, reader.getCount());
View Full Code Here

Examples of org.geotools.data.shapefile.ShapefileDataStore

            POINTMAP.put(20.0, new HashMap<String, Integer>());
            POINTMAP.put(50.0, new HashMap<String, Integer>());

            URL url = TestData.url("shapes/streams.shp");

            ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory()
                    .createDataStore(url);

            String typeName = ds.getSchema().getTypeName();
            REPOSITORY.register("dsStreams", ds);

            MemoryDataStore dsStreams_5 = createMemStoreVertical(ds.getSchema(), "dsStreams_5",
                    "streams_5");
            MemoryDataStore dsStreams_10 = createMemStoreVertical(ds.getSchema(), "dsStreams_10",
                    "streams_10");
            MemoryDataStore dsStreams_20 = createMemStoreVertical(ds.getSchema(), "dsStreams_20",
                    "streams_20");
            MemoryDataStore dsStreams_50 = createMemStoreVertical(ds.getSchema(), "dsStreams_50",
                    "streams_50");

            MemoryDataStore dsStreams_5_10 = createMemStoreMixed(ds.getSchema(), "dsStreams_5_10",
                    "streams_5_10");
            MemoryDataStore dsStreams_20_50 = createMemStoreMixed(ds.getSchema(),
                    "dsStreams_20_50", "streams_20_50");

            MemoryDataStore dsStreams_5_10_20_50 = createMemStoreHorizontal(ds.getSchema(),
                    "dsStreams_5_10_20_50", "streams_5_10_20_50");

            // StreamsFeatureSource = ds.getFeatureSource(typeName);
            Transaction t = new DefaultTransaction();
            Query query = new Query(typeName, Filter.INCLUDE);
            FeatureReader<SimpleFeatureType, SimpleFeature> reader = ds.getFeatureReader(query, t);
            while (reader.hasNext()) {

                SimpleFeature stream = reader.next();

                POINTMAP.get(0.0).put(stream.getID(),
View Full Code Here

Examples of org.geotools.data.shapefile.ShapefileDataStore

        out.close();
        // ////////

        url = TestData.url("shapes/streams.shp");

        ShapefileDataStore shapeDS = (ShapefileDataStore) new ShapefileDataStoreFactory()
                .createDataStore(url);

        Map<String, Serializable> params = new HashMap<String, Serializable>();
        FileDataStoreFactorySpi factory = new ShapefileDataStoreFactory();
        params.put(ShapefileDataStoreFactory.URLP.key, new File("target/0/streams.shp").toURI()
                .toURL());
        ShapefileDataStore ds = (ShapefileDataStore) factory.createNewDataStore(params);

        SimpleFeatureSource fs = shapeDS.getFeatureSource(shapeDS
                .getTypeNames()[0]);

        ds.createSchema(fs.getSchema());
        ds.forceSchemaCRS(fs.getSchema().getCoordinateReferenceSystem());
        FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds
                .getTypeNames()[0], Transaction.AUTO_COMMIT);

        SimpleFeatureIterator it = fs.getFeatures().features();
        try {
            while (it.hasNext()) {
                SimpleFeature f = it.next();
                SimpleFeature fNew = writer.next();
                fNew.setAttributes(f.getAttributes());
                writer.write();
            }
        }
        finally {
            it.close();
        }
        writer.close();
        ds.dispose();
        shapeDS.dispose();

        Toolbox tb = new Toolbox();
        tb.parse(new String[] { "generalize",
                "target" + File.separator + "0" + File.separator + "streams.shp", "target",
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.