All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.geolatte.geom.codec.Wkt Maven / Gradle / Ivy

Go to download

This geoLatte-geom library offers a geometry model that conforms to the OGC Simple Features for SQL specification.

The newest version!
/*
 * This file is part of the GeoLatte project.
 *
 *     GeoLatte is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     GeoLatte is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 *
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with GeoLatte.  If not, see .
 *
 * Copyright (C) 2010 - 2011 and Ownership of code is shared by:
 * Qmino bvba - Romeinsestraat 18 - 3001 Heverlee  (http://www.qmino.com)
 * Geovise bvba - Generaal Eisenhowerlei 9 - 2140 Antwerpen (http://www.geovise.com)
 */

package org.geolatte.geom.codec;

import org.geolatte.geom.Geometry;
import org.geolatte.geom.Position;
import org.geolatte.geom.crs.CoordinateReferenceSystem;

import java.util.HashMap;
import java.util.Map;

/**
 * Creates encoders/decoders for WKT geometry representations.
 *
 * 

Note that the WktEncoder/WktDecoder instances returned by the factory * methods are not thread-safe.

* * @author Karel Maesen, Geovise BVBA, 2011 */ public class Wkt { public enum Dialect { /** * Implements SFA vs 1.1.0, OGC document 05_126 */ SFA_1_1_0, /** * Implements SFA vs 1.2.1, OGC document 06-103r4 */ SFA_1_2_1, /** * the PostGIS EWKT dialect (versions 1.0 and later). */ POSTGIS_EWKT_1, MYSQL_WKT, HANA_EWKT, DB2_WKT } private static final Dialect DEFAULT_DIALECT = Dialect.POSTGIS_EWKT_1; private static final Map> DECODERS = new HashMap<>(); private static final Map> ENCODERS = new HashMap<>(); static { DECODERS.put(Dialect.SFA_1_1_0, Sfa110WktDecoder.class); DECODERS.put(Dialect.SFA_1_2_1, Sfa121WktDecoder.class); DECODERS.put(Dialect.POSTGIS_EWKT_1, PostgisWktDecoder.class); DECODERS.put(Dialect.MYSQL_WKT, PostgisWktDecoder.class); // use also the PostgisWktDecoder since it can handle everything from Mysql DECODERS.put(Dialect.HANA_EWKT, HANAWktDecoder.class); DECODERS.put(Dialect.DB2_WKT, Db2WktDecoder.class); ENCODERS.put(Dialect.SFA_1_1_0, Sfa110WktEncoder.class); ENCODERS.put(Dialect.SFA_1_2_1, Sfa121WktEncoder.class); ENCODERS.put(Dialect.POSTGIS_EWKT_1, PostgisWktEncoder.class); ENCODERS.put(Dialect.MYSQL_WKT, PostgisWktEncoder.class); // this is temporary, not everything it produces can be understood by MySQL ENCODERS.put(Dialect.HANA_EWKT, HANAWktEncoder.class); ENCODERS.put(Dialect.DB2_WKT, Db2WktEncoder.class); } /** * Decodes the specified WKT String to a Geometry. *

This method uses the default WKT dialect (Postgis v1.5 EWKT)

* * @param wkt the WKT string to decode * @param crs the Coordinate Reference System for the result * @return The decoded Geometry in the specified reference system */ public static

Geometry

fromWkt(String wkt, CoordinateReferenceSystem

crs) { WktDecoder decoder = newDecoder(); return decoder.decode(wkt, crs); } public static Geometry fromWkt(String wkt) { WktDecoder decoder = newDecoder(); return decoder.decode(wkt); } /** * Decodes the specified WKT String to a Geometry. *

This method uses the default WKT dialect (Postgis v1.5 EWKT)

* * @param wkt the WKT string to decode* * @param crs the Coordinate Reference System for the result * @param dialect thw WKT Dialect of the WKT String * @return The decoded Geometry in the specified reference system * @return The decoded Geometry */ public static

Geometry

fromWkt(String wkt, CoordinateReferenceSystem

crs, Dialect dialect) { WktDecoder decoder = newDecoder(dialect); return decoder.decode(wkt, crs); } public static Geometry fromWkt(String wkt, Dialect dialect) { WktDecoder decoder = newDecoder(dialect); return decoder.decode(wkt); } /** * Encodes a Geometry to a WKT representation. *

This method uses the default WKT dialect (Postgis v1.5 EWKT)

* * @param geometry the Geometry to encode * @return the WKT representation of the given geometry */ public static String toWkt(Geometry geometry) { WktEncoder encoder = newEncoder(); return encoder.encode(geometry); } /** * Encodes a Geometry to a WKT representation according to * a specific dialect. * *

This method uses the default WKT dialect (Postgis v1.5 EWKT)

* * @param geometry the Geometry to encode * @return the WKT representation of the given geometry */ public static String toWkt(Geometry geometry, Dialect dialect) { WktEncoder encoder = newEncoder(dialect); return encoder.encode(geometry); } /** * Creates a WktDecoder for the specified WKT Dialect. * * @param dialect the WKT dialect * @return an WktDecoder that supports the specified dialect */ public static WktDecoder newDecoder(Dialect dialect) { Class decoderClass = DECODERS.get(dialect); assert (decoderClass != null) : "A variant declared, but no encoder/decoder registered."; return createInstance(decoderClass); } /** * Creates a WktDecoder for the default dialect (Postgis 1.x EWKT). * * @return an instance of the default {@code WktDecoder} */ public static WktDecoder newDecoder() { return newDecoder(DEFAULT_DIALECT); } /** * Creates a WktEncoder for the specified WKT Dialect. * * @param dialect the WKT dialect * @return an WktEncoder that supports the specified dialect */ public static WktEncoder newEncoder(Dialect dialect) { Class decoderClass = ENCODERS.get(dialect); assert (decoderClass != null) : "A variant declared, but no encoder/decoder registered."; return createInstance(decoderClass); } /** * Creates a WktEncoder for the default dialect (Postgis 1.x EWKT). * * @return an WktEncoder that supports the default dialect */ public static WktEncoder newEncoder() { return newEncoder(DEFAULT_DIALECT); } private static T createInstance(Class codecClass) { if (codecClass == null) { throw new IllegalArgumentException("Null WKT codec class is not allowed."); } try { return codecClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException(e); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy