Search in sources :

Example 21 with SelectedArea

use of au.org.emii.portal.menu.SelectedArea in project spatial-portal by AtlasOfLivingAustralia.

the class PhylogeneticDiversityComposer method getSelectedAreas.

public List<SelectedArea> getSelectedAreas() {
    List<SelectedArea> selectedAreas = new ArrayList<SelectedArea>();
    Vbox vboxArea = (Vbox) getFellowIfAny("vboxArea");
    for (Component c : vboxArea.getChildren()) {
        if ((c instanceof Checkbox) && ((Checkbox) c).isChecked()) {
            SelectedArea sa = null;
            String area = ((Checkbox) c).getValue();
            try {
                if (StringConstants.CURRENT.equals(area)) {
                    sa = new SelectedArea(null, getMapComposer().getViewArea());
                } else if (StringConstants.AUSTRALIA.equals(area)) {
                    sa = new SelectedArea(null, CommonData.getSettings().getProperty(CommonData.AUSTRALIA_WKT));
                } else if (StringConstants.WORLD.equals(area)) {
                    sa = new SelectedArea(null, CommonData.WORLD_WKT);
                } else {
                    List<MapLayer> layers = getMapComposer().getPolygonLayers();
                    for (MapLayer ml : layers) {
                        if (area.equals(ml.getName())) {
                            sa = new SelectedArea(ml, null);
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                LOGGER.warn("Unable to retrieve selected area", e);
            }
            if (sa != null) {
                selectedAreas.add(sa);
            }
        }
    }
    //add all areas from a selection
    if (autoCompleteLayerSelection != null && cAreasFromLayer.isChecked()) {
        if (autoCompleteLayerAreas == null) {
            String fieldId = CommonData.getLayerFacetNameDefault(autoCompleteLayerSelection);
            JSONParser jp = new JSONParser();
            JSONObject objJson = null;
            try {
                objJson = (JSONObject) jp.parse(Util.readUrl(CommonData.getLayersServer() + "/field/" + fieldId));
            } catch (ParseException e) {
                LOGGER.error("failed to parse for: " + fieldId);
            }
            JSONArray objects = (JSONArray) objJson.get("objects");
            autoCompleteLayerAreas = new ArrayList();
            for (int i = 0; i < objects.size(); i++) {
                MapLayer ml = createMapLayerForObject((JSONObject) objects.get(i));
                SelectedArea sa = new SelectedArea(ml, null);
                autoCompleteLayerAreas.add(sa);
            }
        }
        selectedAreas.addAll(autoCompleteLayerAreas);
    }
    return selectedAreas;
}
Also used : SelectedArea(au.org.emii.portal.menu.SelectedArea) MapLayer(au.org.emii.portal.menu.MapLayer) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) ParseException(org.json.simple.parser.ParseException) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) Component(org.zkoss.zk.ui.Component)

Example 22 with SelectedArea

use of au.org.emii.portal.menu.SelectedArea in project spatial-portal by AtlasOfLivingAustralia.

the class ToolComposer method getSelectedAreaHighlight.

public SelectedArea getSelectedAreaHighlight() {
    String area = rgAreaHighlight.getSelectedItem().getValue();
    SelectedArea sa = null;
    try {
        if (StringConstants.CURRENT.equals(area)) {
            sa = new SelectedArea(null, getMapComposer().getViewArea());
        } else if (StringConstants.AUSTRALIA.equals(area)) {
            sa = new SelectedArea(null, CommonData.getSettings().getProperty(CommonData.AUSTRALIA_WKT));
        } else if (StringConstants.WORLD.equals(area)) {
            sa = new SelectedArea(null, CommonData.WORLD_WKT);
        } else {
            List<MapLayer> layers = getMapComposer().getPolygonLayers();
            for (MapLayer ml : layers) {
                if (area.equals(ml.getName())) {
                    sa = new SelectedArea(ml, null);
                    break;
                }
            }
        }
    } catch (Exception e) {
        LOGGER.warn("Unable to retrieve selected area", e);
    }
    return sa;
}
Also used : SelectedArea(au.org.emii.portal.menu.SelectedArea) MapLayer(au.org.emii.portal.menu.MapLayer) IOException(java.io.IOException)

Example 23 with SelectedArea

use of au.org.emii.portal.menu.SelectedArea in project spatial-portal by AtlasOfLivingAustralia.

the class AreaReportPDFComposer method onFinish.

@Override
public boolean onFinish() {
    if (pool != null) {
        return true;
    }
    //close any existing area report
    Window w = (Window) getPage().getFellowIfAny("popupResults");
    if (w != null) {
        w.detach();
    }
    SelectedArea sa = getSelectedArea();
    String areaName = getSelectedAreaName();
    String areaDisplayName = getSelectedAreaDisplayName();
    MapLayer ml = getMapComposer().getMapLayer(areaName);
    double[] bbox = null;
    if (ml != null && ml.getMapLayerMetadata().getBbox() != null && ml.getMapLayerMetadata().getBbox().size() == 4) {
        bbox = new double[4];
        bbox[0] = ml.getMapLayerMetadata().getBbox().get(0);
        bbox[1] = ml.getMapLayerMetadata().getBbox().get(1);
        bbox[2] = ml.getMapLayerMetadata().getBbox().get(2);
        bbox[3] = ml.getMapLayerMetadata().getBbox().get(3);
    }
    String wktTmp = (ml == null ? sa.getWkt() : (ml.getFacets() == null ? ml.getWKT() : null));
    if (wktTmp == null && ml != null && ml.getPid() != null) {
        wktTmp = Util.readUrl(CommonData.getLayersServer() + "/shape/wkt/" + ml.getPid());
    }
    final String queryWkt = (ml == null ? sa.getWkt() : (ml.getFacets() == null ? ml.getWKT() : null));
    final String area = areaDisplayName;
    final String wkt = wktTmp;
    final List<Facet> facets = (ml != null && ml.getFacets() != null ? ml.getFacets() : null);
    progress = new ConcurrentHashMap();
    progress.put("label", "Starting");
    progress.put("percent", 0.0);
    Callable pdfAreaReport = new Callable<AreaReportPDF>() {

        @Override
        public AreaReportPDF call() {
            return new AreaReportPDF(wkt, area, facets, queryWkt, progress);
        }
    };
    pool = Executors.newFixedThreadPool(1);
    future = pool.submit(pdfAreaReport);
    getMapComposer().getOpenLayersJavascript().execute("setTimeout('checkProgress()', 2000);");
    divProgress.setVisible(true);
    return true;
}
Also used : AreaReportPDF(au.org.emii.portal.util.AreaReportPDF) SelectedArea(au.org.emii.portal.menu.SelectedArea) MapLayer(au.org.emii.portal.menu.MapLayer) Facet(au.org.ala.legend.Facet)

Example 24 with SelectedArea

use of au.org.emii.portal.menu.SelectedArea in project spatial-portal by AtlasOfLivingAustralia.

the class PointGenerationComposer method onFinish.

@Override
public boolean onFinish() {
    SelectedArea sa = getSelectedArea();
    double[][] bbox = null;
    if (sa.getMapLayer() != null && sa.getMapLayer().getMapLayerMetadata() != null) {
        List<Double> bb = sa.getMapLayer().getMapLayerMetadata().getBbox();
        bbox = new double[2][2];
        bbox[0][0] = bb.get(0);
        bbox[0][1] = bb.get(1);
        bbox[1][0] = bb.get(2);
        bbox[1][1] = bb.get(3);
    } else {
        List<Double> bb = Util.getBoundingBox(sa.getWkt());
        bbox = new double[][] { { bb.get(0), bb.get(1) }, { bb.get(2), bb.get(3) } };
    }
    //with bounding box, cut points
    try {
        String wkt = (sa.getMapLayer() != null ? sa.getMapLayer().getWKT() : sa.getWkt());
        StringBuilder sb = new StringBuilder();
        sb.append("longitude,latitude");
        int count = 0;
        int width = (int) Math.ceil((bbox[1][0] - bbox[0][0]) / resolution.getValue());
        int height = (int) Math.ceil((bbox[1][1] - bbox[0][1]) / resolution.getValue());
        double startx = Math.floor(bbox[0][0] / resolution.getValue()) * resolution.getValue();
        double starty = Math.floor(bbox[0][1] / resolution.getValue()) * resolution.getValue();
        if (wkt == null) {
            if (sa.getFacets() != null) {
                double[][] points = new double[(width + 1) * (height + 1)][2];
                int pos = 0;
                for (int i = 0; i <= width; i++) {
                    for (int j = 0; j <= height; j++) {
                        double lng = startx + i * resolution.getValue();
                        double lat = starty + j * resolution.getValue();
                        points[pos][0] = lng;
                        points[pos][1] = lat;
                        pos = pos + 1;
                    }
                }
                List<String> fields = new ArrayList<String>();
                for (Facet f : sa.getFacets()) {
                    fields.add(f.getFields()[0]);
                }
                List<String[]> result = Sampling.sampling(fields, points);
                for (int i = 0; i < result.size(); i++) {
                    Facet f = sa.getFacets().get(i);
                    String[] intersection = result.get(i);
                    for (int j = 0; j < intersection.length; j++) {
                        if (f.isValid("\"" + intersection[j] + "\"")) {
                            sb.append("\n");
                            sb.append(points[j][0]).append(",").append(points[j][1]);
                            count++;
                        }
                    }
                }
            } else {
                LOGGER.error("invalid area selected for point generation");
                getMapComposer().showMessage("Unsupported area selected for point generation. Choose a different area.");
                return false;
            }
        } else {
            //write temporary shapefile
            File tmp = File.createTempFile("tmp", ".shp");
            ShapefileUtils.saveShapefile(tmp, wkt, "tmp");
            FileDataStore store = FileDataStoreFinder.getDataStore(tmp);
            FeatureSource featureSource = store.getFeatureSource(store.getTypeNames()[0]);
            FeatureCollection featureCollection = featureSource.getFeatures();
            GeometryFactory gf = new GeometryFactory();
            List<Geometry> geometry = new ArrayList<Geometry>();
            FeatureIterator it = featureCollection.features();
            while (it.hasNext()) {
                SimpleFeature feature = (SimpleFeature) it.next();
                geometry.add((Geometry) feature.getDefaultGeometry());
            }
            try {
                it.close();
            } catch (Exception e) {
            }
            for (int i = 0; i <= width; i++) {
                for (int j = 0; j <= height; j++) {
                    double lng = startx + i * resolution.getValue();
                    double lat = starty + j * resolution.getValue();
                    Coordinate c = new Coordinate(lng, lat);
                    Geometry point = gf.createPoint(c);
                    for (Geometry geom : geometry) {
                        if (geom.contains(point)) {
                            sb.append("\n");
                            sb.append(lng).append(",").append(lat);
                            count++;
                        }
                    }
                }
            }
            //close tmp file
            try {
                store.dispose();
            } catch (Exception e) {
            }
            //delete tmp files
            try {
                FileUtils.deleteQuietly(tmp);
                FileUtils.deleteQuietly(new File(tmp.getPath().replace(".shp", ".shx")));
                FileUtils.deleteQuietly(new File(tmp.getPath().replace(".shp", ".fix")));
                FileUtils.deleteQuietly(new File(tmp.getPath().replace(".shp", ".dbf")));
                FileUtils.deleteQuietly(new File(tmp.getPath().replace(".shp", ".prj")));
            } catch (Exception e) {
            }
        }
        if (count <= 0) {
            getMapComposer().showMessage("No points generated. Try again with a smaller resolution or larger area.");
        } else if (count > Integer.parseInt(CommonData.getSettings().getProperty("max_record_count_upload"))) {
            getMapComposer().showMessage(count + " points generated. Maximum is " + CommonData.getSettings().getProperty("generated_points_max") + ".\n" + "Try again with a larger resolution or smaller area.");
        } else {
            String name = tToolName.getValue();
            try {
                UploadSpeciesController.loadUserPoints(new UserDataDTO(name), new StringReader(sb.toString()), true, name, "points in " + getSelectedAreaDisplayName() + " on " + resolution.getValue() + " degree resolution grid.", getMapComposer(), null);
                detach();
                return true;
            } catch (Exception e) {
                LOGGER.error("failed to upload points");
            }
        }
    } catch (Exception e) {
    }
    return false;
}
Also used : GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) SelectedArea(au.org.emii.portal.menu.SelectedArea) ArrayList(java.util.ArrayList) FeatureIterator(org.geotools.feature.FeatureIterator) StringReader(java.io.StringReader) UserDataDTO(au.org.ala.spatial.dto.UserDataDTO) FileDataStore(org.geotools.data.FileDataStore) Facet(au.org.ala.legend.Facet) FeatureSource(org.geotools.data.FeatureSource) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Geometry(com.vividsolutions.jts.geom.Geometry) FeatureCollection(org.geotools.feature.FeatureCollection) Coordinate(com.vividsolutions.jts.geom.Coordinate) File(java.io.File)

