org.locationtech.jts.io.WKTWriter Java Examples

The following examples show how to use org.locationtech.jts.io.WKTWriter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FunctionAsWKT.java    From GeoTriples with Apache License 2.0 6 votes vote down vote up
@Override
public Object execute(Object argument, QLTerm qlterm)
		throws SAXException, IOException, ParserConfigurationException, FactoryException,
		MalformedGeometryException, ParseException {
	if (qlterm.equals(QLTerm.ROW_CLASS)){
		Object geom = argument;
		if (geom instanceof  String) {
			return "<http://www.opengis.net/def/crs/EPSG/0/" + Config.EPSG_CODE + "> " + geom;
		}
		else if (geom instanceof  Geometry){
			WKTWriter wkt = new WKTWriter();
			return "<http://www.opengis.net/def/crs/EPSG/0/" + Config.EPSG_CODE + "> " +  (wkt.write((Geometry) geom));
		}
	}
	else if (qlterm.equals(QLTerm.SHP_CLASS) && argument instanceof org.gdal.ogr.Geometry) {
		org.gdal.ogr.Geometry gdalgeom=(org.gdal.ogr.Geometry )argument;

		return "<http://www.opengis.net/def/crs/EPSG/0/" + Config.EPSG_CODE + "> " + gdalgeom.ExportToWkt();
	}

	Geometry geometry = computeGeometry(argument, qlterm);
	return GTransormationFunctions.asWKT((Geometry) geometry, CRS.decode("EPSG:" + Config.EPSG_CODE));
}
 
Example #2
Source File: GeoUtils.java    From elasticsearch-plugin-geoshape with MIT License 6 votes vote down vote up
public static String exportWkbTo(BytesRef wkb, InternalGeoShape.OutputFormat output_format, GeoJsonWriter geoJsonWriter)
        throws ParseException {
    switch (output_format) {
        case WKT:
            Geometry geom = new WKBReader().read(wkb.bytes);
            return new WKTWriter().write(geom);
        case WKB:
            return WKBWriter.toHex(wkb.bytes);
        default:
            Geometry geo = new WKBReader().read(wkb.bytes);
            return geoJsonWriter.write(geo);
    }
}
 
Example #3
Source File: ODataFesParserTest.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void setup() {
    this.parser = new ODataFesParser();
    this.geometryFactory = new GeometryFactory(new PrecisionModel(PrecisionModel.FLOATING_SINGLE), 4326);
    this.polygon = this.geometryFactory
            .createPolygon(new Coordinate[] {
                    new Coordinate(-15.46, 77.98), new Coordinate(-93.51, 38.27),
                    new Coordinate(47.10, -1.05), new Coordinate(58.71, 70.61), new Coordinate(-15.46, 77.98)
            });
    this.wktGeometry = new WKTWriter().write(polygon).replaceFirst(" ", "").replaceAll(", ", ",");
}
 
Example #4
Source File: RandomGeometryBuilder.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
public RandomGeometryBuilder() {
    geometryFactory = new GeometryFactory();
    random = new Random(123456789L);
    decimals = 4;
    numPoints = 10;
    numGeometries = 2;
    geometryJson = new GeometryJSON(decimals);
    wktWriter = new WKTWriter();
}
 
Example #5
Source File: GeoUtils.java    From elasticsearch-plugin-geoshape with MIT License 5 votes vote down vote up
public static String exportGeoTo(Geometry geom, InternalGeoShape.OutputFormat outputFormat, GeoJsonWriter geoJsonWriter) {
    switch (outputFormat) {
        case WKT:
            return new WKTWriter().write(geom);
        case WKB:
            return WKBWriter.toHex(new WKBWriter().write(geom));
        default:
            return geoJsonWriter.write(geom);
    }
}
 
Example #6
Source File: GeometryAdapter.java    From geofence with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String marshal(G the_geom) throws ParseException {
    if (the_geom != null) {
        WKTWriter wktWriter = new WKTWriter();
        if (the_geom.getSRID() == 0)
            the_geom.setSRID(4326);

        return wktWriter.write(the_geom);
    } else {
        throw new ParseException("Geometry obj is null.");
    }
}
 
Example #7
Source File: PolygonAdapter.java    From geofence with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String marshal(Polygon the_geom) throws ParseException {
    if (the_geom != null) {
        WKTWriter wktWriter = new WKTWriter();
        if (the_geom.getSRID() == 0)
            the_geom.setSRID(4326);

        return wktWriter.write(the_geom);
    } else {
        throw new ParseException("Polygon obj is null.");
    }
}
 
Example #8
Source File: XMultiPolygonAdapter.java    From geofence with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String marshal(MultiPolygon the_geom) throws ParseException {
    if (the_geom != null) {
        WKTWriter wktWriter = new WKTWriter();
        if (the_geom.getSRID() == 0)
            the_geom.setSRID(4326);

        return wktWriter.write(the_geom);
    } else {
        throw new ParseException("Geometry obj is null.");
    }
}
 
