Skip to content

Commit

Permalink
fixed bug handling polygons with multiple rings
Browse files Browse the repository at this point in the history
  • Loading branch information
adufilie committed Jul 24, 2015
1 parent 0b85a85 commit 537259d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
Expand Up @@ -104,6 +104,7 @@ public void convertFeature(IFeatureGeometryStream geomStream, int shapeType, Str
totalVertices++;
x = vertexStream.getX();
y = vertexStream.getY();

// don't add invalid points
if (x <= -Double.MAX_VALUE || x >= Double.MAX_VALUE ||
y <= -Double.MAX_VALUE || y >= Double.MAX_VALUE)
Expand All @@ -121,24 +122,27 @@ public void convertFeature(IFeatureGeometryStream geomStream, int shapeType, Str
// don't add consecutive duplicate points
if (x == prevX && y == prevY)
continue;
// stop adding points when the current coord is equal to the first coord
if (x == firstX && y == firstY)
// stop adding points when the current coord is equal to the first coord and we're at the end of a part
if (x == firstX && y == firstY && vertexStream.isEndOfPart())
break;
}

// add new point if we are at the end of outputPoints
if (reusableVertexChainLinks.size() == chainLength)
reusableVertexChainLinks.add(new VertexChainLink());

// save coord in next vertex object
vertex = reusableVertexChainLinks.get(chainLength);
// get next vertex object
if (chainLength < reusableVertexChainLinks.size())
vertex = reusableVertexChainLinks.get(chainLength);
else
reusableVertexChainLinks.add(vertex = new VertexChainLink());
// save coord
vertex.initialize(x, y, firstVertexID + chainLength);
// insert vertex in chain
reusableVertexChainLinks.get(0).insert(vertex);

chainLength++;
prevX = x;
prevY = y;

// stop at end of part
if (vertexStream.isEndOfPart())
break;
}

if (chainLength == 0)
Expand Down
Expand Up @@ -37,11 +37,15 @@ public interface IGeometryVertexStream
public boolean next();

/**
* @return The X coordinate of the current vertex.
* Checks if the current vertex ends a part of the geometry, meaning vertices that follow will be from a new part.
*/
public boolean isEndOfPart();
/**
* Gets the X coordinate of the current vertex.
*/
public double getX();
/**
* @return The Y coordinate of the current vertex.
* Gets the Y coordinate of the current vertex.
*/
public double getY();
}
Binary file modified WeaveServices/lib/GeometryStreamConverter.jar
Binary file not shown.
Expand Up @@ -17,6 +17,7 @@

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Polygon;

/**
* This is an interface to a stream of vertices from a geometry.
Expand All @@ -28,11 +29,18 @@ public class JTSGeometryVertexStream implements IGeometryVertexStream
public JTSGeometryVertexStream(Geometry geom)
{
coords = geom.getCoordinates();
index = -1; // start before the first coord so the first call to next() will not skip anything
index = -1; // start before the first coord so the first call to next() will not skip anything
partNum = -1; // start before the first part
partEndIndex = -1;
if (geom.getGeometryType().equals("Polygon"))
polygon = (Polygon)geom;
}


private Polygon polygon = null;
private Coordinate[] coords;
private int index;
private int index;
private int partNum;
private int partEndIndex;

/**
* This checks if there is a vertex available from the stream.
Expand All @@ -41,7 +49,7 @@ public JTSGeometryVertexStream(Geometry geom)
public boolean hasNext()
{
return index + 1 < coords.length;
}
}

/**
* This advances the internal pointer to the next vertex.
Expand All @@ -51,19 +59,36 @@ public boolean hasNext()
*/
public boolean next()
{
if (index == partEndIndex)
{
++partNum;
if (partNum == 0)
partEndIndex += polygon != null ? polygon.getExteriorRing().getNumPoints() : coords.length;
else
partEndIndex += polygon.getInteriorRingN(partNum - 1).getNumPoints();
}

return ++index < coords.length;
}

/**
* Checks if the current vertex ends a part of the geometry, meaning vertices that follow will be from a new part.
*/
public boolean isEndOfPart()
{
return index == partEndIndex;
}

/**
* @return The X coordinate of the current vertex.
* Gets the X coordinate of the current vertex.
*/
public double getX()
{
return coords[index].x;
}

/**
* @return The Y coordinate of the current vertex.
* Gets the Y coordinate of the current vertex.
*/
public double getY()
{
Expand Down

0 comments on commit 537259d

Please sign in to comment.