@Test public void testInterpolate() { GeometryFactory gf = new GeometryFactory(); CoordinateSequence cs = gf.getCoordinateSequenceFactory().create(2, 2); cs.setOrdinate(0, 0, -180.0); cs.setOrdinate(0, 1, 45.0); cs.setOrdinate(1, 0, 180.0); cs.setOrdinate(1, 1, 45.0); CoordinateSequence interpolated = GeometryHelper.interpolateLinear( gf.createLineString(cs), 20.0, gf); double lon = -180.0; double lat = 45.0; for (int i = 0; i <= 18; i++) { assertEquals(lon, interpolated.getOrdinate(i, 0), 0.0); assertEquals(lat, interpolated.getOrdinate(i, 1), 0.0); lon += 20.0; } }
public static CoordinateSequence interpolateLinear(LineString line, double threshold, GeometryFactory gf) { double[] tempPointArray = new double[128]; int i = 0; Point point = line.getPointN(0); double x0 = point.getX(); double y0 = point.getY(); tempPointArray[i++] = x0; tempPointArray[i++] = y0; for (int p = 1; p < line.getNumPoints(); p++) { point = line.getPointN(p); double x1 = point.getX(); double y1 = point.getY(); double dx = x1 - x0; double dy = y1 - y0; if (dy == 0) { int nSeg = (int) Math.ceil(Math.abs(dx) / threshold); for (int j = 0; j < nSeg - 1; j++) { if (i == tempPointArray.length) { tempPointArray = grow(tempPointArray); } tempPointArray[i++] = x0 + (j + 1) * dx / nSeg; tempPointArray[i++] = y0; } } else if (dx == 0) { int nSeg = (int) Math.ceil(Math.abs(dy) / threshold); for (int j = 0; j < nSeg - 1; j++) { if (i == tempPointArray.length) { tempPointArray = grow(tempPointArray); } tempPointArray[i++] = x0; tempPointArray[i++] = y0 + (j + 1) * dy / nSeg; } } else { double c = Math.sqrt(dx * dx + dy * dy); int nSeg = (int) Math.ceil(c / threshold); for (int j = 0; j < nSeg - 1; j++) { if (i == tempPointArray.length) { tempPointArray = grow(tempPointArray); } tempPointArray[i++] = x0 + (j + 1) * dx / nSeg; tempPointArray[i++] = y0 + (j + 1) * dy / nSeg; } } if (i == tempPointArray.length) { tempPointArray = grow(tempPointArray); } tempPointArray[i++] = x1; tempPointArray[i++] = y1; x0 = x1; y0 = y1; } if (gf == null) { gf = new GeometryFactory(); } CoordinateSequence cs = gf.getCoordinateSequenceFactory().create(i / 2, 2); for (int j = 0, k = 0; k < i; j++) { cs.setOrdinate(j, 0, tempPointArray[k++]); cs.setOrdinate(j, 1, tempPointArray[k++]); } return cs; }
GeometryHelper { public static CoordinateSequence interpolateLinear(LineString line, double threshold, GeometryFactory gf) { double[] tempPointArray = new double[128]; int i = 0; Point point = line.getPointN(0); double x0 = point.getX(); double y0 = point.getY(); tempPointArray[i++] = x0; tempPointArray[i++] = y0; for (int p = 1; p < line.getNumPoints(); p++) { point = line.getPointN(p); double x1 = point.getX(); double y1 = point.getY(); double dx = x1 - x0; double dy = y1 - y0; if (dy == 0) { int nSeg = (int) Math.ceil(Math.abs(dx) / threshold); for (int j = 0; j < nSeg - 1; j++) { if (i == tempPointArray.length) { tempPointArray = grow(tempPointArray); } tempPointArray[i++] = x0 + (j + 1) * dx / nSeg; tempPointArray[i++] = y0; } } else if (dx == 0) { int nSeg = (int) Math.ceil(Math.abs(dy) / threshold); for (int j = 0; j < nSeg - 1; j++) { if (i == tempPointArray.length) { tempPointArray = grow(tempPointArray); } tempPointArray[i++] = x0; tempPointArray[i++] = y0 + (j + 1) * dy / nSeg; } } else { double c = Math.sqrt(dx * dx + dy * dy); int nSeg = (int) Math.ceil(c / threshold); for (int j = 0; j < nSeg - 1; j++) { if (i == tempPointArray.length) { tempPointArray = grow(tempPointArray); } tempPointArray[i++] = x0 + (j + 1) * dx / nSeg; tempPointArray[i++] = y0 + (j + 1) * dy / nSeg; } } if (i == tempPointArray.length) { tempPointArray = grow(tempPointArray); } tempPointArray[i++] = x1; tempPointArray[i++] = y1; x0 = x1; y0 = y1; } if (gf == null) { gf = new GeometryFactory(); } CoordinateSequence cs = gf.getCoordinateSequenceFactory().create(i / 2, 2); for (int j = 0, k = 0; k < i; j++) { cs.setOrdinate(j, 0, tempPointArray[k++]); cs.setOrdinate(j, 1, tempPointArray[k++]); } return cs; } }
GeometryHelper { public static CoordinateSequence interpolateLinear(LineString line, double threshold, GeometryFactory gf) { double[] tempPointArray = new double[128]; int i = 0; Point point = line.getPointN(0); double x0 = point.getX(); double y0 = point.getY(); tempPointArray[i++] = x0; tempPointArray[i++] = y0; for (int p = 1; p < line.getNumPoints(); p++) { point = line.getPointN(p); double x1 = point.getX(); double y1 = point.getY(); double dx = x1 - x0; double dy = y1 - y0; if (dy == 0) { int nSeg = (int) Math.ceil(Math.abs(dx) / threshold); for (int j = 0; j < nSeg - 1; j++) { if (i == tempPointArray.length) { tempPointArray = grow(tempPointArray); } tempPointArray[i++] = x0 + (j + 1) * dx / nSeg; tempPointArray[i++] = y0; } } else if (dx == 0) { int nSeg = (int) Math.ceil(Math.abs(dy) / threshold); for (int j = 0; j < nSeg - 1; j++) { if (i == tempPointArray.length) { tempPointArray = grow(tempPointArray); } tempPointArray[i++] = x0; tempPointArray[i++] = y0 + (j + 1) * dy / nSeg; } } else { double c = Math.sqrt(dx * dx + dy * dy); int nSeg = (int) Math.ceil(c / threshold); for (int j = 0; j < nSeg - 1; j++) { if (i == tempPointArray.length) { tempPointArray = grow(tempPointArray); } tempPointArray[i++] = x0 + (j + 1) * dx / nSeg; tempPointArray[i++] = y0 + (j + 1) * dy / nSeg; } } if (i == tempPointArray.length) { tempPointArray = grow(tempPointArray); } tempPointArray[i++] = x1; tempPointArray[i++] = y1; x0 = x1; y0 = y1; } if (gf == null) { gf = new GeometryFactory(); } CoordinateSequence cs = gf.getCoordinateSequenceFactory().create(i / 2, 2); for (int j = 0, k = 0; k < i; j++) { cs.setOrdinate(j, 0, tempPointArray[k++]); cs.setOrdinate(j, 1, tempPointArray[k++]); } return cs; }  }
GeometryHelper { public static CoordinateSequence interpolateLinear(LineString line, double threshold, GeometryFactory gf) { double[] tempPointArray = new double[128]; int i = 0; Point point = line.getPointN(0); double x0 = point.getX(); double y0 = point.getY(); tempPointArray[i++] = x0; tempPointArray[i++] = y0; for (int p = 1; p < line.getNumPoints(); p++) { point = line.getPointN(p); double x1 = point.getX(); double y1 = point.getY(); double dx = x1 - x0; double dy = y1 - y0; if (dy == 0) { int nSeg = (int) Math.ceil(Math.abs(dx) / threshold); for (int j = 0; j < nSeg - 1; j++) { if (i == tempPointArray.length) { tempPointArray = grow(tempPointArray); } tempPointArray[i++] = x0 + (j + 1) * dx / nSeg; tempPointArray[i++] = y0; } } else if (dx == 0) { int nSeg = (int) Math.ceil(Math.abs(dy) / threshold); for (int j = 0; j < nSeg - 1; j++) { if (i == tempPointArray.length) { tempPointArray = grow(tempPointArray); } tempPointArray[i++] = x0; tempPointArray[i++] = y0 + (j + 1) * dy / nSeg; } } else { double c = Math.sqrt(dx * dx + dy * dy); int nSeg = (int) Math.ceil(c / threshold); for (int j = 0; j < nSeg - 1; j++) { if (i == tempPointArray.length) { tempPointArray = grow(tempPointArray); } tempPointArray[i++] = x0 + (j + 1) * dx / nSeg; tempPointArray[i++] = y0 + (j + 1) * dy / nSeg; } } if (i == tempPointArray.length) { tempPointArray = grow(tempPointArray); } tempPointArray[i++] = x1; tempPointArray[i++] = y1; x0 = x1; y0 = y1; } if (gf == null) { gf = new GeometryFactory(); } CoordinateSequence cs = gf.getCoordinateSequenceFactory().create(i / 2, 2); for (int j = 0, k = 0; k < i; j++) { cs.setOrdinate(j, 0, tempPointArray[k++]); cs.setOrdinate(j, 1, tempPointArray[k++]); } return cs; }  static CoordinateSequence createLinearRing(GeometryFactory gf,
            double x1, double y1, double x2, double y2); static CoordinateSequence interpolateLinear(LineString line, double threshold, GeometryFactory gf); static boolean isWithin(final CoordinateSequence cs,
            final double minX, final double minY,
            final double maxX, final double maxY); }
