Contents Prev: How to build geometry Next: Applying geometric operations to polygons

Building polygons from relations

Areas can be represented in OpenStreetMap by closed ways and by relations that reference some ways as boundary components. Relations are the most powerful elements in OSM and they can be used to model areas with several components. Such geometric objects are called multipolygons. They consist of one or more polygons and each polygon may have holes.

For example here is a relation that models a few buildings:

The following code builds a MultiPolygon from a relation and prints the resulting geometry to the console in GeoJSON format:

  public static void main(String[] args)
      throws IOException, EntityNotFoundException
  {
    // String query =
    // "http://overpass-api.de/api/interpreter?data=(rel(122895);>;);out;";
    String query = "http://osmtestdata.topobyte.de/relation-122895.osm";
 
    // Open a stream
    InputStream input = new URL(query).openStream();
 
    OsmIterator iterator = new OsmXmlIterator(input, false);
    InMemoryMapDataSet data = MapDataSetLoader.read(iterator, falsefalse,
        true);
 
    TLongObjectMap<OsmRelation> relations = data.getRelations();
 
    if (relations.isEmpty()) {
      System.out.println("No relation found");
      return;
    }
 
    OsmRelation relation = relations.valueCollection().iterator().next();
 
    Geometry polygon = new GeometryBuilder().build(relation, data);
 
    Map<String, String> tags = OsmModelUtil.getTagsAsMap(relation);
    Map<String, Object> properties = new HashMap<>();
    for (String key : tags.keySet()) {
      properties.put(key, tags.get(key));
    }
 
    GeoJSONWriter writer = new GeoJSONWriter();
    org.wololo.geojson.Geometry g = writer.write(polygon);
    Feature feature = new Feature(g, properties);
 
    String json = feature.toString();
 
    System.out.println(GeoJsonHelper.prettyPrintFeature(json));
  }

Here's the output:

{
  "type" : "Feature",
  "properties" : {
    "name" : "Blaues Wunder",
    "type" : "multipolygon"
  },
  "geometry" : {
    "type" : "MultiPolygon",
    "coordinates" : [ [ [ [ 13.2077736, 52.6330749 ], [ 13.2077811, 52.6330802 ], [ 13.2077758, 52.633094 ], [ 13.2077669, 52.6331071 ], [ 13.2077542, 52.633119 ], [ 13.2077381, 52.6331294 ], [ 13.2077192, 52.6331377 ], [ 13.2076981, 52.6331438 ], [ 13.2076756, 52.6331475 ], [ 13.2076525, 52.6331487 ], [ 13.2076451, 52.6331437 ], [ 13.2073873, 52.6332817 ], [ 13.2068207, 52.6328897 ], [ 13.2070788, 52.6327517 ], [ 13.2070715, 52.6327466 ], [ 13.2070779, 52.632731 ], [ 13.207089, 52.6327163 ], [ 13.2071049, 52.6327034 ], [ 13.2071248, 52.6326927 ], [ 13.2071479, 52.6326848 ], [ 13.2071732, 52.63268 ], [ 13.2071995, 52.6326783 ], [ 13.2072068, 52.6326833 ], [ 13.2074649, 52.6325452 ], [ 13.2080312, 52.6329371 ], [ 13.2077736, 52.6330749 ] ], [ [ 13.2072557, 52.6329074 ], [ 13.2074367, 52.6328099 ], [ 13.2075972, 52.6329196 ], [ 13.2074162, 52.6330171 ], [ 13.2072557, 52.6329074 ] ] ], [ [ [ 13.2066865, 52.6335848 ], [ 13.2071467, 52.6333394 ], [ 13.206794, 52.6333266 ], [ 13.2068012, 52.6332723 ], [ 13.2071504, 52.6332876 ], [ 13.2067443, 52.6330045 ], [ 13.2066865, 52.6335848 ] ] ], [ [ [ 13.2089258, 52.6340292 ], [ 13.2089183, 52.634024 ], [ 13.2086601, 52.6341621 ], [ 13.2080935, 52.63377 ], [ 13.2083518, 52.6336319 ], [ 13.2083444, 52.6336268 ], [ 13.2083497, 52.633613 ], [ 13.2083586, 52.6336 ], [ 13.2083714, 52.6335881 ], [ 13.2083875, 52.6335778 ], [ 13.2084063, 52.6335695 ], [ 13.2084273, 52.6335634 ], [ 13.2084498, 52.6335598 ], [ 13.2084728, 52.6335586 ], [ 13.2084799, 52.6335634 ], [ 13.2087378, 52.6334255 ], [ 13.2093045, 52.6338175 ], [ 13.2090464, 52.6339555 ], [ 13.2090537, 52.6339607 ], [ 13.2090475, 52.6339763 ], [ 13.2090364, 52.633991 ], [ 13.2090205, 52.634004 ], [ 13.2090006, 52.6340147 ], [ 13.2089775, 52.6340227 ], [ 13.2089522, 52.6340275 ], [ 13.2089258, 52.6340292 ] ], [ [ 13.2085282, 52.6337868 ], [ 13.2087099, 52.6336898 ], [ 13.2088705, 52.6338006 ], [ 13.2086888, 52.6338976 ], [ 13.2085282, 52.6337868 ] ] ], [ [ [ 13.2074843, 52.6341464 ], [ 13.2079472, 52.6338994 ], [ 13.207924, 52.6341117 ], [ 13.2080072, 52.634115 ], [ 13.2080336, 52.6338998 ], [ 13.2084403, 52.634186 ], [ 13.2074843, 52.6341464 ] ] ], [ [ [ 13.2086193, 52.6325596 ], [ 13.2081428, 52.6328141 ], [ 13.2081641, 52.6326027 ], [ 13.2080801, 52.6325998 ], [ 13.2080562, 52.6328098 ], [ 13.2076378, 52.632523 ], [ 13.2086193, 52.6325596 ] ] ], [ [ [ 13.2093498, 52.633701 ], [ 13.2094121, 52.633103 ], [ 13.2089427, 52.6333541 ], [ 13.2092887, 52.6333678 ], [ 13.2092836, 52.6334203 ], [ 13.208936, 52.6334062 ], [ 13.2093498, 52.633701 ] ] ], [ [ [ 13.2073, 52.634109 ], [ 13.2070731, 52.6339521 ], [ 13.2070645, 52.6339566 ], [ 13.2070388, 52.6339529 ], [ 13.2070147, 52.6339461 ], [ 13.2069935, 52.6339365 ], [ 13.2069759, 52.6339244 ], [ 13.2069629, 52.6339103 ], [ 13.2069549, 52.633895 ], [ 13.2069518, 52.633879 ], [ 13.2069606, 52.6338742 ], [ 13.2067336, 52.6337172 ], [ 13.2073776, 52.6333726 ], [ 13.2076043, 52.6335295 ], [ 13.2076126, 52.633525 ], [ 13.2076353, 52.6335281 ], [ 13.2076567, 52.6335335 ], [ 13.2076763, 52.6335412 ], [ 13.2076932, 52.633551 ], [ 13.207707, 52.6335624 ], [ 13.2077172, 52.6335751 ], [ 13.2077233, 52.6335887 ], [ 13.2077257, 52.6336028 ], [ 13.207717, 52.6336074 ], [ 13.2079439, 52.6337644 ], [ 13.2073, 52.634109 ] ], [ [ 13.2071685, 52.6337343 ], [ 13.2073495, 52.6336373 ], [ 13.2075095, 52.6337473 ], [ 13.2073285, 52.6338443 ], [ 13.2071685, 52.6337343 ] ] ], [ [ [ 13.2083994, 52.6331041 ], [ 13.2084078, 52.6330996 ], [ 13.2081813, 52.6329428 ], [ 13.2088253, 52.6325982 ], [ 13.2090523, 52.6327553 ], [ 13.209061, 52.6327506 ], [ 13.2090835, 52.6327538 ], [ 13.2091049, 52.6327592 ], [ 13.2091243, 52.6327669 ], [ 13.2091411, 52.6327766 ], [ 13.2091548, 52.632788 ], [ 13.2091649, 52.6328007 ], [ 13.209171, 52.6328143 ], [ 13.2091734, 52.6328283 ], [ 13.2091646, 52.6328329 ], [ 13.2093917, 52.63299 ], [ 13.2087475, 52.6333346 ], [ 13.2085206, 52.6331776 ], [ 13.2085124, 52.6331821 ], [ 13.2084897, 52.633179 ], [ 13.2084682, 52.6331735 ], [ 13.2084486, 52.6331658 ], [ 13.2084317, 52.6331561 ], [ 13.2084179, 52.6331446 ], [ 13.2084078, 52.6331319 ], [ 13.2084017, 52.6331182 ], [ 13.2083994, 52.6331041 ] ], [ [ 13.2086167, 52.6329597 ], [ 13.2087974, 52.6328631 ], [ 13.2089568, 52.6329731 ], [ 13.208776, 52.6330696 ], [ 13.2086167, 52.6329597 ] ] ] ]
  }
}

You can paste this into a page like geojson.io to view the multipolygon on an interactive map. Alternatively the result is also available in the examples repository.

Contents Prev: How to build geometry Next: Applying geometric operations to polygons