Java源码示例:com.vividsolutions.jts.geom.CoordinateSequence
示例1
/**
* 生成KPolygon
* @param lineString
* @return
*/
public KPolygon makeKPolygonFrom(com.vividsolutions.jts.geom.LineString lineString) {
CoordinateSequence coordinateSequence = lineString.getCoordinateSequence();
ArrayList<Vector3> points = new ArrayList<>();
// The loop stops at the second-last coord since the last coord will be
// the same as the start coord.
Vector3 lastAddedPoint = null;
for (int i = 0; i < coordinateSequence.size() - 1; i++) {
Coordinate coord = coordinateSequence.getCoordinate(i);
Vector3 p = new Vector3((float)coord.x, (float)coord.z, (float)coord.y);
if (lastAddedPoint != null && p.x == lastAddedPoint.x && p.z == lastAddedPoint.z) {
// Don't add the point since it's the same as the last one
continue;
} else {
points.add(p);
lastAddedPoint = p;
}
}
if (points.size() < 3) {
return null;
}
KPolygon polygon = new KPolygon(points);
return polygon;
}
示例2
public static IShape buildTriangle(final double base, final double height, final ILocation location) {
final Coordinate[] points = new Coordinate[4];
final double z = location == null ? 0.0 : location.getZ();
points[0] = new GamaPoint(-base / 2.0, height / 2, z);
points[1] = new GamaPoint(0, -height / 2, z);
points[2] = new GamaPoint(base / 2.0, height / 2, z);
points[3] = points[0];
final CoordinateSequenceFactory fact = GamaGeometryFactory.COORDINATES_FACTORY;
final CoordinateSequence cs = fact.create(points);
final LinearRing geom = GeometryUtils.GEOMETRY_FACTORY.createLinearRing(cs);
final Polygon p = GeometryUtils.GEOMETRY_FACTORY.createPolygon(geom, null);
final IShape s = new GamaShape(p);
if (location != null) {
s.setLocation(location);
}
return s;
}
示例3
public static IShape buildTriangle(final double side_size, final ILocation location) {
final double h = Math.sqrt(3) / 2 * side_size;
final Coordinate[] points = new Coordinate[4];
final double x = location == null ? 0 : location.getX();
final double y = location == null ? 0 : location.getY();
final double z = location == null ? 0 : location.getZ();
points[0] = new GamaPoint(x - side_size / 2.0, y + h / 3, z);
points[1] = new GamaPoint(x, y - 2 * h / 3, z);
points[2] = new GamaPoint(x + side_size / 2.0, y + h / 3, z);
points[3] = points[0];
final CoordinateSequenceFactory fact = GamaGeometryFactory.COORDINATES_FACTORY;
final CoordinateSequence cs = fact.create(points);
final LinearRing geom = GeometryUtils.GEOMETRY_FACTORY.createLinearRing(cs);
final Polygon p = GeometryUtils.GEOMETRY_FACTORY.createPolygon(geom, null);
return new GamaShape(p);
}
示例4
private Geometry createMultiPoint(Map<String, Object> geometryMap,
GeometryFactory geometryFactory) throws ParseException {
Geometry result = null;
try {
@SuppressWarnings("unchecked")
List<List<Number>> coordinatesList = (List<List<Number>>) geometryMap
.get(GeoJsonConstants.NAME_COORDINATES);
CoordinateSequence coordinates = this
.createCoordinateSequence(coordinatesList);
result = geometryFactory.createMultiPoint(coordinates);
} catch (RuntimeException e) {
throw new ParseException(
"Could not parse MultiPoint from GeoJson string.", e);
}
return result;
}
示例5
/**
* Creates facet sequences
*
* @param g
* @return List<GeometryFacetSequence>
*/
private static List computeFacetSequences(Geometry g) {
final List sections = new ArrayList();
g.apply(new GeometryComponentFilter() {
public void filter(Geometry geom) {
CoordinateSequence seq = null;
if (geom instanceof LineString) {
seq = ((LineString) geom).getCoordinateSequence();
addFacetSequences(seq, sections);
}
else if (geom instanceof Point) {
seq = ((Point) geom).getCoordinateSequence();
addFacetSequences(seq, sections);
}
}
});
return sections;
}
示例6
/**
* Computes an average normal vector from a list of polygon coordinates.
* Uses Newell's method, which is based
* on the fact that the vector with components
* equal to the areas of the projection of the polygon onto
* the Cartesian axis planes is normal.
*
* @param seq the sequence of coordinates for the polygon
* @return a normal vector
*/
private Vector3D averageNormal(CoordinateSequence seq)
{
int n = seq.size();
Coordinate sum = new Coordinate(0,0,0);
Coordinate p1 = new Coordinate(0,0,0);
Coordinate p2 = new Coordinate(0,0,0);
for (int i = 0; i < n - 1; i++) {
seq.getCoordinate(i, p1);
seq.getCoordinate(i+1, p2);
sum.x += (p1.y - p2.y)*(p1.z + p2.z);
sum.y += (p1.z - p2.z)*(p1.x + p2.x);
sum.z += (p1.x - p2.x)*(p1.y + p2.y);
}
sum.x /= n;
sum.y /= n;
sum.z /= n;
Vector3D norm = Vector3D.create(sum).normalize();
return norm;
}
示例7
private void add(LineString lineString) {
if (factory == null) {
this.factory = lineString.getFactory();
}
CoordinateSequence seq = lineString.getCoordinateSequence();
boolean doneStart = false;
for (int i = 1; i < seq.size(); i++) {
DissolveHalfEdge e = (DissolveHalfEdge) graph.addEdge(seq.getCoordinate(i-1), seq.getCoordinate(i));
// skip zero-length edges
if (e == null) continue;
/**
* Record source initial segments, so that they can be reflected in output when needed
* (i.e. during formation of isolated rings)
*/
if (! doneStart) {
e.setStart();
doneStart = true;
}
}
}
示例8
private void writeCoordinate(CoordinateSequence seq, int index, OutStream os)
throws IOException
{
ByteOrderValues.putDouble(seq.getX(index), buf, byteOrder);
os.write(buf, 8);
ByteOrderValues.putDouble(seq.getY(index), buf, byteOrder);
os.write(buf, 8);
// only write 3rd dim if caller has requested it for this writer
if (outputDimension >= 3) {
// if 3rd dim is requested, only write it if the CoordinateSequence provides it
double ordVal = Coordinate.NULL_ORDINATE;
if (seq.getDimension() >= 3)
ordVal = seq.getOrdinate(index, 2);
ByteOrderValues.putDouble(ordVal, buf, byteOrder);
os.write(buf, 8);
}
}
示例9
/**
* Generates the WKT for a <tt>LINESTRING</tt>
* specified by a {@link CoordinateSequence}.
*
* @param seq the sequence to write
*
* @return the WKT string
*/
public static String toLineString(CoordinateSequence seq)
{
StringBuffer buf = new StringBuffer();
buf.append("LINESTRING ");
if (seq.size() == 0)
buf.append(" EMPTY");
else {
buf.append("(");
for (int i = 0; i < seq.size(); i++) {
if (i > 0)
buf.append(", ");
buf.append(seq.getX(i) + " " + seq.getY(i));
}
buf.append(")");
}
return buf.toString();
}
示例10
@Override
public void apply(final CoordinateSequenceFilter filter) {
final CoordinateSequence points = getCoordinateSequence();
filter.filter(points, 0);
if (filter.isDone()) { return; }
filter.filter(points, 1);
if (filter.isGeometryChanged()) {
geometryChanged();
}
}
示例11
public Polygon buildRectangle(final Coordinate[] points) {
final CoordinateSequenceFactory fact = GamaGeometryFactory.COORDINATES_FACTORY;
final CoordinateSequence cs = fact.create(points);
final LinearRing geom = GeometryUtils.GEOMETRY_FACTORY.createLinearRing(cs);
final Polygon p = GeometryUtils.GEOMETRY_FACTORY.createPolygon(geom, null);
return p;
}
示例12
/**
* Method create()
*
* @see com.vividsolutions.jts.geom.CoordinateSequenceFactory#create(com.vividsolutions.jts.geom.CoordinateSequence)
*/
@Override
public ICoordinates create(final CoordinateSequence coordSeq) {
if (coordSeq.size() == 1) { return new UniqueCoordinateSequence(coordSeq.getCoordinate(0)); }
if (coordSeq instanceof GamaCoordinateSequence) { return ((GamaCoordinateSequence) coordSeq).clone(); }
return new GamaCoordinateSequence(coordSeq.toCoordinateArray());
}
示例13
private Geometry createPolygon(Map<String, Object> geometryMap,
GeometryFactory geometryFactory) throws ParseException {
Geometry result = null;
try {
@SuppressWarnings("unchecked")
List<List<List<Number>>> ringsList = (List<List<List<Number>>>) geometryMap
.get(GeoJsonConstants.NAME_COORDINATES);
List<CoordinateSequence> rings = new ArrayList<CoordinateSequence>();
for (List<List<Number>> coordinates : ringsList) {
rings.add(createCoordinateSequence(coordinates));
}
if (rings.isEmpty()) {
throw new IllegalArgumentException("Polygon specified with no rings.");
}
LinearRing outer = geometryFactory.createLinearRing(rings.get(0));
LinearRing[] inner = null;
if (rings.size() > 1) {
inner = new LinearRing[rings.size() - 1];
for (int i = 1; i < rings.size(); i++) {
inner[i - 1] = geometryFactory.createLinearRing(rings.get(i));
}
}
result = geometryFactory.createPolygon(outer, inner);
} catch (RuntimeException e) {
throw new ParseException("Could not parse Polygon from GeoJson string.",
e);
}
return result;
}
示例14
private CoordinateSequence createCoordinate(List<Number> ordinates) {
CoordinateSequence result = new CoordinateArraySequence(1);
if (ordinates.size() > 0) {
result.setOrdinate(0, 0, ordinates.get(0).doubleValue());
}
if (ordinates.size() > 1) {
result.setOrdinate(0, 1, ordinates.get(1).doubleValue());
}
if (ordinates.size() > 2) {
result.setOrdinate(0, 2, ordinates.get(2).doubleValue());
}
return result;
}
示例15
private String getJsonString(CoordinateSequence coordinateSequence) {
StringBuffer result = new StringBuffer();
if (coordinateSequence.size() > 1) {
result.append("[");
}
for (int i = 0; i < coordinateSequence.size(); i++) {
if (i > 0) {
result.append(",");
}
result.append("[");
result.append(formatOrdinate(coordinateSequence.getOrdinate(i, CoordinateSequence.X)));
result.append(",");
result.append(formatOrdinate(coordinateSequence.getOrdinate(i, CoordinateSequence.Y)));
if (coordinateSequence.getDimension() > 2 ) {
double z = coordinateSequence.getOrdinate(i, CoordinateSequence.Z);
if (! Double.isNaN(z)) {
result.append(",");
result.append(formatOrdinate(z));
}
}
result.append("]");
}
if (coordinateSequence.size() > 1) {
result.append("]");
}
return result.toString();
}
示例16
/**
* Gets the snapped coordinate array for an atomic geometry,
* or null if it has collapsed.
*
* @return the snapped coordinate array for this geometry
* @return null if the snapped coordinates have collapsed, or are missing
*/
public CoordinateSequence edit(CoordinateSequence coordSeq, Geometry geometry, GeometryFactory targetFactory) {
if (geometryLinesMap.containsKey(geometry)) {
Coordinate[] pts = (Coordinate[]) geometryLinesMap.get(geometry);
// Assert: pts should always have length > 0
boolean isValidPts = isValidSize(pts, geometry);
if (! isValidPts) return null;
return targetFactory.getCoordinateSequenceFactory().create(pts);
}
//TODO: should this return null if no matching snapped line is found
// probably should never reach here?
return coordSeq;
}
示例17
/**
* Reads a {@link Geometry} from SDO_GEOMETRY attributes.
*
* @param oraGeom the Oracle geometry to read
* @return the Geometry read
* @throws IllegalArgumentException when an encoding error or unsupported geometry type is found
*/
Geometry read(OraGeom oraGeom) {
int ordDim = oraGeom.ordDim();
if (ordDim < 2) {
throw new IllegalArgumentException("Dimension D = " + ordDim + " is not supported by JTS. " +
"Either specify a valid dimension or use Oracle Locator Version 9i or later");
}
// read from SDO_POINT_TYPE, if that carries the primary geometry data
if (oraGeom.isCompactPoint()) {
CoordinateSequence ptCoord = extractCoords(oraGeom, oraGeom.point);
return createPoint(ptCoord);
}
CoordinateSequence coords = null;
switch (oraGeom.geomType()) {
case OraGeom.GEOM_TYPE.POINT:
return readPoint(oraGeom, 0);
case OraGeom.GEOM_TYPE.LINE:
return readLine(oraGeom, 0);
case OraGeom.GEOM_TYPE.POLYGON:
return readPolygon(oraGeom, 0);
case OraGeom.GEOM_TYPE.MULTIPOINT:
return readMultiPoint(oraGeom, 0);
case OraGeom.GEOM_TYPE.MULTILINE:
return readMultiLine(oraGeom);
case OraGeom.GEOM_TYPE.MULTIPOLYGON:
return readMultiPolygon(oraGeom);
case OraGeom.GEOM_TYPE.COLLECTION:
return readCollection(oraGeom);
default:
throw new IllegalArgumentException("GTYPE " + oraGeom.gType + " is not supported");
}
}
示例18
/**
* Create MultiPoint as encoded by elemInfo.
*
* @param oraGeom SDO_GEOMETRY attributes being read
* @param elemIndex the element being read
* @param coords the coordinates of the entire geometry
* @return MultiPoint
*/
private MultiPoint readMultiPoint(OraGeom oraGeom, int elemIndex)
{
CoordinateSequence seq;
/**
* Special handling when GTYPE is MULTIPOINT.
* In this case all ordinates are read as a single MultiPoint, regardless of elemInfo contents.
* This is because MultiPoints can be encoded as either a single MULTI elemInfo,
* or as multiple POINT elemInfos
*/
if (oraGeom.geomType() == OraGeom.GEOM_TYPE.MULTIPOINT) {
seq = extractCoords(oraGeom, oraGeom.ordinates);
}
else {
int etype = oraGeom.eType(elemIndex);
int interpretation = oraGeom.interpretation(elemIndex);
checkOrdinates(oraGeom, elemIndex, "MultiPoint");
checkETYPE(etype, OraGeom.ETYPE.POINT, "MultiPoint");
// MultiPoints have a unique interpretation code
if (! (interpretation >= 1)){
errorInterpretation(interpretation, "MultiPoint");
}
seq = extractCoords(oraGeom, elemIndex);
}
MultiPoint points = geometryFactory.createMultiPoint(seq);
return points;
}
示例19
/**
* Create LinearRing for exterior/interior polygon ELEM_INFO triplets.
*
* @param oraGeom SDO_GEOMETRY attributes being read
* @param elemIndex the element being read
* @param coords the coordinates of the entire geometry
* @return LinearRing
*
* @throws IllegalArgumentException If circle, or curve is requested
*/
private LinearRing readLinearRing(OraGeom oraGeom, int elemIndex)
{
int etype = oraGeom.eType(elemIndex);
int interpretation = oraGeom.interpretation(elemIndex);
checkOrdinates(oraGeom, elemIndex, "Polygon");
checkETYPE(etype,OraGeom.ETYPE.POLYGON, OraGeom.ETYPE.POLYGON_EXTERIOR, OraGeom.ETYPE.POLYGON_INTERIOR, "Polygon");
checkInterpretation(interpretation, OraGeom.INTERP.POLYGON, OraGeom.INTERP.RECTANGLE, "Polygon");
CoordinateSequence seq = extractCoords(oraGeom, elemIndex);
LinearRing ring;
if (interpretation == OraGeom.INTERP.POLYGON) {
ring = geometryFactory.createLinearRing(seq);
}
else {
// interpretation == OraSDO.INTERP.RECTANGLE
// rectangle does not maintain measures
Coordinate min = seq.getCoordinate(0);
Coordinate max = seq.getCoordinate(1);
ring = geometryFactory.createLinearRing(new Coordinate[] {
min, new Coordinate(max.x, min.y),
max, new Coordinate(min.x, max.y),
min
});
}
return ring;
}
示例20
/**
* Create Point as encoded.
*
* @param oraGeom SDO_GEOMETRY attributes being read
* @param elemIndex the element being read
* @param coords the coordinates of the entire geometry
* @return Point
*/
private Point readPoint(OraGeom oraGeom, int elemIndex) {
int etype = oraGeom.eType(elemIndex);
int interpretation = oraGeom.interpretation(elemIndex);
checkOrdinates(oraGeom, elemIndex, "Point");
checkETYPE(etype,OraGeom.ETYPE.POINT, "Point");
checkInterpretation(interpretation, OraGeom.INTERP.POINT, "Point");
CoordinateSequence seq = extractCoords(oraGeom, elemIndex);
return createPoint(seq);
}
示例21
private CoordinateSequence extractCoords(OraGeom oraGeom, double[] ordinates, int start, int end)
{
CoordinateSequenceFactory csFactory = geometryFactory.getCoordinateSequenceFactory();
// handle empty case
if ((ordinates == null) || (ordinates.length == 0)) {
return csFactory.create(new Coordinate[0]);
}
int ordDim = oraGeom.ordDim();
/**
* The dimension created matches the input dim, unless it is explicitly set,
* and unless the CoordinateSequence impl limits the dimension.
*/
int csDim = ordDim;
if(outputDimension != OraGeom.NULL_DIMENSION){
csDim = outputDimension;
}
int nCoord = (ordDim == 0 ? 0 : (end - start) / ordDim);
CoordinateSequence cs = csFactory.create(nCoord, csDim);
int actualCSDim = cs.getDimension();
int readDim = Math.min(actualCSDim, ordDim);
for (int iCoord = 0; iCoord < nCoord; iCoord++) {
for (int iDim = 0; iDim < readDim; iDim++) {
int ordIndex = start + iCoord * ordDim + iDim - 1;
// TODO: be more lenient in handling invalid ordinates length
cs.setOrdinate(iCoord, iDim, ordinates[ordIndex]);
}
}
return cs;
}
示例22
private static void addFacetSequences(CoordinateSequence pts, List sections) {
int i = 0;
int size = pts.size();
while (i <= size - 1) {
int end = i + FACET_SEQUENCE_SIZE + 1;
// if only one point remains after this section, include it in this
// section
if (end >= size - 1)
end = size;
FacetSequence sect = new FacetSequence(pts, i, end);
sections.add(sect);
i = i + FACET_SEQUENCE_SIZE;
}
}
示例23
/**
* Creates a wrapper projecting to the XY plane.
*
* @param seq the sequence to be projected
* @return a sequence which projects coordinates
*/
public static CoordinateSequence projectToXY(CoordinateSequence seq)
{
/**
* This is just a no-op, but return a wrapper
* to allow better testing
*/
return new AxisPlaneCoordinateSequence(seq, XY_INDEX);
}
示例24
/**
* Computes a point which is the average of all coordinates
* in a sequence.
* If the sequence lies in a single plane,
* the computed point also lies in the plane.
*
* @param seq a coordinate sequence
* @return a Coordinate with averaged ordinates
*/
private Coordinate averagePoint(CoordinateSequence seq) {
Coordinate a = new Coordinate(0,0,0);
int n = seq.size();
for (int i = 0; i < n; i++) {
a.x += seq.getOrdinate(i, CoordinateSequence.X);
a.y += seq.getOrdinate(i, CoordinateSequence.Y);
a.z += seq.getOrdinate(i, CoordinateSequence.Z);
}
a.x /= n;
a.y /= n;
a.z /= n;
return a;
}
示例25
private static CoordinateSequence project(CoordinateSequence seq, int facingPlane)
{
switch (facingPlane) {
case Plane3D.XY_PLANE: return AxisPlaneCoordinateSequence.projectToXY(seq);
case Plane3D.XZ_PLANE: return AxisPlaneCoordinateSequence.projectToXZ(seq);
default: return AxisPlaneCoordinateSequence.projectToYZ(seq);
}
}
示例26
public void filter(CoordinateSequence seq, int i) {
// compare to vertex
checkVertexDistance(seq.getCoordinate(i));
// compare to segment, if this is one
if (i > 0) {
checkSegmentDistance(seq.getCoordinate(i - 1), seq.getCoordinate(i));
}
}
示例27
/**
* Computes the signed area for a ring. The signed area is:
* <ul>
* <li>positive if the ring is oriented CW
* <li>negative if the ring is oriented CCW
* <li>zero if the ring is degenerate or flat
* </ul>
*
* @param ring
* the coordinates forming the ring
* @return the signed area of the ring
*/
public static double signedArea(CoordinateSequence ring)
{
int n = ring.size();
if (n < 3)
return 0.0;
/**
* Based on the Shoelace formula.
* http://en.wikipedia.org/wiki/Shoelace_formula
*/
Coordinate p0 = new Coordinate();
Coordinate p1 = new Coordinate();
Coordinate p2 = new Coordinate();
ring.getCoordinate(0, p1);
ring.getCoordinate(1, p2);
double x0 = p1.x;
p2.x -= x0;
double sum = 0.0;
for (int i = 1; i < n - 1; i++) {
p0.y = p1.y;
p1.x = p2.x;
p1.y = p2.y;
ring.getCoordinate(i + 1, p2);
p2.x -= x0;
sum += p1.x * (p0.y - p2.y);
}
return sum / 2.0;
}
示例28
/**
* Computes the length of a linestring specified by a sequence of points.
*
* @param pts
* the points specifying the linestring
* @return the length of the linestring
*/
public static double length(CoordinateSequence pts)
{
// optimized for processing CoordinateSequences
int n = pts.size();
if (n <= 1)
return 0.0;
double len = 0.0;
Coordinate p = new Coordinate();
pts.getCoordinate(0, p);
double x0 = p.x;
double y0 = p.y;
for (int i = 1; i < n; i++) {
pts.getCoordinate(i, p);
double x1 = p.x;
double y1 = p.y;
double dx = x1 - x0;
double dy = y1 - y0;
len += Math.sqrt(dx * dx + dy * dy);
x0 = x1;
y0 = y1;
}
return len;
}
示例29
private void writeCoordinateSequence(CoordinateSequence seq, boolean writeSize, OutStream os)
throws IOException
{
if (writeSize)
writeInt(seq.size(), os);
for (int i = 0; i < seq.size(); i++) {
writeCoordinate(seq, i, os);
}
}
示例30
public void testCopy() {
CoordinateSequence s1 = CoordinateArraySequenceFactory.instance().create(
new Coordinate[] { new Coordinate(1, 2), new Coordinate(3, 4)});
CoordinateSequence s2 = CoordinateSequences.copy(s1);
assertTrue(s1.getCoordinate(0).equals(s2.getCoordinate(0)));
assertTrue(s1.getCoordinate(0) != s2.getCoordinate(0));
}