Polygon with outline and holes

783
4
Jump to solution
12-03-2021 08:16 AM
RobertSchweikert
New Contributor II

Hi, How are outlines and holes of a polygon stored in the Polygon java object in the runtime API? And what about multipolygons? How are they stored?

0 Kudos
1 Solution

Accepted Solutions
MarkBaird
Esri Regular Contributor

If you are wanting to draw a polygon with a hole in it, the following method shows how this can be achieved.  The polygon is made up of "parts" which form the inner and out rings of your polygon with a hole it it.

 

  private Polygon makePolygonWithHole() {
    // create a new point collection for polygon
    PointCollection outerPoints = new PointCollection(SpatialReferences.getWebMercator());

    // create and add points to the point collection
    outerPoints.add(new Point(2000, 2000));
    outerPoints.add(new Point(2000, 4000));
    outerPoints.add(new Point(4000, 4000));
    outerPoints.add(new Point(4000, 2000));

    Part outerPart = new Part(outerPoints);

    // inner part (the hole)
    PointCollection innerPoints = new PointCollection(SpatialReferences.getWebMercator());

    innerPoints.add(new Point(3500,2500));
    innerPoints.add(new Point(3500,3500));
    innerPoints.add(new Point(2500,3500));
    innerPoints.add(new Point(2500,2500));

    Part innerPart = new Part(innerPoints);

    // make parts for multi part polygon
    PartCollection partCollection = new PartCollection(SpatialReferences.getWebMercator());
    partCollection.add(outerPart);
    partCollection.add(innerPart);

    // create the polyline from the point collection
    Polygon polygon = new Polygon(partCollection);

    return (Polygon) GeometryEngine.simplify(polygon);
  }

 

 

The polygon can be used in features (persisted in a feature service for example), or a graphics overlay.  I've shown some simple code below which shows how to draw the polygon in a graphics overlay:

 

      // create a map view and set the map to it
      mapView = new MapView();
      mapView.setMap(map);

      GraphicsOverlay graphicsOverlay = new GraphicsOverlay();
      mapView.getGraphicsOverlays().add(graphicsOverlay);

      SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFF0063FF, 1);

      SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, 0xFF00FF00, lineSymbol);

      Graphic graphicWithHole = new Graphic(makePolygonWithHole(), fillSymbol);

      graphicsOverlay.getGraphics().add(graphicWithHole);

 

 

 

MarkBaird_0-1638789596096.png

 

Multi part polygons can also be created with parts.

Does this help?

View solution in original post

4 Replies
MarkBaird
Esri Regular Contributor

@RobertSchweikert yes you can do both of these things.  How are you wanting to store them?  

Are you wanting to store them in a feature layer or a graphic overlay?

0 Kudos
MarkBaird
Esri Regular Contributor

If you are wanting to draw a polygon with a hole in it, the following method shows how this can be achieved.  The polygon is made up of "parts" which form the inner and out rings of your polygon with a hole it it.

 

  private Polygon makePolygonWithHole() {
    // create a new point collection for polygon
    PointCollection outerPoints = new PointCollection(SpatialReferences.getWebMercator());

    // create and add points to the point collection
    outerPoints.add(new Point(2000, 2000));
    outerPoints.add(new Point(2000, 4000));
    outerPoints.add(new Point(4000, 4000));
    outerPoints.add(new Point(4000, 2000));

    Part outerPart = new Part(outerPoints);

    // inner part (the hole)
    PointCollection innerPoints = new PointCollection(SpatialReferences.getWebMercator());

    innerPoints.add(new Point(3500,2500));
    innerPoints.add(new Point(3500,3500));
    innerPoints.add(new Point(2500,3500));
    innerPoints.add(new Point(2500,2500));

    Part innerPart = new Part(innerPoints);

    // make parts for multi part polygon
    PartCollection partCollection = new PartCollection(SpatialReferences.getWebMercator());
    partCollection.add(outerPart);
    partCollection.add(innerPart);

    // create the polyline from the point collection
    Polygon polygon = new Polygon(partCollection);

    return (Polygon) GeometryEngine.simplify(polygon);
  }

 

 

The polygon can be used in features (persisted in a feature service for example), or a graphics overlay.  I've shown some simple code below which shows how to draw the polygon in a graphics overlay:

 

      // create a map view and set the map to it
      mapView = new MapView();
      mapView.setMap(map);

      GraphicsOverlay graphicsOverlay = new GraphicsOverlay();
      mapView.getGraphicsOverlays().add(graphicsOverlay);

      SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFF0063FF, 1);

      SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, 0xFF00FF00, lineSymbol);

      Graphic graphicWithHole = new Graphic(makePolygonWithHole(), fillSymbol);

      graphicsOverlay.getGraphics().add(graphicWithHole);

 

 

 

MarkBaird_0-1638789596096.png

 

Multi part polygons can also be created with parts.

Does this help?

RobertSchweikert
New Contributor II

Thanks Mark, this is super helpful. If you could also share about the read process for polygons with outlines and holes like specifically how is reading a multipolygon with let's say three parts (3 polygons) different from reading a polygon with 1outline and 2 holes. Is my understanding of creation of a multipolygon correct?

0 Kudos
MarkBaird
Esri Regular Contributor

Reading the polygon is just the same as creating it.  From the polygon class you can get the parts which make up the polygon rings.

You can have multiple rings (parts) which can make up a multi-part polygon.  For example you could have a polygon representing the multiple islands which make up the Outer Hebrides.

You can also have rings inside rings, and even rings in side rings, which are inside rings!  This allows you to make items like:

 - An island which has a lake in it

 - An island which has a lake, and the lake has another island which in turn has its own lake.

 

0 Kudos