Reputation: 49
I'm brand new (as of a week ago) to the OSMnx package and a semester into learning Python / GIS, so feel free to assume shocking ignorance on my part.
My overall goal is to learn more about how to implement OSMnx to get OpenStreetMap (OSM) data on the city level. My secondary to create a bit of Python code that extracts OSM data that I can use as boundary data for Kernel Density Estimates in urban areas.
My first goal is to get a polygon shapefile of Amherst, MA and save it to my computer.
tag_bound = {"boundary":"administrative","admin_level":7,"area":True}
amherst_bound = ox.geometries.geometries_from_place(place_name,tag_bound, which_result=None, buffer_dist=None)
type(amherst_bound)
# Check to make sure it looks reasonable by plotting it. See https://geoffboeing.com/2016/11/osmnx-python-street-networks/
ox.project_gdf(amherst_bound).plot()
_ = amherst_bound.axis('off')
# Save the file
amherst_bound.save_gdf_shapefile(filename= r"C:\Users\afhal\Dropbox\Arboriculture\PythonGIS\finalproject\testshape.shp")
Two parts of the code have an error:
- Plotting Attribute Error
AttributeError Traceback (most recent call last) in () 29 type(amherst_bound) 30 ox.project_gdf(amherst_bound).plot() ---> 31 _ = amherst_bound.axis('off') 32 33 amherst_bound.save_gdf_shapefile(filename= r"C:\Users\afhal\Dropbox\Arboriculture\PythonGIS\finalproject\testshape.shp")
C:\Users\afhal.conda\envs\OpenStreetMap\lib\site-packages\pandas\core\generic.py in getattr(self, name) 5137 if self._info_axis._can_hold_identifiers_and_holds_name(name): 5138
return self[name] -> 5139 return object.getattribute(self, name) 5140 5141 def setattr(self, name: str, value) -> None:AttributeError: 'GeoDataFrame' object has no attribute 'axis'
(Still produces this, which looks weird: )
AttributeError Traceback (most recent call last) in () 31 #_ = amherst_bound.axis('off') 32 ---> 33 amherst_bound.save_gdf_shapefile(filename= r"C:\Users\afhal\Dropbox\Arboriculture\PythonGIS\finalproject\testshape.shp") 34 35
C:\Users\afhal.conda\envs\OpenStreetMap\lib\site-packages\pandas\core\generic.py in getattr(self, name) 5137 if self._info_axis._can_hold_identifiers_and_holds_name(name): 5138
return self[name] -> 5139 return object.getattribute(self, name) 5140 5141 def setattr(self, name: str, value) -> None:AttributeError: 'GeoDataFrame' object has no attribute 'save_gdf_shapefile'
I keep seeing people using DiGraphs instead of gdb (OSMnx: Creating Custom Queries with Alternative Infrastructures and GeoPandas and OSMnx- plotting on map), but I'm not sure why that would be preferred.
Where am I going wrong with my plotting and saving?
Upvotes: 1
Views: 3819
Reputation: 6442
You have undefined variables in your code snippet, so it's not reproducible, but I'll try to make some guesses as to what you're doing and aiming for. First off, if you're new to the OSMnx package, make sure you take the time to work through its usage examples, as they demonstrate how to do everything you're trying to do. Then consult the documentation for further details. As OSMnx lets you work with OpenStreetMap data in NetworkX and Geopandas, make sure you're familiar with those packages too.
My first goal is to get a polygon shapefile of Amherst, MA and save it to my computer.
If you want the (polygon) administrative boundaries of Amherst, just geocode that city name to a GeoDataFrame as described in the examples and documentation. Then you can save your GeoDataFrame as a shapefile the normal Geopandas way:
import osmnx as ox
ox.config(use_cache=True, log_console=True)
gdf = ox.geocode_to_gdf('Amherst, MA')
gdf.to_file('amherst-shapefile')
And if you want to plot your GeoDataFrame:
ax = gdf.plot()
_ = ax.axis('off')
Make sure you read through the (linked above) usage examples and documentation so you know how to use OSMnx. If you're unfamiliar with Geopandas/NetworkX, make sure you do the same for those packages as well.
Upvotes: 0