Trying to draw map of a particular city with Cartopy

2k views Asked by At

I am trying to get a map of San Francisco using Cartopy. I got the map of California but have no clue how to draw map of a particular city. Is it even possible to get a map of only San Francisco?

My code:

import pandas
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader

data = pandas.read_csv('train_200.csv',header=None)
lats,lons = data[7],data[8]

ax = plt.axes([0, 0, 1, 1],
              projection=ccrs.LambertConformal())

ax.set_extent([-138, -110.5, 32, 40], ccrs.Geodetic())    
shapename = 'admin_1_states_provinces_shp'
states_shp = shpreader.natural_earth(resolution='10m',
                                     category='cultural', name=shapename)

for i in shpreader.Reader(states_shp).records():
    if i.attributes['postal'] == "CA":
        print i.attributes
        ax.add_geometries([i.geometry], ccrs.PlateCarree(),facecolor=[0.9375, 0.9375, 0.859375], edgecolor='black')
    else:
        pass
ax.scatter(lats,lons,transform=ccrs.PlateCarree(),zorder=100)
plt.show()

Output map:

Map Of California

The first few outputs for shpreader.Reader(states_shp).records().attributes:

{'labelrank': 7, 'sr_adm0_a3': 'BDI', 'region_cod': '                                                  ', 'adm0_sr': 1, 'gadm_level': 1, 'scalerank': 9, 'Shape_Area': '1.46386727466e-001', 'type_en': 'Province', 'datarank': 8, 'iso_3166_2': 'BI-', 'wikipedia': '                                                                                                                                                                                                                                                              ', 'note': '                                                                                                                                                                                                                                                              ', 'big_code': '                                                  ', 'hasc_maybe': '                                                  ', 'type': 'Province', 'region_big': '                                                                                                                                                                                                        ', 'provnum_ne': 20005, 'mapcolor13': 8, 'code_hasc': 'BI.CA', 'name_alt': '                                                                                                                                                                                                        ', 'adm1_code_': 'BDI-2632', 'code_local': '                                                  ', 'name_local': '                                                                                                                                                                                                        ', 'check_me': 0, 'Shape_Leng': '1.77905469919e+000', 'iso_a2': 'BI', 'featurecla': 'Admin-1 scale rank', 'postal': 'CA', 'sr_sov_a3': 'BDI', 'admin0_lab': 2, 'name_len': 7, 'area_sqkm': '0.00000e+000', 'name': 'Cankuzo', 'admin': 'Burundi', 'region': '                                                                                                    ', 'abbrev': '          ', 'sameascity': 7, 'mapcolor9': 5, 'adm1_code': 'BDI-2632', 'diss_me': 2632}
{'labelrank': 5, 'sr_adm0_a3': 'COL', 'region_cod': '                                                  ', 'adm0_sr': 1, 'gadm_level': 1, 'scalerank': 5, 'Shape_Area': '2.49683494720e+000', 'type_en': 'Department', 'datarank': 5, 'iso_3166_2': 'CO-', 'wikipedia': '                                                                                                                                                                                                                                                              ', 'note': '                                                                                                                                                                                                                                                              ', 'big_code': '                                                  ', 'hasc_maybe': '                                                  ', 'type': 'Departamento', 'region_big': '                                                                                                                                                                                                        ', 'provnum_ne': 2, 'mapcolor13': 1, 'code_hasc': 'CO.CA', 'name_alt': '                                                                                                                                                                                                        ', 'adm1_code_': 'COL-1404', 'code_local': '                                                  ', 'name_local': '                                                                                                                                                                                                        ', 'check_me': 0, 'Shape_Leng': '1.26131409274e+001', 'iso_a2': 'CO', 'featurecla': 'Admin-1 scale rank', 'postal': 'CA', 'sr_sov_a3': 'COL', 'admin0_lab': 2, 'name_len': 5, 'area_sqkm': '0.00000e+000', 'name': 'Cauca', 'admin': 'Colombia', 'region': '                                                                                                    ', 'abbrev': '          ', 'sameascity': -99, 'mapcolor9': 3, 'adm1_code': 'COL-1404', 'diss_me': 1404}
{'labelrank': 9, 'sr_adm0_a3': 'CRI', 'region_cod': '                                                  ', 'adm0_sr': 1, 'gadm_level': 1, 'scalerank': 8, 'Shape_Area': '2.55427502099e-001', 'type_en': 'Province', 'datarank': 8, 'iso_3166_2': 'CR-', 'wikipedia': '                                                                                                                                                                                                                                                              ', 'note': '                                                                                                                                                                                                                                                              ', 'big_code': '                                                  ', 'hasc_maybe': '                                                  ', 'type': 'Provincia', 'region_big': '                                                                                                                                                                                                        ', 'provnum_ne': 6, 'mapcolor13': 2, 'code_hasc': 'CR.CA', 'name_alt': '                                                                                                                                                                                                        ', 'adm1_code_': 'CRI-1327', 'code_local': '                                                  ', 'name_local': '                                                                                                                                                                                                        ', 'check_me': 0, 'Shape_Leng': '2.55929902160e+000', 'iso_a2': 'CR', 'featurecla': 'Admin-1 scale rank', 'postal': 'CA', 'sr_sov_a3': 'CRI', 'admin0_lab': 2, 'name_len': 7, 'area_sqkm': '0.00000e+000', 'name': 'Cartago', 'admin': 'Costa Rica', 'region': '                                                                                                    ', 'abbrev': '          ', 'sameascity': 9, 'mapcolor9': 4, 'adm1_code': 'CRI-1327', 'diss_me': 1327}
1

There are 1 answers

0
swatchai On BEST ANSWER

With cartopy.io.shapereader.natural_earth(), you get the admin levels down to states/provinces in each country (with admin_1). Not detailed enough to county/city/district level in any country. In your case, you need to get data from other sources. Try download USA from http://www.gadm.org/country. You will get a zip file of admin 0-3 for that particular country you choose to download. Once you get the shape files in your local folder, you can read it with:

as_shp = cartopy.io.shapereader.Reader( 'path_to/shape_files/XXX_adm2.shp' )

as_shp is an iterator to grab every record in the shape file.