Search in sources :

Example 41 with ParseException

use of com.vividsolutions.jts.io.ParseException in project spatial-portal by AtlasOfLivingAustralia.

the class AreaPolygon method validWKT.

public boolean validWKT(String wkt) {
    if (wkt.replaceAll(" ", "").isEmpty()) {
        invalidWKT.setValue("WKT is Invalid");
        return false;
    } else {
        invalidWKT.setValue("");
    }
    try {
        WKTReader wktReader = new WKTReader();
        com.vividsolutions.jts.geom.Geometry g = wktReader.read(wkt);
        // NC 20130319: Ensure that the WKT is valid according to the WKT standards.
        IsValidOp op = new IsValidOp(g);
        if (!op.isValid()) {
            // this will fix some issues
            g = g.buffer(0);
            op = new IsValidOp(g);
        }
        if (!op.isValid()) {
            invalidWKT.setValue(CommonData.lang(StringConstants.ERROR_WKT_INVALID) + " " + op.getValidationError().getMessage());
            LOGGER.warn("WKT is invalid." + op.getValidationError().getMessage());
        // TODO Fix invalid WKT text using https://github.com/tudelft-gist/prepair maybe???
        } else if (g.isRectangle()) {
            // NC 20130319: When the shape is a rectangle ensure that the points a specified in the correct order.
            // get the new WKT for the rectangle will possibly need to change the order.
            com.vividsolutions.jts.geom.Envelope envelope = g.getEnvelopeInternal();
            String wkt2 = StringConstants.POLYGON + "((" + envelope.getMinX() + " " + envelope.getMinY() + "," + envelope.getMaxX() + " " + envelope.getMinY() + "," + envelope.getMaxX() + " " + envelope.getMaxY() + "," + envelope.getMinX() + " " + envelope.getMaxY() + "," + envelope.getMinX() + " " + envelope.getMinY() + "))";
            if (!wkt.equals(wkt2)) {
                LOGGER.debug("NEW WKT for Rectangle: " + wkt);
                invalidWKT.setValue(CommonData.lang("error_wkt_rectangle_wrong_order"));
                displayGeom.setValue(wkt2);
                return false;
            }
        }
        return op.isValid();
    } catch (ParseException parseException) {
        invalidWKT.setValue(CommonData.lang(StringConstants.ERROR_WKT_INVALID) + " " + parseException.getMessage());
        return false;
    }
}
Also used : IsValidOp(com.vividsolutions.jts.operation.valid.IsValidOp) ParseException(com.vividsolutions.jts.io.ParseException) WKTReader(com.vividsolutions.jts.io.WKTReader)

Example 42 with ParseException

use of com.vividsolutions.jts.io.ParseException in project spatial-portal by AtlasOfLivingAustralia.

the class AreaUploadShapefile method onUpload$btnFileUpload.

