Search in sources :

Example 6 with FeatureCollection

use of org.geotools.feature.FeatureCollection in project polymap4-core by Polymap4.

the class DataSourceProcessor method addFeaturesRequest.

@Override
public void addFeaturesRequest(AddFeaturesRequest request, ProcessorContext context) throws Exception {
    Collection<Feature> features = request.getFeatures();
    log.debug("addFeatures(): Features: " + features.size());
    FeatureCollection fc = new AdaptorFeatureCollection("features", (SimpleFeatureType) fs.getSchema()) {

        @Override
        protected void closeIterator(Iterator it) {
        }

        @Override
        protected Iterator openIterator() {
            return features.iterator();
        }

        @Override
        public int size() {
            return features.size();
        }
    };
    List<FeatureId> result = ((FeatureStore) fs).addFeatures(fc);
    context.sendResponse(new ModifyFeaturesResponse(new FidSet(result)));
    context.sendResponse(ProcessorResponse.EOP);
}
Also used : FeatureId(org.opengis.filter.identity.FeatureId) FeatureCollection(org.geotools.feature.FeatureCollection) AdaptorFeatureCollection(org.geotools.feature.collection.AdaptorFeatureCollection) AdaptorFeatureCollection(org.geotools.feature.collection.AdaptorFeatureCollection) FeatureIterator(org.geotools.feature.FeatureIterator) Iterator(java.util.Iterator) Feature(org.opengis.feature.Feature) FeatureStore(org.geotools.data.FeatureStore)

Example 7 with FeatureCollection

use of org.geotools.feature.FeatureCollection in project polymap4-core by Polymap4.

the class DataSourceProcessor method getFeatureSizeRequest.

@Override
public void getFeatureSizeRequest(GetFeaturesSizeRequest request, ProcessorContext context) throws Exception {
    FeatureCollection fc = fs.getFeatures(request.getQuery());
    int result = fc.size();
    context.sendResponse(new GetFeaturesSizeResponse(result));
    context.sendResponse(ProcessorResponse.EOP);
}
Also used : FeatureCollection(org.geotools.feature.FeatureCollection) AdaptorFeatureCollection(org.geotools.feature.collection.AdaptorFeatureCollection)

Example 8 with FeatureCollection

use of org.geotools.feature.FeatureCollection in project OpenTripPlanner by opentripplanner.

the class ResultSet method writeIsochrones.

/**
 * Write the isochrones as GeoJSON
 */
public void writeIsochrones(JsonGenerator jgen) throws IOException {
    if (this.isochrones == null)
        return;
    FeatureJSON fj = new FeatureJSON();
    FeatureCollection fc = LIsochrone.makeContourFeatures(Arrays.asList(isochrones));
    StringWriter sw = new StringWriter();
    fj.writeFeatureCollection(fc, sw);
    // TODO cludge
    String json = sw.toString();
    jgen.writeRaw(json.substring(1, json.length() - 1));
}
Also used : FeatureJSON(org.geotools.geojson.feature.FeatureJSON) StringWriter(java.io.StringWriter) FeatureCollection(org.geotools.feature.FeatureCollection)

Example 9 with FeatureCollection

use of org.geotools.feature.FeatureCollection in project v-leaflet by mstahv.

the class GeoJSONExample method getTestComponent.

