2

I have two polygons, one inside another, and one draw it on the map there is a line connecting vertices of outside and inside polygon. I am trying to draw it like multipolygon using

Polygon[] polygons = null;
List<Polygon> PolygonList = new ArrayList<Polygon>();
polygons = new Polygon[PolygonList.size()];
PolygonList.toArray(polygons);
MultiPolygon multiPolygon = null;
multiPolygon = geometryFactory.createMultiPolygon(polygons);

Original coordinates stored in PolygonList and then converted in polygon array are,

[POLYGON ((6420562.64 4963523.59, 6420559.63 4963527.13, 6420551.28 4963536.4, 6420545.66 4963542.93, 6420539.47 4963549.81, 6420549.2 4963566.43, 6420557.44 4963581.87, 6420547.09 4963592.12, 6420546.75 4963592.47, 6420538.87 4963604.54, 6420542.3 4963606.54, 6420554.08 4963592.16, 6420569.13 4963585.65, 6420581.56 4963580.36, 6420579.2 4963571.27, 6420577.81 4963568, 6420576.04 4963565.49, 6420573.62 4963563.02, 6420569.94 4963560.08, 6420564.25 4963556.37, 6420556.16 4963551.2, 6420552.46 4963548.07, 6420551.29 4963549.4, 6420549.14 4963547.81, 6420552.7 4963543.31, 6420554.96 4963544.66, 6420559.37 4963535.02, 6420556.44 4963532.82, 6420560.19 4963527.58, 6420563.24 4963529.58, 6420564.47 4963526.17, 6420562.64 4963523.59)), POLYGON ((6420560.94 4963584.49, 6420567.53 4963579.99, 6420563.58 4963574.11, 6420557.04 4963578.61, 6420560.94 4963584.49))]

but when I get coordinates from created multipolygon all coordinates are mixed,

[(6420562.64, 4963523.59, NaN), (6420559.63, 4963527.13, NaN), (6420551.28, 4963536.4, NaN), (6420545.66, 4963542.93, NaN), (6420539.47, 4963549.81, NaN), (6420549.2, 4963566.43, NaN), (6420557.44, 4963581.87, NaN), (6420547.09, 4963592.12, NaN), (6420546.75, 4963592.47, NaN), (6420538.87, 4963604.54, NaN), (6420542.3, 4963606.54, NaN), (6420554.08, 4963592.16, NaN), (6420569.13, 4963585.65, NaN), (6420581.56, 4963580.36, NaN), (6420579.2, 4963571.27, NaN), (6420577.81, 4963568.0, NaN), (6420576.04, 4963565.49, NaN), (6420573.62, 4963563.02, NaN), (6420569.94, 4963560.08, NaN), (6420564.25, 4963556.37, NaN), (6420556.16, 4963551.2, NaN), (6420552.46, 4963548.07, NaN), (6420551.29, 4963549.4, NaN), (6420549.14, 4963547.81, NaN), (6420552.7, 4963543.31, NaN), (6420554.96, 4963544.66, NaN), (6420559.37, 4963535.02, NaN), (6420556.44, 4963532.82, NaN), (6420560.19, 4963527.58, NaN), (6420563.24, 4963529.58, NaN), (6420564.47, 4963526.17, NaN), (6420562.64, 4963523.59, NaN)]

and the drawn polygon look like this enter image description here (image is rotated) Is there any way to use multipolygon or need to use a linear ring and define polygon with holes in it.enter image description here

  • your image is still missing – Ian Turton Apr 20 '17 at 15:03
  • I don't know what happen with it, I added it again – Zoran Kokeza Apr 20 '17 at 15:37
  • I have solved this using a polygon with holes, but it will be nice to know what I was doing wrong with multi-polygon. – Zoran Kokeza Apr 20 '17 at 15:46
  • your code makes no sense, and I still can't work out what you are trying to do, is the rectangle a hole or another polygon? – Ian Turton Apr 20 '17 at 15:48
  • The second polygon(this case rectangle) in the list is always inside the first polygon(outside polygon), the rectangle is a hole. It draws them as its supposed, only this line shouldn't be there, from outside polygon to rectangle. I am getting coordinates from a database, so I didn't put that part here, and then create a polygon from them and it to list. – Zoran Kokeza Apr 20 '17 at 16:02