public void onUpload$btnFileUpload(Event event) {
    UploadEvent ue = null;
    if ("onUpload".equals(event.getName())) {
        ue = (UploadEvent) event;
    } else if ("onForward".equals(event.getName())) {
        ue = (UploadEvent) ((ForwardEvent) event).getOrigin();
    }
    if (ue == null) {
        LOGGER.debug("unable to upload file");
        return;
    } else {
        LOGGER.debug("fileUploaded()");
    }
    try {
        Media m = ue.getMedia();
        LOGGER.debug("m.getName(): " + m.getName());
        LOGGER.debug("getContentType: " + m.getContentType());
        LOGGER.debug("getFormat: " + m.getFormat());
        UserDataDTO ud = new UserDataDTO(txtLayerName.getValue());
        ud.setFilename(m.getName());
        byte[] kmldata = getKml(m);
        if (kmldata.length > 0) {
            loadUserLayerKML(m.getName(), kmldata, ud);
        } else if (m.getName().toLowerCase().endsWith("zip")) {
            Map args = new HashMap();
            args.put(StringConstants.LAYERNAME, txtLayerName.getValue());
            args.put(StringConstants.MEDIA, m);
            String windowname = "areashapewizard";
            if (getFellowIfAny(windowname) != null) {
                getFellowIfAny(windowname).detach();
            }
            Window window = (Window) Executions.createComponents("WEB-INF/zul/add/area/AreaUploadShapefileWizard.zul", this.getParent(), args);
            try {
                window.setParent(this.getParent());
                window.doModal();
            } catch (SuspendNotAllowedException e) {
            // we are really closing the window without opening/displaying to the user
            }
        } else if (m.getName().toLowerCase().endsWith("zip_removeme")) {
            Map input = Zipper.unzipFile(m.getName(), m.getStreamData(), "/data/ala/runtime/output/layers/");
            String type = "";
            String file = "";
            if (input.containsKey(StringConstants.TYPE)) {
                type = (String) input.get(StringConstants.TYPE);
            }
            if (input.containsKey(StringConstants.FILE)) {
                file = (String) input.get(StringConstants.FILE);
            }
            if ("shp".equalsIgnoreCase(type)) {
                LOGGER.debug("Uploaded file is a shapefile. Loading...");
                Map shape = ShapefileUtils.loadShapefile(new File(file));
                if (shape != null) {
                    String wkt = (String) shape.get(StringConstants.WKT);
                    LOGGER.debug("Got shapefile wkt...validating");
                    String msg = "";
                    boolean invalid = false;
                    try {
                        WKTReader wktReader = new WKTReader();
                        com.vividsolutions.jts.geom.Geometry g = wktReader.read(wkt);
                        // NC 20130319: Ensure that the WKT is valid according to the WKT standards.
                        IsValidOp op = new IsValidOp(g);
                        if (!op.isValid()) {
                            // this will fix some issues
                            g = g.buffer(0);
                            op = new IsValidOp(g);
                        }
                        if (!op.isValid()) {
                            invalid = true;
                            LOGGER.warn(CommonData.lang(StringConstants.ERROR_WKT_INVALID) + " " + op.getValidationError().getMessage());
                            msg = op.getValidationError().getMessage();
                        // TODO Fix invalid WKT text using https://github.com/tudelft-gist/prepair maybe???
                        } else if (g.isRectangle()) {
                            // NC 20130319: When the shape is a rectangle ensure that the points a specified in the correct order.
                            // get the new WKT for the rectangle will possibly need to change the order.
                            com.vividsolutions.jts.geom.Envelope envelope = g.getEnvelopeInternal();
                            String wkt2 = "POLYGON((" + envelope.getMinX() + " " + envelope.getMinY() + "," + envelope.getMaxX() + " " + envelope.getMinY() + "," + envelope.getMaxX() + " " + envelope.getMaxY() + "," + envelope.getMinX() + " " + envelope.getMaxY() + "," + envelope.getMinX() + " " + envelope.getMinY() + "))";
                            if (!wkt.equals(wkt2)) {
                                LOGGER.debug("NEW WKT for Rectangle: " + wkt);
                                msg = CommonData.lang("error_wkt_anticlockwise");
                                invalid = true;
                            }
                        }
                        if (!invalid) {
                            invalid = !op.isValid();
                        }
                    } catch (ParseException parseException) {
                        LOGGER.error("error testing validity of uploaded shape file wkt", parseException);
                    }
                    if (invalid) {
                        ok = false;
                        getMapComposer().showMessage(CommonData.lang(StringConstants.ERROR_WKT_INVALID) + " " + msg);
                    } else {
                        layerName = getMapComposer().getNextAreaLayerName(txtLayerName.getValue());
                        MapLayer mapLayer = getMapComposer().addWKTLayer(wkt, layerName, txtLayerName.getValue());
                        ud.setUploadedTimeInMs(System.currentTimeMillis());
                        ud.setType("shapefile");
                        String metadata = "";
                        metadata += "User uploaded Shapefile \n";
                        metadata += "Name: " + ud.getName() + " <br />\n";
                        metadata += "Filename: " + ud.getFilename() + " <br />\n";
                        metadata += "Date: " + ud.getDisplayTime() + " <br />\n";
                        mapLayer.getMapLayerMetadata().setMoreInfo(metadata);
                        ok = true;
                    }
                }
            } else {
                LOGGER.debug("Unknown file type. ");
                getMapComposer().showMessage(CommonData.lang("error_unknown_file_type"));
            }
        } else {
            LOGGER.debug("Unknown file type. ");
            getMapComposer().showMessage(CommonData.lang("error_unknown_file_type"));
        }
    } catch (Exception ex) {
        getMapComposer().showMessage(CommonData.lang("error_upload_failed"));
        LOGGER.error("unable to load user area file: ", ex);
    }
}
Also used : Window(org.zkoss.zul.Window) HashMap(java.util.HashMap) MapLayer(au.org.emii.portal.menu.MapLayer) Media(org.zkoss.util.media.Media) WKTReader(com.vividsolutions.jts.io.WKTReader) ParseException(com.vividsolutions.jts.io.ParseException) SuspendNotAllowedException(org.zkoss.zk.ui.SuspendNotAllowedException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) UploadEvent(org.zkoss.zk.ui.event.UploadEvent) IsValidOp(com.vividsolutions.jts.operation.valid.IsValidOp) Geometry(com.vividsolutions.jts.geom.Geometry) UserDataDTO(au.org.ala.spatial.dto.UserDataDTO) ParseException(com.vividsolutions.jts.io.ParseException) SuspendNotAllowedException(org.zkoss.zk.ui.SuspendNotAllowedException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 43 with ParseException

use of com.vividsolutions.jts.io.ParseException in project spatial-portal by AtlasOfLivingAustralia.

the class AreaUploadShapefile method loadUserLayerKML.

public void loadUserLayerKML(String name, byte[] kmldata, UserDataDTO ud) {
    try {
        String id = String.valueOf(System.currentTimeMillis());
        String kmlpath = "/data/ala/runtime/output/layers/" + id + "/";
        File kmlfilepath = new File(kmlpath);
        kmlfilepath.mkdirs();
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(kmlfilepath.getAbsolutePath() + "/" + name)));
        String kmlstr = new String(kmldata);
        out.write(kmlstr);
        out.close();
        MapComposer mc = getMapComposer();
        layerName = (mc.getMapLayer(txtLayerName.getValue()) == null) ? txtLayerName.getValue() : mc.getNextAreaLayerName(txtLayerName.getValue());
        String wkt = getKMLPolygonAsWKT(kmlstr);
        if (wkt != null && wkt.length() == 0) {
            getMapComposer().showMessage("Failed to find the area in this KML file. \r\n\r\n" + "KML file must have a single placemark within no more than 1 folder.");
            return;
        } else if (wkt == null) {
            getMapComposer().showMessage("Failed to parse this KML file. \r\n\r\n" + "Try importing a Shapefile or WKT instead.");
            return;
        }
        boolean invalid = false;
        String msg = "";
        try {
            WKTReader wktReader = new WKTReader();
            com.vividsolutions.jts.geom.Geometry g = wktReader.read(wkt);
            // NC 20130319: Ensure that the WKT is valid according to the WKT standards.
            IsValidOp op = new IsValidOp(g);
            if (!op.isValid()) {
                // this will fix some issues
                g = g.buffer(0);
                op = new IsValidOp(g);
            }
            if (!op.isValid()) {
                invalid = true;
                LOGGER.warn(CommonData.lang(StringConstants.ERROR_WKT_INVALID) + " " + op.getValidationError().getMessage());
                msg = op.getValidationError().getMessage();
            // TODO Fix invalid WKT text using https://github.com/tudelft-gist/prepair maybe???
            } else if (g.isRectangle()) {
                // NC 20130319: When the shape is a rectangle ensure that the points a specified in the correct order.
                // get the new WKT for the rectangle will possibly need to change the order.
                com.vividsolutions.jts.geom.Envelope envelope = g.getEnvelopeInternal();
                String wkt2 = StringConstants.POLYGON + "((" + envelope.getMinX() + " " + envelope.getMinY() + "," + envelope.getMaxX() + " " + envelope.getMinY() + "," + envelope.getMaxX() + " " + envelope.getMaxY() + "," + envelope.getMinX() + " " + envelope.getMaxY() + "," + envelope.getMinX() + " " + envelope.getMinY() + "))";
                if (!wkt.equals(wkt2)) {
                    LOGGER.debug("NEW WKT for Rectangle: " + wkt);
                    msg = CommonData.lang("error_wkt_anticlockwise");
                    invalid = true;
                }
            }
            if (!invalid) {
                invalid = !op.isValid();
            }
        } catch (ParseException parseException) {
            LOGGER.error(CommonData.lang(StringConstants.ERROR_WKT_INVALID), parseException);
        }
        if (invalid) {
            ok = false;
            getMapComposer().showMessage(CommonData.lang(StringConstants.ERROR_WKT_INVALID) + " " + msg);
        } else {
            MapLayer mapLayer = mc.addWKTLayer(wkt, layerName, txtLayerName.getValue());
            ud.setUploadedTimeInMs(Long.parseLong(id));
            ud.setType("kml");
            String metadata = "";
            metadata += "User uploaded KML area \n";
            metadata += "Name: " + ud.getName() + " <br />\n";
            metadata += "Filename: " + ud.getFilename() + " <br />\n";
            metadata += "Date: " + ud.getDisplayTime() + " <br />\n";
            mapLayer.getMapLayerMetadata().setMoreInfo(metadata);
            if (mapLayer == null) {
                LOGGER.debug("The layer " + name + " couldnt be created");
                mc.showMessage(mc.getLanguagePack().getLang("ext_layer_creation_failure"));
            } else {
                ok = true;
                mc.addUserDefinedLayerToMenu(mapLayer, true);
            }
        }
    } catch (IOException e) {
        getMapComposer().showMessage(CommonData.lang("error_upload_failed"));
        LOGGER.debug("unable to load user kml: ", e);
    }
}
Also used : MapLayer(au.org.emii.portal.menu.MapLayer) WKTReader(com.vividsolutions.jts.io.WKTReader) MapComposer(au.org.emii.portal.composer.MapComposer) IsValidOp(com.vividsolutions.jts.operation.valid.IsValidOp) Geometry(com.vividsolutions.jts.geom.Geometry) ParseException(com.vividsolutions.jts.io.ParseException)