GeometryHelper { public static CoordinateSequence interpolateLinear(LineString line, double threshold, GeometryFactory gf) { double[] tempPointArray = new double[128]; int i = 0; Point point = line.getPointN(0); double x0 = point.getX(); double y0 = point.getY(); tempPointArray[i++] = x0; tempPointArray[i++] = y0; for (int p = 1; p < line.getNumPoints(); p++) { point = line.getPointN(p); double x1 = point.getX(); double y1 = point.getY(); double dx = x1 - x0; double dy = y1 - y0; if (dy == 0) { int nSeg = (int) Math.ceil(Math.abs(dx) / threshold); for (int j = 0; j < nSeg - 1; j++) { if (i == tempPointArray.length) { tempPointArray = grow(tempPointArray); } tempPointArray[i++] = x0 + (j + 1) * dx / nSeg; tempPointArray[i++] = y0; } } else if (dx == 0) { int nSeg = (int) Math.ceil(Math.abs(dy) / threshold); for (int j = 0; j < nSeg - 1; j++) { if (i == tempPointArray.length) { tempPointArray = grow(tempPointArray); } tempPointArray[i++] = x0; tempPointArray[i++] = y0 + (j + 1) * dy / nSeg; } } else { double c = Math.sqrt(dx * dx + dy * dy); int nSeg = (int) Math.ceil(c / threshold); for (int j = 0; j < nSeg - 1; j++) { if (i == tempPointArray.length) { tempPointArray = grow(tempPointArray); } tempPointArray[i++] = x0 + (j + 1) * dx / nSeg; tempPointArray[i++] = y0 + (j + 1) * dy / nSeg; } } if (i == tempPointArray.length) { tempPointArray = grow(tempPointArray); } tempPointArray[i++] = x1; tempPointArray[i++] = y1; x0 = x1; y0 = y1; } if (gf == null) { gf = new GeometryFactory(); } CoordinateSequence cs = gf.getCoordinateSequenceFactory().create(i / 2, 2); for (int j = 0, k = 0; k < i; j++) { cs.setOrdinate(j, 0, tempPointArray[k++]); cs.setOrdinate(j, 1, tempPointArray[k++]); } return cs; }  static CoordinateSequence createLinearRing(GeometryFactory gf,
            double x1, double y1, double x2, double y2); static CoordinateSequence interpolateLinear(LineString line, double threshold, GeometryFactory gf); static boolean isWithin(final CoordinateSequence cs,
            final double minX, final double minY,
            final double maxX, final double maxY);  }