Search in sources :

Example 1 with LeafletLayer

use of org.vaadin.addon.leaflet.LeafletLayer 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 {
            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 : MalformedURLException(java.net.MalformedURLException) LOpenStreetMapLayer(org.vaadin.addon.leaflet.LOpenStreetMapLayer) LeafletLayer(org.vaadin.addon.leaflet.LeafletLayer) Label(com.vaadin.ui.Label) IOException(java.io.IOException) Feature(org.opengis.feature.Feature) AbsoluteLayout(com.vaadin.ui.AbsoluteLayout) FeatureIterator(org.geotools.feature.FeatureIterator) Geometry(com.vividsolutions.jts.geom.Geometry) LLayerGroup(org.vaadin.addon.leaflet.LLayerGroup) FeatureJSON(org.geotools.geojson.feature.FeatureJSON) LMap(org.vaadin.addon.leaflet.LMap) FeatureCollection(org.geotools.feature.FeatureCollection) Component(com.vaadin.ui.Component) AbstractLeafletVector(org.vaadin.addon.leaflet.AbstractLeafletVector)

Example 2 with LeafletLayer

use of org.vaadin.addon.leaflet.LeafletLayer 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("http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_040_00_500k.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();
                final String name = feature.getProperty("NAME").getValue().toString();
                System.out.println("State " + name + " read!");
                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 LPolygon) {
                        LPolygon lPolygon = (LPolygon) l;
                        lPolygon.addClickListener(new LeafletClickListener() {

                            @Override
                            public void onClick(LeafletClickEvent event) {
                                Notification.show("That is " + name);
                            }
                        });
                    }
                }
            }
            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 : LeafletClickEvent(org.vaadin.addon.leaflet.LeafletClickEvent) MalformedURLException(java.net.MalformedURLException) LeafletLayer(org.vaadin.addon.leaflet.LeafletLayer) LeafletClickListener(org.vaadin.addon.leaflet.LeafletClickListener) IOException(java.io.IOException) Feature(org.opengis.feature.Feature) URL(java.net.URL) LPolygon(org.vaadin.addon.leaflet.LPolygon) FeatureIterator(org.geotools.feature.FeatureIterator) Geometry(com.vividsolutions.jts.geom.Geometry) FeatureJSON(org.geotools.geojson.feature.FeatureJSON) LMap(org.vaadin.addon.leaflet.LMap) FeatureCollection(org.geotools.feature.FeatureCollection)

Example 3 with LeafletLayer

use of org.vaadin.addon.leaflet.LeafletLayer in project v-leaflet by mstahv.

the class BasicJtsTest method setup.

@Override
protected void setup() {
    super.setup();
    Button button = new Button("Delete first component from map");
    button.addClickListener(new ClickListener() {

        @Override
        public void buttonClick(ClickEvent event) {
            Component next = leafletMap.iterator().next();
            leafletMap.removeComponent(next);
        }
    });
    content.addComponentAsFirst(button);
    button = new Button("Add polyline to map");
    button.addClickListener(new ClickListener() {

        @Override
        public void buttonClick(ClickEvent event) {
            MultiLineString multiLineString = getMultiLineString();
            Collection<LeafletLayer> layers = JTSUtil.toLayers(multiLineString);
            for (LeafletLayer leafletLayer : layers) {
                lfg.addComponent(leafletLayer);
            }
        }
    });
    content.addComponentAsFirst(button);
}
Also used : MultiLineString(com.vividsolutions.jts.geom.MultiLineString) Button(com.vaadin.ui.Button) ClickEvent(com.vaadin.ui.Button.ClickEvent) LeafletLayer(org.vaadin.addon.leaflet.LeafletLayer) Collection(java.util.Collection) Component(com.vaadin.ui.Component) ClickListener(com.vaadin.ui.Button.ClickListener)

Example 4 with LeafletLayer

use of org.vaadin.addon.leaflet.LeafletLayer in project v-leaflet by mstahv.

the class BasicJtsTest method getTestComponent.

