Search in sources :

Example 1 with WKBReader

use of com.vividsolutions.jts.io.WKBReader in project teiid by teiid.

the class GeometryUtils method getGeometry.

public static Geometry getGeometry(InputStream is1, Integer srid, boolean allowEwkb) throws FunctionExecutionException {
    try {
        WKBReader reader = new WKBReader();
        Geometry jtsGeom = reader.read(new InputStreamInStream(is1));
        if (!allowEwkb && (jtsGeom.getSRID() != GeometryType.UNKNOWN_SRID || (jtsGeom.getCoordinate() != null && !Double.isNaN(jtsGeom.getCoordinate().z)))) {
            // $NON-NLS-1$
            throw new FunctionExecutionException(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31160, "EWKB"));
        }
        if (srid != null) {
            jtsGeom.setSRID(srid);
        }
        return jtsGeom;
    } catch (ParseException e) {
        throw new FunctionExecutionException(e);
    } catch (IOException e) {
        throw new FunctionExecutionException(e);
    } finally {
        if (is1 != null) {
            try {
                is1.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : InputStreamInStream(com.vividsolutions.jts.io.InputStreamInStream) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) ParseException(com.vividsolutions.jts.io.ParseException) IOException(java.io.IOException) WKBReader(com.vividsolutions.jts.io.WKBReader)

Example 2 with WKBReader

use of com.vividsolutions.jts.io.WKBReader in project activityinfo by bedatadriven.

the class FormResource method updateGeometry.

@POST
@Path("record/{recordId}/field/{fieldId}/geometry")
public Response updateGeometry(@PathParam("recordId") String recordId, @PathParam("fieldId") String fieldId, byte[] binaryBody) {
    // Parse the Geometry
    WKBReader reader = new WKBReader(new GeometryFactory());
    Geometry geometry;
    try {
        geometry = reader.read(binaryBody);
    } catch (ParseException e) {
        return Response.status(Response.Status.BAD_REQUEST).entity("Could not parse WKB geometry: " + e.getMessage()).build();
    }
    geometry.normalize();
    if (!geometry.isValid()) {
        return Response.status(Response.Status.BAD_REQUEST).entity(geometry.getGeometryType() + " is not valid").build();
    }
    // Check first to see if this form exists
    Optional<FormStorage> storage = backend.getStorage().getForm(formId);
    if (!storage.isPresent()) {
        return Response.status(Response.Status.NOT_FOUND).entity("Form " + formId + " does not exist.").build();
    }
    // Find the field and verify that it's a GeoArea type
    FormField field;
    try {
        field = storage.get().getFormClass().getField(ResourceId.valueOf(fieldId));
    } catch (IllegalArgumentException e) {
        return Response.status(Response.Status.NOT_FOUND).entity("Record " + recordId + " does not exist.").build();
    }
    if (!(field.getType() instanceof GeoAreaType)) {
        return Response.status(Response.Status.BAD_REQUEST).entity("Field " + fieldId + " is not a GeoArea type").build();
    }
    try {
        storage.get().updateGeometry(ResourceId.valueOf(recordId), ResourceId.valueOf(fieldId), geometry);
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Failed to update geometry for record " + recordId, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
    }
    return Response.ok().build();
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) VersionedFormStorage(org.activityinfo.store.spi.VersionedFormStorage) FormStorage(org.activityinfo.store.spi.FormStorage) GeoAreaType(org.activityinfo.model.type.geo.GeoAreaType) ParseException(com.vividsolutions.jts.io.ParseException) WKBReader(com.vividsolutions.jts.io.WKBReader) NotFoundException(com.sun.jersey.api.NotFoundException) SQLException(java.sql.SQLException) InvalidUpdateException(org.activityinfo.store.query.server.InvalidUpdateException) ParseException(com.vividsolutions.jts.io.ParseException)

Example 3 with WKBReader

use of com.vividsolutions.jts.io.WKBReader in project activityinfo by bedatadriven.

the class WkbGeometryProvider method getGeometries.

@Override
@Timed(name = "mapping.fetch_geometry")
public List<AdminGeo> getGeometries(int adminLevelId) {
    try {
        List<AdminGeo> list = Lists.newArrayList();
        DataInputStream in = new DataInputStream(openWkb(adminLevelId));
        WKBReader wkbReader = new WKBReader(geometryFactory);
        int count = in.readInt();
        for (int i = 0; i != count; ++i) {
            int id = in.readInt();
            LOGGER.info("Reading geometry for admin entity " + id);
            Geometry geometry = wkbReader.read(new DataInputInStream(in));
            list.add(new AdminGeo(id, geometry));
        }
        return list;
    } catch (IOException | ParseException e) {
        throw new RuntimeException(e);
    }
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) IOException(java.io.IOException) ParseException(com.vividsolutions.jts.io.ParseException) DataInputStream(java.io.DataInputStream) WKBReader(com.vividsolutions.jts.io.WKBReader) Timed(org.activityinfo.server.util.monitoring.Timed)

Aggregations

ParseException (com.vividsolutions.jts.io.ParseException)3 WKBReader (com.vividsolutions.jts.io.WKBReader)3 Geometry (com.vividsolutions.jts.geom.Geometry)2 IOException (java.io.IOException)2 NotFoundException (com.sun.jersey.api.NotFoundException)1 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)1 InputStreamInStream (com.vividsolutions.jts.io.InputStreamInStream)1 DataInputStream (java.io.DataInputStream)1 SQLException (java.sql.SQLException)1 GeoAreaType (org.activityinfo.model.type.geo.GeoAreaType)1 Timed (org.activityinfo.server.util.monitoring.Timed)1 InvalidUpdateException (org.activityinfo.store.query.server.InvalidUpdateException)1 FormStorage (org.activityinfo.store.spi.FormStorage)1 VersionedFormStorage (org.activityinfo.store.spi.VersionedFormStorage)1 FunctionExecutionException (org.teiid.api.exception.query.FunctionExecutionException)1