@Override
public Component getTestComponent() {
    leafletMap = new LMap();
    leafletMap.setWidth("600px");
    leafletMap.setHeight("400px");
    /*
         * Note, this is just one option to read GeoJSON in java. Here, using 
         * helper from geotools library. In some simple cases approach to use
         * plain Json library like Jackson or GSON might be better.
         */
    FeatureJSON io = new FeatureJSON();
    try {
        long currentTimeMillis = System.currentTimeMillis();
        // Look ma, no proxy needed, how cool is that!
        FeatureCollection fc = io.readFeatureCollection(new URL("https://gist.githubusercontent.com/hrbrmstr/91ea5cc9474286c72838/raw/59421ff9b268ff0929b051ddafafbeb94a4c1910/continents.json").openStream());
        Logger.getLogger(GeoJSONExample.class.getName()).severe("Download in " + (System.currentTimeMillis() - currentTimeMillis));
        currentTimeMillis = System.currentTimeMillis();
        FeatureIterator iterator = fc.features();
        try {
            while (iterator.hasNext()) {
                Feature feature = iterator.next();
                Geometry geometry = (Geometry) feature.getDefaultGeometryProperty().getValue();
                // The geojson provided in example is rather complex (several megabytes)
                // Use JTS to simplyfy. Note that it is rather easy to use
                // different settings on different zoom levels, as well as decide
                // to drop the feature form client altogether
                geometry = DouglasPeuckerSimplifier.simplify(geometry, 0.2);
                // In this example can be Polygon/Multipolygon
                Collection<LeafletLayer> toLayers = JTSUtil.toLayers(geometry);
                for (LeafletLayer l : toLayers) {
                    leafletMap.addComponent(l);
                    if (l instanceof LLayerGroup) {
                        LLayerGroup group = (LLayerGroup) l;
                        Iterator<Component> components = group.getComponentIterator();
                        while (components.hasNext()) {
                            LPolygon lPolygon = (LPolygon) components.next();
                            lPolygon.setStroke(false);
                            lPolygon.setFillColor("brown");
                        }
                    }
                }
            }
            Logger.getLogger(GeoJSONExample.class.getName()).severe("Reducing and creating layers " + (System.currentTimeMillis() - currentTimeMillis));
        } finally {
            iterator.close();
        }
    } catch (MalformedURLException ex) {
        Logger.getLogger(GeoJSONExample.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(GeoJSONExample.class.getName()).log(Level.SEVERE, null, ex);
    }
    leafletMap.zoomToContent();
    return leafletMap;
}
Also used : MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Feature(org.opengis.feature.Feature) URL(java.net.URL) FeatureIterator(org.geotools.feature.FeatureIterator) Geometry(org.locationtech.jts.geom.Geometry) FeatureJSON(org.geotools.geojson.feature.FeatureJSON) FeatureCollection(org.geotools.feature.FeatureCollection) Component(com.vaadin.ui.Component)

Example 10 with FeatureCollection

use of org.geotools.feature.FeatureCollection in project v-leaflet by mstahv.

the class ChoroplethExample method getTestComponent.

@Override
public Component getTestComponent() {
    leafletMap = new LMap();
    leafletMap.addLayer(new LOpenStreetMapLayer());
    leafletMap.setView(37.8, -96.0, 4.0);
    /*
         * Reading from geojson here, but typically you'd just query
         * your DB directly for the data.
         */
    FeatureJSON io = new FeatureJSON();
    try {
        // 
        // 
        FeatureCollection fc = io.readFeatureCollection(getClass().getResourceAsStream("/us-states.json"));
        FeatureIterator iterator = fc.features();
        try {
        // fixme geotools still uses pre-location tech jts
        // while (iterator.hasNext()) {
        // Feature feature = iterator.next();
        // 
        // final String name = feature.getProperty("name").getValue().toString();
        // final Double density = (Double) feature.getProperty("density").getValue();
        // System.out.println("State " + name + " read!");
        // Geometry geometry = (Geometry) feature.getDefaultGeometryProperty().getValue();
        // 
        // // Using a helper create v-leaflet components from geojson
        // Collection<LeafletLayer> toLayers = JTSUtil.toLayers(geometry);
        // for (LeafletLayer l : toLayers) {
        // leafletMap.addComponent(l);
        // if (l instanceof AbstractLeafletVector) {
        // configureFeature(l, density, name);
        // } else if (l instanceof LLayerGroup) {
        // LLayerGroup g = (LLayerGroup) l;
        // for (Component component : g) {
        // configureFeature((LeafletLayer) component, density, name);
        // }
        // }
        // }
        // }
        } finally {
            iterator.close();
        }
    } catch (MalformedURLException ex) {
        Logger.getLogger(ChoroplethExample.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ChoroplethExample.class.getName()).log(Level.SEVERE, null, ex);
    }
    /*
         * AbsoluteLayout is a handy layout you can use to place any Vaadin
         * components on top of map. Here we just use raw html label to create
         * a legend, but we could use dynamically generated html or e.g. Table
         * component on top of the map as well.
         */
    AbsoluteLayout absoluteLayout = new AbsoluteLayout();
    absoluteLayout.setWidth("800px");
    absoluteLayout.setHeight("500px");
    absoluteLayout.addComponent(leafletMap);
    Label label = new Label("<style>.legend { background:white; padding:10px; border-radius: 4px; text-align: left; line-height: 18px; color: #555; } .legend i { width: 18px; height: 18px; float: left; margin-right: 8px; opacity: 0.7; }</style><div class=\"info legend leaflet-control\"><i style=\"background:#FFEDA0\"></i> 0–10<br><i style=\"background:#FED976\"></i> 10–20<br><i style=\"background:#FEB24C\"></i> 20–50<br><i style=\"background:#FD8D3C\"></i> 50–100<br><i style=\"background:#FC4E2A\"></i> 100–200<br><i style=\"background:#E31A1C\"></i> 200–500<br><i style=\"background:#BD0026\"></i> 500–1000<br><i style=\"background:#800026\"></i> 1000+</div>", ContentMode.HTML);
    label.setWidth("100px");
    absoluteLayout.addComponent(label, "bottom: 30px; right: 20px;");
    return absoluteLayout;
}
Also used : FeatureIterator(org.geotools.feature.FeatureIterator) FeatureJSON(org.geotools.geojson.feature.FeatureJSON) MalformedURLException(java.net.MalformedURLException) LMap(org.vaadin.addon.leaflet.LMap) LOpenStreetMapLayer(org.vaadin.addon.leaflet.LOpenStreetMapLayer) FeatureCollection(org.geotools.feature.FeatureCollection) Label(com.vaadin.ui.Label) IOException(java.io.IOException) AbsoluteLayout(com.vaadin.ui.AbsoluteLayout)

Aggregations

FeatureCollection (org.geotools.feature.FeatureCollection)14 FeatureIterator (org.geotools.feature.FeatureIterator)7 FeatureJSON (org.geotools.geojson.feature.FeatureJSON)6 IOException (java.io.IOException)4 Feature (org.opengis.feature.Feature)4 File (java.io.File)3 AdaptorFeatureCollection (org.geotools.feature.collection.AdaptorFeatureCollection)3 Geometry (com.vividsolutions.jts.geom.Geometry)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 DataStore (org.geotools.data.DataStore)2 FeatureSource (org.geotools.data.FeatureSource)2 Query (org.geotools.data.Query)2 MemoryDataStore (org.geotools.data.memory.MemoryDataStore)2 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)2 DefaultFeatureCollection (org.geotools.feature.DefaultFeatureCollection)2 Facet (au.org.ala.legend.Facet)1 UserDataDTO (au.org.ala.spatial.dto.UserDataDTO)1