def get_polygons(self): """ Return campaign's area as Shapely Polygon. :return: Polygons :rtype: list """ campaign_polygons = [] for feature in self.geometry['features']: polygon = geometry.Polygon(feature['geometry']['coordinates'][0]) campaign_polygons.append(polygon) return campaign_polygons
def get_union_polygons(self): """Return union polygons""" simplify = False if len(self.geometry['features']) > 1: polygons = [] for feature in self.geometry['features']: polygons.append( geometry.Polygon(feature['geometry']['coordinates'][0])) cascaded_polygons = ops.cascaded_union(polygons) simplify = True else: cascaded_polygons = geometry.Polygon( self.geometry['features'][0]['geometry']['coordinates'][0]) joined_polygons = cascaded_polygons.buffer( 0.001, 1, join_style=geometry.JOIN_STYLE.mitre).buffer( -0.001, 1, join_style=geometry.JOIN_STYLE.mitre) if simplify: joined_polygons = simplify_polygon(joined_polygons, 0.001) return joined_polygons
def find_outside_polygons(file, campaign_polygons): """ Find polygons outside the campaign's area :param file: the OSM file :type file: fd :param campaign_polygons: campaign's areas as Shapely Polygons :type campaign_polygons: list of Shapely Polygon :return out_ids: ids of nodes and ways that are outside campaign's areas. :rtype: dictionary """ nodes = {} ways = {} in_way = False out_ids = {'nodes': [], 'ways': []} for event, elem in ET.iterparse(file, events=('start', 'end')): # start of a node element # store its id, lon and lat if elem.tag == 'node' and event == 'start': nodes[elem.attrib['id']] = { 'id': elem.attrib['id'], 'lon': elem.attrib['lon'], 'lat': elem.attrib['lat'] } # start of way element if elem.tag == 'way' and event == 'start': # create a new way ways[elem.attrib['id']] = [] in_way = True way_id = elem.attrib['id'] # nd element in a way element if elem.tag == 'nd' and in_way is True: # fetch the node node = nodes[elem.attrib['ref']] ways[way_id].append(node) # end of way element if elem.tag == 'way' and event == 'end': in_way = False # create a shapely polygon polygon = [] for point in ways[way_id]: polygon.append((float(point['lon']), float(point['lat']))) polygon = geometry.Polygon(polygon) # is the polygon in one of the campaign's polygons? is_in = False for campaign_polygon in campaign_polygons: if campaign_polygon.contains(polygon): is_in = True if not is_in: node_ids = list(map(lambda x: x['id'], ways[way_id])) for node_id in node_ids: out_ids['nodes'].append(node_id) out_ids['ways'].append(way_id) return out_ids