0

I am trying to make a tool to analyse GPX files using OpenStreetMap data for identification of locations. I have successfully extracted all the waypoints from the GPX files and created as a MultiPoint object, and extracted OpenStreetMap border relation (border data) with overpass wrapper. The problem is taking relations with 500+ objects and turn them into Polygon or MultiPolygons. I have successfully created all the parts of the border as LineString objects, and all parts that are circular (LinearRings) are successfully made into Polygon objects. The problem is to join all the non-circular objects.

    newLines = []
    for line in lines:
        if isinstance(line, MultiLineString):
            newLines.extend(line)
        else:
            newLines.append(line)
    try:
        polygons.append(Polygon(linemerge(newLines)))
        logger.debug("Created Polygon from sum of lines")
    except:
        try:
            polygons.append(MultiPolygon(linemerge(newLines)))
            logger.debug("Created MultiPolygon from sum of lines")
        except:
            logger.debug("Failed to create Polygon from sum of lines")
            raise

... from log file, no exception thrown

2016/03/17 21:43:59: gpxupload.py DEBUG - Failed to create Polygon from sum of lines

What goes wrong?

Aun Johnsen
  • 13
  • 1
  • 7
  • What was the traceback of the exception? This was not captured/logged in the question. – Mike T Mar 23 '16 at 22:06
  • I was struggling to merge the outer bounds with linemerge, which didn't merge the lines and ended the task without throwing exceptions, when I skipped this point and created polygons of each way segment using buffer() I was able to join all the Polygonized way segments with cascading_union, and than delete the interior hole. Problem have been solved on my own account. – Aun Johnsen Mar 25 '16 at 15:26

2 Answers2

2

pyosmium can help if you want to go straight from osm files to shapely data. It's a library to read osm files, and has helper classes to create WKB (well-known binary) format objects from areas, which you can then load into shapely.

import osmium
import shapely.wkb

wkbfab = osmium.geom.WKBFactory()

class WayMerger(osmium.SimpleHandler):

    def area(self, a):
        wkbshape = wkbfab.create_multipolygon(a)
        shapely_obj = shapely.wkb.loads(wkbshape, hex=True)
ejegg
  • 358
  • 2
  • 7
0

linemerge didn't do the job. Converting each way segment to polygon with polygons.append(line.buffer(meter2deg(1.0))) and than cascade_union(polygons) worked.

Aun Johnsen
  • 13
  • 1
  • 7