1

The problem is (as you discovered) that you don't want a multipolygon at all, you want a single polygon with a hole in it. This is how you would create one of those:

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.geom.Polygon;

public class Holes {
  /**
   * build a polygon with a hole in it.
   * 
   * @param args
   */
  public static void main(String[] args) {
    GeometryFactory gf = new GeometryFactory();
    // coords for outer ring & hole
    String[] coords = {
        "6420562.64 4963523.59, 6420559.63 4963527.13, 6420551.28 4963536.4, 6420545.66 4963542.93, 6420539.47 4963549.81, 6420549.2 4963566.43, 6420557.44 4963581.87, 6420547.09 4963592.12, 6420546.75 4963592.47, 6420538.87 4963604.54, 6420542.3 4963606.54, 6420554.08 4963592.16, 6420569.13 4963585.65, 6420581.56 4963580.36, 6420579.2 4963571.27, 6420577.81 4963568, 6420576.04 4963565.49, 6420573.62 4963563.02, 6420569.94 4963560.08, 6420564.25 4963556.37, 6420556.16 4963551.2, 6420552.46 4963548.07, 6420551.29 4963549.4, 6420549.14 4963547.81, 6420552.7 4963543.31, 6420554.96 4963544.66, 6420559.37 4963535.02, 6420556.44 4963532.82, 6420560.19 4963527.58, 6420563.24 4963529.58, 6420564.47 4963526.17, 6420562.64 4963523.59",
        "6420560.94 4963584.49, 6420567.53 4963579.99, 6420563.58 4963574.11, 6420557.04 4963578.61, 6420560.94 4963584.49" };

    Coordinate[] c = convertToCoords(coords[0]);
    LinearRing outer = gf.createLinearRing(c);
    LinearRing[] inners = new LinearRing[1];
    inners[0] = gf.createLinearRing(convertToCoords(coords[1]));

    Polygon out = gf.createPolygon(outer, inners);
    System.out.println(out);
  }

  /**
   * @param coords
   * @return
   */
  public static Coordinate[] convertToCoords(String coords) {
    String[] pairs = coords.split(",");

    Coordinate[] c = new Coordinate[pairs.length];
    int i = 0;
    for (String p : pairs) {
      String[] xy = p.trim().split("  *");
      c[i++] = new Coordinate(Double.parseDouble(xy[0]), Double.parseDouble(xy[1]));
    }
    return c;
  }

}

This gives you the following WKT:

POLYGON ((6420562.64 4963523.59, 6420559.63 4963527.13, 6420551.28 4963536.4, 6420545.66 4963542.93, 6420539.47 4963549.81, 6420549.2 4963566.43, 6420557.44 4963581.87, 6420547.09 4963592.12, 6420546.75 4963592.47, 6420538.87 4963604.54, 6420542.3 4963606.54, 6420554.08 4963592.16, 6420569.13 4963585.65, 6420581.56 4963580.36, 6420579.2 4963571.27, 6420577.81 4963568, 6420576.04 4963565.49, 6420573.62 4963563.02, 6420569.94 4963560.08, 6420564.25 4963556.37, 6420556.16 4963551.2, 6420552.46 4963548.07, 6420551.29 4963549.4, 6420549.14 4963547.81, 6420552.7 4963543.31, 6420554.96 4963544.66, 6420559.37 4963535.02, 6420556.44 4963532.82, 6420560.19 4963527.58, 6420563.24 4963529.58, 6420564.47 4963526.17, 6420562.64 4963523.59), (6420560.94 4963584.49, 6420567.53 4963579.99, 6420563.58 4963574.11, 6420557.04 4963578.61, 6420560.94 4963584.49))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.