Example 44 with ParseException

use of com.vividsolutions.jts.io.ParseException in project spatial-portal by AtlasOfLivingAustralia.

the class AreaMerge method mergeAreas.

void mergeAreas() {
    List<Facet> facets = new ArrayList<Facet>();
    List<Geometry> wkt = new ArrayList<Geometry>();
    WKTReader wktReader = new WKTReader();
    String layerDisplayNames = "";
    for (int i = 0; i < vboxAreas.getChildren().size(); i++) {
        Checkbox cb = (Checkbox) vboxAreas.getChildren().get(i);
        if (cb.isChecked()) {
            MapLayer ml = cb.getValue();
            if (layerDisplayNames.length() > 0) {
                layerDisplayNames += ", ";
            }
            layerDisplayNames += ml.getDisplayName();
            if (ml != null) {
                if (ml.getFacets() != null) {
                    facets.addAll(ml.getFacets());
                }
                try {
                    // get actual WKT when 'envelope' is specified
                    String w = ml.getWKT();
                    if (w.startsWith(StringConstants.ENVELOPE)) {
                        // should only be one pid
                        String pid = w.substring(w.indexOf(',') + 1, w.length() - 1);
                        w = Util.readUrl(CommonData.getLayersServer() + "/shape/wkt/" + pid);
                    }
                    Geometry g = wktReader.read(w);
                    if (g != null) {
                        wkt.add(g);
                    }
                } catch (ParseException e) {
                    LOGGER.error("cannot parse WKT for map layer: " + ml.getDisplayName() + " for WKT: " + ml.getWKT());
                }
            } else {
                String swkt = null;
                if (CommonData.getSettings().getProperty(CommonData.AUSTRALIA_NAME).equalsIgnoreCase(cb.getLabel())) {
                    swkt = CommonData.getSettings().getProperty(CommonData.AUSTRALIA_WKT);
                } else if ("Current Extent".equalsIgnoreCase(cb.getLabel())) {
                    swkt = getMapComposer().getViewArea();
                } else {
                    LOGGER.error("cannot determine what this checked area is: " + cb.getLabel());
                }
                if (swkt != null) {
                    try {
                        Geometry g = wktReader.read(swkt);
                        if (g != null) {
                            wkt.add(g);
                        }
                    } catch (ParseException e) {
                        LOGGER.error("cannot parse WKT for map layer: " + ml.getDisplayName() + " for WKT: " + swkt);
                    }
                }
            }
        }
    }
    // produce single geometry
    Geometry geometry = null;
    if (!wkt.isEmpty()) {
        geometry = wkt.get(0);
        for (int i = 1; i < wkt.size(); i++) {
            geometry = GeometryCombiner.combine(geometry, wkt.get(i));
        }
    }
    String finalWkt = (geometry == null) ? null : geometry.toString();
    MapComposer mc = getMapComposer();
    layerName = (mc.getMapLayer(txtLayerName.getValue()) == null) ? txtLayerName.getValue() : mc.getNextAreaLayerName(txtLayerName.getValue());
    MapLayer mapLayer = mc.addWKTLayer(finalWkt, layerName, txtLayerName.getValue());
    // if possible, use facets instead of WKT with biocache
    if (wkt.size() == facets.size()) {
        // change to a single OR facet.
        // Because all facet areas are single or multiple, ORs just need to OR for joining.
        String fq = facets.get(0).toString();
        for (int i = 1; i < facets.size(); i++) {
            fq += " OR " + facets.get(i).toString();
        }
        List<Facet> array = new ArrayList<Facet>();
        array.add(Facet.parseFacet(fq));
        mapLayer.setFacets(array);
    }
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    java.util.Calendar calendar = java.util.Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    String metadata = "";
    metadata += "Merged WKT layers\nLayers: " + layerDisplayNames + "\n";
    metadata += "Name: " + layerName + " <br />\n";
    metadata += "Date: " + formatter.format(calendar.getTime()) + " <br />\n";
    mapLayer.getMapLayerMetadata().setMoreInfo(metadata);
    // reapply layer name
    getMapComposer().getMapLayer(layerName).setDisplayName(txtLayerName.getValue());
    getMapComposer().redrawLayersList();
    ok = true;
    this.detach();
}
Also used : MapLayer(au.org.emii.portal.menu.MapLayer) ArrayList(java.util.ArrayList) WKTReader(com.vividsolutions.jts.io.WKTReader) Geometry(com.vividsolutions.jts.geom.Geometry) MapComposer(au.org.emii.portal.composer.MapComposer) Checkbox(org.zkoss.zul.Checkbox) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ParseException(com.vividsolutions.jts.io.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Facet(au.org.ala.legend.Facet)

Example 45 with ParseException

use of com.vividsolutions.jts.io.ParseException in project spatial-portal by AtlasOfLivingAustralia.

the class AreaWKT method validWKT.

public boolean validWKT(String wkt) {
    if (wkt.replaceAll(" ", "").isEmpty()) {
        invalidWKT.setValue(CommonData.lang(StringConstants.ERROR_WKT_INVALID));
        return false;
    }
    try {
        WKTReader wktReader = new WKTReader();
        com.vividsolutions.jts.geom.Geometry g = wktReader.read(wkt);
        // NC 20130319: Ensure that the WKT is valid according to the WKT standards.
        IsValidOp op = new IsValidOp(g);
        if (!op.isValid()) {
            // this will fix some issues
            g = g.buffer(0);
            op = new IsValidOp(g);
        }
        if (!op.isValid()) {
            invalidWKT.setValue(CommonData.lang(StringConstants.ERROR_WKT_INVALID) + " " + op.getValidationError().getMessage());
            LOGGER.warn("WKT is invalid." + op.getValidationError().getMessage());
        // TODO Fix invalid WKT text using https://github.com/tudelft-gist/prepair maybe???
        } else if (g.isRectangle()) {
            // NC 20130319: When the shape is a rectangle ensure that the points a specified in the correct order.
            // get the new WKT for the rectangle will possibly need to change the order.
            com.vividsolutions.jts.geom.Envelope envelope = g.getEnvelopeInternal();
            String wkt2 = StringConstants.POLYGON + "((" + envelope.getMinX() + " " + envelope.getMinY() + "," + envelope.getMaxX() + " " + envelope.getMinY() + "," + envelope.getMaxX() + " " + envelope.getMaxY() + "," + envelope.getMinX() + " " + envelope.getMaxY() + "," + envelope.getMinX() + " " + envelope.getMinY() + "))";
            if (!wkt.equals(wkt2)) {
                LOGGER.debug("NEW WKT for Rectangle: " + wkt);
                invalidWKT.setValue(CommonData.lang("error_wkt_rectangle_wrong_order"));
                displayGeom.setValue(wkt2);
                return false;
            }
        }
        return op.isValid();
    } catch (ParseException parseException) {
        invalidWKT.setValue(CommonData.lang(StringConstants.ERROR_WKT_INVALID) + " " + parseException.getMessage());
        return false;
    }
}
Also used : IsValidOp(com.vividsolutions.jts.operation.valid.IsValidOp) ParseException(com.vividsolutions.jts.io.ParseException) WKTReader(com.vividsolutions.jts.io.WKTReader)

Aggregations

ParseException (com.vividsolutions.jts.io.ParseException)64 Geometry (com.vividsolutions.jts.geom.Geometry)28 WKTReader (com.vividsolutions.jts.io.WKTReader)21 ConverterException (ch.ehi.ili2db.converter.ConverterException)17 RyaStatement (org.apache.rya.api.domain.RyaStatement)7 SimpleFeature (org.opengis.feature.simple.SimpleFeature)7 Statement (org.openrdf.model.Statement)7 Wkb2iox (ch.interlis.iox_j.wkb.Wkb2iox)6 IsValidOp (com.vividsolutions.jts.operation.valid.IsValidOp)6 ServiceException (eu.europa.ec.fisheries.uvms.commons.service.exception.ServiceException)6 IoxException (ch.interlis.iox.IoxException)5 MapLayer (au.org.emii.portal.menu.MapLayer)4 WKBReader (com.vividsolutions.jts.io.WKBReader)4 IOException (java.io.IOException)4 Literal (org.openrdf.model.Literal)4 Coordinate (com.vividsolutions.jts.geom.Coordinate)3 Point (com.vividsolutions.jts.geom.Point)3 Polygon (com.vividsolutions.jts.geom.Polygon)3 WKTWriter (com.vividsolutions.jts.io.WKTWriter)3 SQLException (java.sql.SQLException)3