public Component getTestComponent() {
    leafletMap = new LMap();
    // Not creating a name -> not added to the
    lfg = new LFeatureGroup();
    // overlay controller
    Polygon poly = getPolygon();
    Collection<LeafletLayer> lPoly = JTSUtil.toLayers(poly);
    lfg.addComponent(lPoly);
    MultiPolygon multiPolygon = getMultiPolygon();
    Collection<LeafletLayer> lMultiPoly = JTSUtil.toLayers(multiPolygon);
    lfg.addComponent(lMultiPoly);
    LineString lineString = getLineString();
    Collection<LeafletLayer> lLine = JTSUtil.toLayers(lineString);
    lfg.addComponent(lLine);
    // MultiLineString multiLineString = getMultiLineString();
    // Collection<LeafletLayer> lMultiLine = JTSUtil.toLayers(multiLineString);
    // lfg.addComponent(lMultiLine);
    MultiPoint multiPoint = getMultiPoint();
    Collection<LeafletLayer> lMultiPoint = JTSUtil.toLayers(multiPoint);
    lfg.addComponent(lMultiPoint);
    Point point = getPoint();
    Collection<LeafletLayer> lPoint = JTSUtil.toLayers(point);
    lfg.addComponent(lPoint);
    leafletMap.setZoomLevel(5);
    leafletMap.setCenter(multiPolygon.getCentroid());
    leafletMap.addComponent(lfg);
    return leafletMap;
}
Also used : MultiPoint(com.vividsolutions.jts.geom.MultiPoint) LMap(org.vaadin.addon.leaflet.LMap) LFeatureGroup(org.vaadin.addon.leaflet.LFeatureGroup) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) LeafletLayer(org.vaadin.addon.leaflet.LeafletLayer) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon)

Example 5 with LeafletLayer

use of org.vaadin.addon.leaflet.LeafletLayer in project v-leaflet by mstahv.

the class PolygonWithHolesTest method getTestComponent.

public Component getTestComponent() {
    leafletMap = new LMap();
    // Not creating a name -> not added to the
    lfg = new LFeatureGroup();
    // overlay controller
    LPolygon polygon = new LPolygon();
    polygon.setPoints(new Point[] { new Point(0, 0), new Point(30, 30), new Point(30, 0) });
    polygon.setHoles(new Point[] { new Point(20, 20), new Point(25, 25), new Point(25, 20) });
    // non complete hole
    polygon.setHoles(new Point[] { new Point(5, 10), new Point(15, 15), new Point(15, 10) });
    polygon.setColor("green");
    lfg.addComponent(polygon);
    Polygon poly = getPolygon();
    Collection<LeafletLayer> lPoly = JTSUtil.toLayers(poly);
    lfg.addComponent(lPoly);
    leafletMap.setZoomLevel(5);
    leafletMap.addComponent(lfg);
    leafletMap.zoomToContent();
    return leafletMap;
}
Also used : LMap(org.vaadin.addon.leaflet.LMap) LFeatureGroup(org.vaadin.addon.leaflet.LFeatureGroup) LeafletLayer(org.vaadin.addon.leaflet.LeafletLayer) Point(org.vaadin.addon.leaflet.shared.Point) LPolygon(org.vaadin.addon.leaflet.LPolygon) Polygon(com.vividsolutions.jts.geom.Polygon) LPolygon(org.vaadin.addon.leaflet.LPolygon)

Aggregations

LeafletLayer (org.vaadin.addon.leaflet.LeafletLayer)5 LMap (org.vaadin.addon.leaflet.LMap)4 Component (com.vaadin.ui.Component)2 Geometry (com.vividsolutions.jts.geom.Geometry)2 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)2 Polygon (com.vividsolutions.jts.geom.Polygon)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 FeatureCollection (org.geotools.feature.FeatureCollection)2 FeatureIterator (org.geotools.feature.FeatureIterator)2 FeatureJSON (org.geotools.geojson.feature.FeatureJSON)2 Feature (org.opengis.feature.Feature)2 LFeatureGroup (org.vaadin.addon.leaflet.LFeatureGroup)2 LPolygon (org.vaadin.addon.leaflet.LPolygon)2 AbsoluteLayout (com.vaadin.ui.AbsoluteLayout)1 Button (com.vaadin.ui.Button)1 ClickEvent (com.vaadin.ui.Button.ClickEvent)1 ClickListener (com.vaadin.ui.Button.ClickListener)1 Label (com.vaadin.ui.Label)1 LineString (com.vividsolutions.jts.geom.LineString)1