Example #9
Source File: GeoExtensionProcessor.java    From elasticsearch-plugin-geoshape with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public IngestDocument execute(IngestDocument ingestDocument) throws IOException, ParseException {
    List<String> geo_objects_list = getGeoShapeFieldsFromDoc(ingestDocument);
    for (String geoShapeField : geo_objects_list) {

        Object geoShapeObject = ingestDocument.getFieldValue(geoShapeField, Object.class);

        if (geoShapeObject == null) {
            continue;
        }

        ShapeBuilder<?,?, ?> shapeBuilder = getShapeBuilderFromObject(geoShapeObject);

        Shape shape = null;
        try {
            shape = shapeBuilder.buildS4J();
        }
        catch (InvalidShapeException ignored) {}

        if (shape == null && fixedField == null) {
            throw new IllegalArgumentException("unable to parse shape [" + shapeBuilder.toWKT() + "]");
        }

        Geometry geom = new WKTReader().read(shapeBuilder.toWKT());

        // fix shapes if needed
        if (shape == null && fixedField != null) {
            geom = GeoUtils.removeDuplicateCoordinates(geom);
        }

        ingestDocument.removeField(geoShapeField);

        if (keepShape) {
            ingestDocument.setFieldValue(geoShapeField + "." + shapeField, geoShapeObject);
        }

        if (fixedField != null) {
            ingestDocument.setFieldValue(geoShapeField + "." + fixedField, new WKTWriter().write(geom));
        }

        // compute and add extra geo sub-fields
        byte[] wkb = new WKBWriter().write(geom);  // elastic will auto-encode this as b64

        if (hashField != null) ingestDocument.setFieldValue(
                geoShapeField + ".hash", String.valueOf(GeoUtils.getHashFromWKB(new BytesRef(wkb))));
        if (wkbField != null) ingestDocument.setFieldValue(
                geoShapeField + "." + wkbField, wkb);
        if (typeField != null) ingestDocument.setFieldValue(
                geoShapeField + "." + typeField, geom.getGeometryType());
        if (areaField != null) ingestDocument.setFieldValue(
                geoShapeField + "." + areaField, geom.getArea());
        if (centroidField != null) ingestDocument.setFieldValue(
                geoShapeField + "." + centroidField, GeoUtils.getCentroidFromGeom(geom));
        if (bboxField != null) {
            Coordinate[] coords = geom.getEnvelope().getCoordinates();
            if (coords.length >= 4) ingestDocument.setFieldValue(
                    geoShapeField + "." + bboxField,
                    GeoUtils.getBboxFromCoords(coords));
        }
    }
    return ingestDocument;
}
 
Example #10
Source File: PointConverter.java    From snap-desktop with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String format(Object value) {
    Point2D point = (Point2D) value;
    return new WKTWriter().write(geometryFactory.createPoint(new Coordinate(point.getX(), point.getY())));
}
 
Example #11
Source File: ShowGeometryWktAction.java    From snap-desktop with GNU General Public License v3.0 4 votes vote down vote up
private void exportToWkt() {
    SimpleFeatureFigure selectedFeatureFigure = getSimpleFeatureFigure();
    if (selectedFeatureFigure == null) {
        Dialogs.showInformation(DLG_TITLE, "Please select a geometry.", null);
        return;
    }
    SimpleFeature simpleFeature = selectedFeatureFigure.getSimpleFeature();
    CoordinateReferenceSystem sourceCrs = simpleFeature.getDefaultGeometryProperty().getDescriptor().getCoordinateReferenceSystem();
    CoordinateReferenceSystem targetCrs = DefaultGeographicCRS.WGS84;

    Geometry sourceGeom = selectedFeatureFigure.getGeometry();
    Geometry targetGeom;
    try {
        targetGeom = transformGeometry(sourceGeom, sourceCrs, targetCrs);
    } catch (Exception e) {
        Dialogs.showWarning(DLG_TITLE, "Failed to transform geometry to " + targetCrs.getName() + ".\n" +
                                       "Using " + sourceCrs.getName() + " instead.", null);
        targetGeom = sourceGeom;
        targetCrs = sourceCrs;
    }

    WKTWriter wktWriter = new WKTWriter();
    wktWriter.setFormatted(true);
    wktWriter.setMaxCoordinatesPerLine(2);
    wktWriter.setTab(3);
    String wkt = wktWriter.writeFormatted(targetGeom);

    JTextArea textArea = new JTextArea(16, 32);
    textArea.setEditable(false);
    textArea.setText(wkt);
    textArea.selectAll();

    JPanel contentPanel = new JPanel(new BorderLayout(4, 4));
    contentPanel.add(new JLabel("Geometry Well-Known-Text (WKT):"), BorderLayout.NORTH);
    contentPanel.add(new JScrollPane(textArea), BorderLayout.CENTER);
    contentPanel.add(new JLabel("Geometry CRS: " + targetCrs.getName().toString()), BorderLayout.SOUTH);

    ModalDialog modalDialog = new ModalDialog(SnapApp.getDefault().getMainFrame(), DLG_TITLE, ModalDialog.ID_OK, null);
    modalDialog.setContent(contentPanel);
    modalDialog.center();
    modalDialog.show();
}
 
Example #12
Source File: RasterFootprintStatistics.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Override
protected Object resultsValue() {
  return new WKTWriter().write(footprint);
}