org.geolatte.geom.Geometries Maven / Gradle / Ivy
Show all versions of geolatte-geom Show documentation
/*
* 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 - 2014 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;
import org.geolatte.geom.crs.CoordinateReferenceSystem;
import java.util.ArrayList;
import java.util.List;
/**
* A Factory for {@code Geometry}s
*
* This Factory allows generically creating Geometries
*
* @author Karel Maesen, Geovise BVBA
* creation-date: 5/15/14
*/
public class Geometries {
/**
* Creates an empty {@code Point} for a coordinate reference system
*
* @param crs the coordinate reference system for the created {@code Point}
* @param the type of {@code Position}
* @return an empty {@code Point} with the specified coordinate reference system
*/
public static
Point
mkEmptyPoint(CoordinateReferenceSystem
crs) {
return new Point
(crs);
}
/**
* Creates an empty {@code LineString} for a coordinate reference system
*
* @param crs the coordinate reference system for the created {@code LineString}
* @param
the type of {@code Position}
* @return an empty {@code LineString} with the specified coordinate reference system
*/
public static
LineString
mkEmptyLineString(CoordinateReferenceSystem
crs) {
return new LineString<>(crs);
}
/**
* Creates an empty {@code Polygon} for a coordinate reference system
*
* @param crs the coordinate reference system for the created {@code Polygon}
* @param
the type of {@code Position}
* @return an empty {@code Polygon} with the specified coordinate reference system
*/
public static
Polygon
mkEmptyPolygon(CoordinateReferenceSystem
crs) {
return new Polygon
(crs);
}
/**
* Creates an empty {@code MultiPoint} for a coordinate reference system
*
* @param crs the coordinate reference system for the created {@code MultiPoint}
* @param
the type of {@code Position}
* @return an empty {@code MultiPoint} with the specified coordinate reference system
*/
public static
MultiPoint
mkEmptyMultiPoint(CoordinateReferenceSystem
crs) {
return new MultiPoint<>(crs);
}
/**
* Creates an empty {@code MultiLineString} for a coordinate reference system
*
* @param crs the coordinate reference system for the created {@code MultiLineString}
* @param
the type of {@code Position}
* @return an empty {@code MultiLineString} with the specified coordinate reference system
*/
public static
MultiLineString
mkEmptyMultiLineString(CoordinateReferenceSystem
crs) {
return new MultiLineString<>(crs);
}
/**
* Creates an empty {@code MultiPolygon} for a coordinate reference system
*
* @param crs the coordinate reference system for the created {@code MultiPolygon}
* @param
the type of {@code Position}
* @return an empty {@code MultiPolygon} with the specified coordinate reference system
*/
public static
MultiPolygon
mkEmptyMultiPolygon(CoordinateReferenceSystem
crs) {
return new MultiPolygon<>(crs);
}
/**
* Creates an empty {@code GeometryCollection} for a coordinate reference system
*
* @param crs the coordinate reference system for the created {@code GeometryCollection}
* @param
the type of {@code Position}
* @return an empty {@code GeometryCollection} with the specified coordinate reference system
*/
public static
GeometryCollection
mkEmptyGeometryCollection(CoordinateReferenceSystem
crs) {
return new GeometryCollection<>(crs);
}
/**
* Creates an Empty geometry for the specified {@code GeometryType}
* @param gtype the type to create
* @param crs the coordinate reference system
* @param
the type of {@code Position}
* @return an empty {@Geometry} of the specified type and coordinate reference system
*/
public static
Geometry
mkEmptyGeometry(GeometryType gtype, CoordinateReferenceSystem
crs) {
switch(gtype) {
case POINT:
return mkEmptyPoint(crs);
case LINESTRING:
return mkEmptyLineString(crs);
case POLYGON:
return mkEmptyPolygon(crs);
case MULTIPOINT:
return mkEmptyMultiPoint(crs);
case MULTILINESTRING:
return mkEmptyMultiLineString(crs);
case MULTIPOLYGON:
return mkEmptyMultiPolygon(crs);
case GEOMETRYCOLLECTION:
return mkEmptyGeometryCollection(crs);
default:
throw new IllegalArgumentException("Can't create empty geometry for type: " + gtype);
}
}
/**
* Creates a {@code Point} from a Position and coordinate reference system
*
* @param pos the position for the created {@code Point}
* @param crs the coordinate reference system for the created {@code Point}
* @param
the type of {@code Position}
* @return a {@code Point} with the specified position and coordinate reference system
*/
public static
Point
mkPoint(P pos, CoordinateReferenceSystem
crs) {
return new Point
(pos, crs);
}
/**
* Creates a {@code LineString} from a {@code PositionSequence} and coordinate reference system
*
* @param seq the {@code PositionSequence} for the created {@code LineString}
* @param crs the coordinate reference system for the created {@code LineString}
* @param
the type of {@code Position}
* @return a {@code LineString} with the specified positions and coordinate reference system
*/
public static
LineString
mkLineString(PositionSequence
seq, CoordinateReferenceSystem
crs) {
return new LineString
(seq, crs);
}
/**
* Creates a {@code LinearRing} from a {@code PositionSequence} and coordinate reference system
*
* @param seq the {@code PositionSequence} for the created {@code LinearRing}
* @param crs the coordinate reference system for the created {@code LinearRing}
* @param
the type of {@code Position}
* @return a {@code LinearRing} with the specified positions and coordinate reference system
*/
public static
LinearRing
mkLinearRing(PositionSequence
seq, CoordinateReferenceSystem
crs) {
return new LinearRing
(seq, crs);
}
public static
Polygon
mkPolygon(LinearRing
... rings) {
return new Polygon
(rings);
}
public static
Polygon
mkPolygon(List> rings) {
LinearRing[] ringArr = (LinearRing
[]) new LinearRing[rings.size()];
return new Polygon
(rings.toArray(ringArr));
}
public static
GeometryCollection
mkGeometryCollection(Geometry
... geometries) {
return new GeometryCollection<>(geometries);
}
public static
GeometryCollection
mkGeometryCollection(List> geometries) {
Geometry[] geomArr = (Geometry
[]) new Geometry[geometries.size()];
return new GeometryCollection<>(geometries.toArray(geomArr));
}
public static
MultiPoint
mkMultiPoint(Point
... points) {
return new MultiPoint
(points);
}
public static
MultiPoint
mkMultiPoint(List> points) {
Point[] pointArr = new Point[points.size()];
return new MultiPoint
(points.toArray(pointArr));
}
public static
MultiPoint
mkMultiPoint(PositionSequence
positions, CoordinateReferenceSystem
crs) {
final List> points = new ArrayList<>(positions.size());
positions.forEach(p -> points.add( mkPoint(p, crs)) );
return mkMultiPoint(points);
}
public static MultiLineString
mkMultiLineString(List> lineStrings) {
LineString[] lsArr = new LineString[lineStrings.size()];
return new MultiLineString
(lineStrings.toArray(lsArr));
}
public static
MultiLineString
mkMultiLineString(LineString
... linestrings) {
return new MultiLineString
(linestrings);
}
public static
MultiPolygon
mkMultiPolygon(Polygon
... polygons) {
return new MultiPolygon
(polygons);
}
public static
MultiPolygon
mkMultiPolygon(List> polygons) {
Polygon[] pArr = new Polygon[polygons.size()];
return new MultiPolygon
(polygons.toArray(pArr));
}
@SuppressWarnings("unchecked")
public static
Geometry
mkGeometry(Class> geometryClass,
PositionSequence
positions,
CoordinateReferenceSystem
crs) {
if (geometryClass == null) {
throw new IllegalArgumentException("Null argument not allowed");
}
if (Point.class.isAssignableFrom(geometryClass)) {
return new Point
(positions, crs);
}
if (LinearRing.class.isAssignableFrom(geometryClass)) {
return new LinearRing
(positions, crs);
}
if (LineString.class.isAssignableFrom(geometryClass)) {
return new LineString
(positions, crs);
}
throw new IllegalStateException("Unknown Geometry class");
}
@SuppressWarnings("unchecked")
public static
Geometry
mkGeometry(Class> geometryClass, Geometry
... parts) {
if (Polygon.class.isAssignableFrom(geometryClass)) {
return new Polygon((LinearRing
[]) parts);
}
if (MultiLineString.class.isAssignableFrom(geometryClass)) {
return new MultiLineString<>((LineString
[]) parts);
}
if (MultiPoint.class.isAssignableFrom(geometryClass)) {
return new MultiPoint<>((Point
[]) parts);
}
if (MultiPolygon.class.isAssignableFrom(geometryClass)) {
return new MultiPolygon<>((Polygon
[]) parts);
}
if (AbstractGeometryCollection.class.isAssignableFrom(geometryClass)) {
return new GeometryCollection<>((Geometry
[]) parts);
}
throw new IllegalStateException("Unknown Geometry class");
}
public static Geometry mkGeometry(Class> geometryClass, CoordinateReferenceSystem crs) {
if (Polygon.class.isAssignableFrom(geometryClass)) {
return new Polygon(crs);
}
if (MultiLineString.class.isAssignableFrom(geometryClass)) {
return new MultiLineString(crs);
}
if (MultiPoint.class.isAssignableFrom(geometryClass)) {
return new MultiPoint(crs);
}
if (MultiPolygon.class.isAssignableFrom(geometryClass)) {
return new MultiPolygon(crs);
}
if (GeometryCollection.class.isAssignableFrom(geometryClass)) {
return new GeometryCollection(crs);
}
throw new IllegalStateException("Unknown Geometry class");
}
@SuppressWarnings("unchecked")
public static Geometry
mkGeometry(Class> geometryClass, List> parts) {
if (Polygon.class.isAssignableFrom(geometryClass)) {
LinearRing[] rings = (LinearRing
[]) (new LinearRing[parts.size()]);
int i = 0;
for (Geometry
part : parts) {
rings[i++] = (LinearRing)part;
}
return new Polygon(rings);
}
if (MultiLineString.class.isAssignableFrom(geometryClass)) {
LineString
[] ls = (LineString
[]) (new LineString[parts.size()]);
int i = 0;
for (Geometry
part : parts) {
ls[i++] = (LineString)part;
}
return new MultiLineString
(ls);
}
if (MultiPoint.class.isAssignableFrom(geometryClass)) {
Point
[] ps = (Point
[]) (new Point[parts.size()]);
int i = 0;
for (Geometry
part : parts) {
ps[i++] = (Point)part;
}
return new MultiPoint
(ps);
}
if (MultiPolygon.class.isAssignableFrom(geometryClass)) {
Polygon
[] ps = (Polygon
[]) (new Polygon[parts.size()]);
int i = 0;
for (Geometry
part : parts) {
ps[i++] = (Polygon)part;
}
return new MultiPolygon
(ps);
}
if (GeometryCollection.class.isAssignableFrom(geometryClass)) {
return mkGeometryCollection(parts);
}
throw new IllegalStateException("Unknown Geometry class");
}
}