def test_pois(): import pytest # download all points of interests from place gdf = ox.pois_from_place(place='Kamppi, Helsinki, Finland') # get all restaurants and schools from place restaurants = ox.pois_from_place(place='Emeryville, California, USA', amenities=['restaurant']) schools = ox.pois_from_place(place='Emeryville, California, USA', amenities=['school']) # get all universities from Boston area (with 2 km buffer to cover also Cambridge) boston_q = "Boston, Massachusetts, United States of America" boston_poly = ox.gdf_from_place(boston_q, buffer_dist=2000) universities = ox.pois_from_polygon(boston_poly.geometry.values[0], amenities=['university']) # by point and by address restaurants = ox.pois_from_point(point=(42.344490, -71.070570), distance=1000, amenities=['restaurant']) restaurants = ox.pois_from_address(address='Emeryville, California, USA', distance=1000, amenities=['restaurant']) # should raise an exception # polygon or -north, south, east, west- should be provided with pytest.raises(ValueError): ox.create_poi_gdf(polygon=None, north=None, south=None, east=None, west=None) gdf = ox.pois_from_place(place='kusatsu, shiga, japan', which_result=2)
def GenerateOSMPOIs(self): df = ox.pois_from_polygon(polygon=self.bbox, amenities=self.current_amenity) points = df.copy() points = points.loc[points['element_type'] == 'node'] polygons = df.copy() polygons = polygons.loc[polygons['element_type'] == 'way'] polygons['geometry'] = polygons.centroid multipolys = df.copy() multipolys = multipolys.loc[multipolys['element_type'] == 'relation'] multipolys['geometry'] = multipolys['geometry'].apply( lambda x: self.RelationtoPoint(x)) df = pd.concat([ pd.DataFrame(points), pd.DataFrame(polygons), pd.DataFrame(multipolys) ], ignore_index=True) self.df = df return df
def test_pois(): tags = {"amenity": True, "landuse": ["retail", "commercial"], "highway": "bus_stop"} gdf = ox.pois_from_place(place1, tags=tags) gdf = ox.pois_from_polygon(polygon, tags={"amenity": "library"}) cs = '[out:json][timeout:180][date:"2019-10-28T19:20:00Z"]' gdf = ox.pois_from_address(address, tags={"amenity": "school"}, custom_settings=cs) gdf = ox.pois_from_point( location_point, dist=500, tags={"amenity": "restaurant"}, timeout=200, memory=100000 )
def test_pois(): # download all points of interests from place gdf = ox.pois_from_place(place='Kamppi, Helsinki, Finland') # Get all restaurants and schools from place restaurants = ox.pois_from_place(place='Emeryville, California, USA', amenities=['restaurant']) schools = ox.pois_from_place(place='Emeryville, California, USA', amenities=['school']) # Get all Universities from Boston area (with 2 km buffer to cover also Cambridge) boston_q = "Boston, Massachusetts, United States of America" boston_poly = ox.gdf_from_place(boston_q, buffer_dist=2000) universities = ox.pois_from_polygon(boston_poly.geometry.values[0], amenities=['university'])
def test_pois(): # download all points of interests from place gdf = ox.pois_from_place(place='Kamppi, Helsinki, Finland') # get all restaurants and schools from place restaurants = ox.pois_from_place(place='Emeryville, California, USA', amenities=['restaurant']) schools = ox.pois_from_place(place='Emeryville, California, USA', amenities=['school']) # get all universities from Boston area (with 2 km buffer to cover also Cambridge) boston_q = "Boston, Massachusetts, United States of America" boston_poly = ox.gdf_from_place(boston_q, buffer_dist=2000) universities = ox.pois_from_polygon(boston_poly.geometry.values[0], amenities=['university']) # by point and by address restaurants = ox.pois_from_point(point=(42.344490, -71.070570), distance=1000, amenities=['restaurant']) restaurants = ox.pois_from_address(address='Emeryville, California, USA', distance=1000, amenities=['restaurant'])
def test_pois(): tags = { 'amenity': True, 'landuse': ['retail', 'commercial'], 'highway': 'bus_stop' } gdf = ox.pois_from_place(place='Piedmont, California, USA', tags=tags) poly = ox.gdf_from_place('Boston, MA, USA', buffer_dist=2000) gdf = ox.pois_from_polygon(poly['geometry'].iloc[0], tags={'amenity': 'university'}) gdf = ox.pois_from_address( address='Piedmont, California, USA', tags={'amenity': 'school'}, custom_settings='[out:json][timeout:180][date:"2019-10-28T19:20:00Z"]') gdf = ox.pois_from_point(point=(42.344490, -71.070570), distance=500, tags={'amenity': 'restaurant'}, timeout=200, memory=100000)
def __init__( self, hazard: GeoDataFrame, output_path: str, domain: Optional[Polygon] = None, target_types: Iterable[str] = tuple(['school']), network: Optional[Graph] = None, targets: Optional[GeoDataFrame] = None, target_capacity: int = 100, agents: Optional[GeoDataFrame] = None, seed: Optional[int] = None): super().__init__() self._seed = seed self.output_path = output_path self.hazard = hazard self.schedule = RandomActivation(self) self.target_capacity = target_capacity if network is None: self.G = osmnx.graph_from_polygon(domain, simplify=False) self.G = self.G.to_undirected() else: self.G = network self.nodes: GeoDataFrame self.edges: GeoDataFrame self.nodes, self.edges = osmnx.save_load.graph_to_gdfs(self.G) if agents is None: agents = GeoDataFrame(geometry=create_footprints_gdf(domain).centroid) if targets is None: targets = osmnx.pois_from_polygon(domain, amenities=list(target_types)) # Query can return polygons as well as points, only using the points targets = targets[targets.geometry.geom_type == 'Point'] output_gpkg = output_path + '.gpkg' driver = 'GPKG' targets.crs, agents.crs = [self.nodes.crs] * 2 nodes_tree = cKDTree(np.transpose([self.nodes.geometry.x, self.nodes.geometry.y])) # Prevents warning about CRS not being the same self.hazard.crs = self.nodes.crs self.hazard.to_file(output_gpkg, layer='hazard', driver=driver) agents_in_hazard_zone: GeoDataFrame = sjoin(agents, self.hazard) agents_in_hazard_zone = agents_in_hazard_zone.loc[~agents_in_hazard_zone.index.duplicated(keep='first')] agents_in_hazard_zone.geometry.to_file(output_gpkg, layer='agents', driver=driver) assert len(agents_in_hazard_zone) > 0, 'There are no agents within the hazard zone' targets_in_hazard_zone: GeoDataFrame = sjoin(targets, self.hazard) targets_in_hazard_zone = targets_in_hazard_zone.loc[~targets_in_hazard_zone.index.duplicated(keep='first')] targets_outside_hazard_zone = targets[~targets.index.isin(targets_in_hazard_zone.index.values)] targets_outside_hazard_zone.to_file(output_gpkg, layer='targets', driver=driver) assert len(targets_outside_hazard_zone) > 0, 'There are no targets outside the hazard zone' _, node_idx = nodes_tree.query( np.transpose([agents_in_hazard_zone.geometry.x, agents_in_hazard_zone.geometry.y])) _, target_node_idx = nodes_tree.query( np.transpose([targets_outside_hazard_zone.geometry.x, targets_outside_hazard_zone.geometry.y])) for (_, row), nearest_node in zip(targets_outside_hazard_zone.iterrows(), self.nodes.index[target_node_idx]): if not self.G.has_node(row.osmid): self.G.add_edge(nearest_node, row.osmid, length=0) self.G.nodes[row.osmid]['osmid'] = row.osmid self.G.nodes[row.osmid]['x'] = row.geometry.x self.G.nodes[row.osmid]['y'] = row.geometry.y self.nodes, self.edges = osmnx.save_load.graph_to_gdfs(self.G) self.nodes[['osmid', 'geometry']].to_file(output_gpkg, layer='nodes', driver=driver) self.edges[['osmid', 'geometry']].to_file(output_gpkg, layer='edges', driver=driver) output_gml = output_path + '.gml' osmnx.nx.write_gml(self.G, path=output_gml) self.igraph = igraph.read(output_gml) self.target_nodes = targets_outside_hazard_zone.osmid self.grid = NetworkGrid(self.G) # Create agents for i, idx in enumerate(node_idx): a = agent.EvacuationAgent(i, self) self.schedule.add(a) self.grid.place_agent(a, self.nodes.index[idx]) a.update_route() a.update_location() self.data_collector = DataCollector( model_reporters={ 'evacuated': evacuated, 'stranded': stranded }, agent_reporters={'position': 'pos', 'reroute_count': 'reroute_count', 'lat': 'lat', 'lon': 'lon', 'highway': 'highway', 'status': status})
def create_poi_gdf(MAREA=None): '''Downloads public transport, parks, and urban amenities from OpenStreetMap, classifies them into seven distinct categories, and outputs a point geodataframe. Args: MAREA (GeoDataFrame) : Polygon of boundaries of a given city. Returns: gdf (GeoDataFrame) : Geodataframe consisting of points of interests for a given city. ''' ##public transport #get public transport (rail) network of metro vancouver rail = ox.graph_from_polygon(MAREA.iloc[0, 0], network_type='all', infrastructure='way["railway"]', retain_all=True) #make rail pois rail_gdf = ox.save_load.graph_to_gdfs(rail, nodes=True, edges=False, node_geometry=True, fill_edge_geometry=True) rail_gdf['amenity'] = 'transit_stop' rail_gdf = rail_gdf[['amenity', 'geometry']] #get public bus stops bus = ox.footprints.footprints_from_polygon( polygon=MAREA.iloc[0, 0], footprint_type='public_transport') bus['amenity'] = 'public_transport' bus['centroid'] = bus.centroid bus = bus.set_geometry('centroid') bus = bus[['amenity', 'centroid']] bus = bus.rename(columns={"centroid": "geometry"}) ##amenities pois # define your selected amenities amenities = [ "transit_stop", "bus_station", "bus_stop", "bicycle_parking", "gym", "park", "pub", "bar", "theatre", "cinema", "nightclub", "events_venue", "restaurant", "cafe", "food_court", "marketplace", "community_centre", "library", "social_facility", "social_centre", "townhall", "school", "childcare", "child_care", "kindergarten", "university", "college", "pharmacy", "dentist", "clinic", "hospital", "doctors", "bank" ] #request amenities from the OpenStreetMap API (Overpass) pois = ox.pois_from_polygon(MAREA.iloc[0, 0]) pois = pois[pois['amenity'].isin(amenities)] pois = pois[['amenity', 'geometry']] #get parks and "leisure" elements leisure = [ "fitness_centre", "sports_centre", "park", "pitch", "playground", "swimming_pool", "garden", "golf_course", "sports_centre", "ice_rink", "dog_park", "nature_reserve", "fitness_centre", "marina", "recreation_ground", "fitness_station", "skate_park" ] parks = ox.footprints.footprints_from_polygon(polygon=MAREA.iloc[0, 0], footprint_type='leisure') parks = parks[parks['leisure'].isin(leisure)] parks['centroid'] = parks.centroid parks = parks.set_geometry('centroid') parks = parks[['leisure', 'centroid']] parks = parks.rename(columns={ "leisure": "amenity", "centroid": "geometry" }) #merge all the dataframes together #merge dataframes together pois_list = [pois, rail_gdf, bus, parks] pois_all = pd.concat(pois_list, axis=0, ignore_index=True) #create x and y columns for network analysis pois_all['x'] = pois_all.centroid.x pois_all['y'] = pois_all.centroid.y pois_all['centroid'] = pois_all.centroid pois_all = pois_all.set_geometry('centroid') pois_all = pois_all[['amenity', 'x', 'y', 'centroid']] pois_all = pois_all.rename(columns={"centroid": "geometry"}) pois_all = pois_all.set_geometry('geometry') return pois_all