Search in sources :

Example 1 with AbstractLeafletVector

use of org.vaadin.addon.leaflet.AbstractLeafletVector 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 AbstractLeafletVector

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

the class ChoroplethExample method configureFeature.

protected void configureFeature(LeafletLayer l, final Double density, final String name) {
    AbstractLeafletVector lPolygon = (AbstractLeafletVector) l;
    lPolygon.setFillColor(getColor(density));
    lPolygon.setColor("white");
    lPolygon.setDashArray("3");
    lPolygon.setWeight(2);
    lPolygon.setFillOpacity(0.7);
    lPolygon.addClickListener(new LeafletClickListener() {

        @Override
        public void onClick(LeafletClickEvent event) {
            // Any server side data access is easy to do here, could for
            // example do a DB query and show more detailed data for the
            // selected feature
            Notification.show("In " + name + ", the population is " + density + "people/mile²");
        }
    });
    lPolygon.setLineCap("");
}
Also used : LeafletClickEvent(org.vaadin.addon.leaflet.LeafletClickEvent) LeafletClickListener(org.vaadin.addon.leaflet.LeafletClickListener) AbstractLeafletVector(org.vaadin.addon.leaflet.AbstractLeafletVector)

Aggregations

AbstractLeafletVector (org.vaadin.addon.leaflet.AbstractLeafletVector)2 AbsoluteLayout (com.vaadin.ui.AbsoluteLayout)1 Component (com.vaadin.ui.Component)1 Label (com.vaadin.ui.Label)1 Geometry (com.vividsolutions.jts.geom.Geometry)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 FeatureCollection (org.geotools.feature.FeatureCollection)1 FeatureIterator (org.geotools.feature.FeatureIterator)1 FeatureJSON (org.geotools.geojson.feature.FeatureJSON)1 Feature (org.opengis.feature.Feature)1 LLayerGroup (org.vaadin.addon.leaflet.LLayerGroup)1 LMap (org.vaadin.addon.leaflet.LMap)1 LOpenStreetMapLayer (org.vaadin.addon.leaflet.LOpenStreetMapLayer)1 LeafletClickEvent (org.vaadin.addon.leaflet.LeafletClickEvent)1 LeafletClickListener (org.vaadin.addon.leaflet.LeafletClickListener)1 LeafletLayer (org.vaadin.addon.leaflet.LeafletLayer)1