def admin_UGANDA_OSM(filename, shapeu, admins):
    logo.starting("Attributes read", parseosm.getNbRelation())
    for relationid in parseosm.getIterRelation():
        logo.progress()
        relation = parseosm.getRelation(relationid)

        admins[relationid] = { "name" : relation['tags']['name'],
                               "level" : int(relation['tags']['admin_level']),
                               "inner" : set(),
                               "outer" : set(),
                             }
        if 'old_name' in relation['tags']:
            admins[relationid]["old_name"] = relation['tags']['old_name']

        wayids = [ data[1] for data in relation['members']
                   if data[0] == 'way' and data[2] in ('outer', 'inner') ]
        lineset = set()
        for wayid in wayids:
            pntinring = []
            linegeometry = parseosm.getGeometryWay(wayid)
            for lon, lat in linegeometry:
                pointid = shapeu.getPoint(lon, lat)
                if pointid is not None:
                    pntinring.append(pointid)

            for pnt in xrange(1, len(pntinring)):
                if pntinring[pnt-1] ==  pntinring[pnt]:
                    # If 2 coordinates after rounding give the same point id
                    continue
                segment = shapeu.getSegment(pntinring[pnt-1], pntinring[pnt])
                lineset.add(shapeu.getLine(segment))

        # Update each administrative level
        admins[relationid]["outer"].update(lineset)
    logo.ending()
def read_UGANDA_OSM(filename, shapeu):
    parseosm.parse_xml(open(filename).read())
    shapeutil.precision = 14  # don't do aggressive rounding
    shapeutil.testnearest = []  # nor neighbour hack

    # Read each polygon and build the connection arrays (point, segment, line)
    logo.starting("Geometry read", parseosm.getNbRelation())
    for relationid in parseosm.getIterRelation():
        logo.progress()
        relation = parseosm.getRelation(relationid)
        wayids = [ data[1] for data in relation['members']
                   if data[0] == 'way' and data[2] in ('outer', 'inner') ]

        for wayid in wayids:
            linegeometry = parseosm.getGeometryWay(wayid)
            lon1, lat1 = linegeometry[0]
            for lon2, lat2 in linegeometry[1:]:
                shapeu.makeSegment(lon1, lat1, lon2, lat2)
                lon1, lat1 = lon2, lat2
    logo.ending()