Example 25 with SelectedArea

use of au.org.emii.portal.menu.SelectedArea in project spatial-portal by AtlasOfLivingAustralia.

the class AddSpeciesInArea method getSelectedArea.

public SelectedArea getSelectedArea() {
    String area = rAreaSelected.getValue();
    SelectedArea sa = null;
    try {
        if (StringConstants.CURRENT.equals(area)) {
            sa = new SelectedArea(null, getMapComposer().getViewArea());
        } else if (StringConstants.AUSTRALIA.equals(area)) {
            sa = new SelectedArea(null, CommonData.getSettings().getProperty(CommonData.AUSTRALIA_WKT));
        } else if (StringConstants.WORLD.equals(area)) {
            sa = new SelectedArea(null, CommonData.WORLD_WKT);
        } else {
            List<MapLayer> layers = getMapComposer().getPolygonLayers();
            for (MapLayer ml : layers) {
                if (area.equals(ml.getName())) {
                    sa = new SelectedArea(ml, null);
                    break;
                }
            }
            if (sa == null) {
                //for 'all areas'
                sa = new SelectedArea(null, area);
            }
        }
    } catch (Exception e) {
        LOGGER.error("Unable to retrieve selected area", e);
    }
    return sa;
}
Also used : SelectedArea(au.org.emii.portal.menu.SelectedArea) MapLayer(au.org.emii.portal.menu.MapLayer)

Aggregations

SelectedArea (au.org.emii.portal.menu.SelectedArea)30 MapLayer (au.org.emii.portal.menu.MapLayer)14 Facet (au.org.ala.legend.Facet)12 HttpClient (org.apache.commons.httpclient.HttpClient)6 PostMethod (org.apache.commons.httpclient.methods.PostMethod)6 Query (au.org.ala.spatial.util.Query)5 JSONObject (org.json.simple.JSONObject)5 ArrayList (java.util.ArrayList)4 JSONParser (org.json.simple.parser.JSONParser)4 ParseException (org.json.simple.parser.ParseException)4 IOException (java.io.IOException)3 StringReader (java.io.StringReader)3 Event (org.zkoss.zk.ui.event.Event)3 CSVReader (au.com.bytecode.opencsv.CSVReader)2 Geometry (com.vividsolutions.jts.geom.Geometry)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 JSONArray (org.json.simple.JSONArray)2 Component (org.zkoss.zk.ui.Component)2