Plot shapefile city borders on top of cartopy map

Michael Boles

I'm trying to plot the outline of Bay Area city/town borders on top of a cartopy terrain map using a shapefile obtained here and following this example. For some reason, the borders don't show up, even when I specify borders are on top via zorder. Am I missing something?

# import functions
import matplotlib.pyplot as plt
import cartopy.io.img_tiles as cimgt
import cartopy.crs as ccrs
from cartopy.io.shapereader import Reader
from cartopy.feature import ShapelyFeature

# Create a Stamen terrain background instance
stamen_terrain = cimgt.Stamen('terrain-background')
fig = plt.figure(figsize = (10, 10))
ax = fig.add_subplot(1, 1, 1, projection=stamen_terrain.crs)

# Set range of map, stipulate zoom level
ax.set_extent([-122.7, -121.5, 37.15, 38.15], crs=ccrs.Geodetic())
ax.add_image(stamen_terrain, 12, zorder = 0)

# Add city borders - not working
filename = r'./shapefile/ba_cities.shp' # from https://earthworks.stanford.edu/catalog/stanford-vj593xs7263
shape_feature = ShapelyFeature(Reader(filename).geometries(), ccrs.PlateCarree(), edgecolor='black')
ax.add_feature(shape_feature, zorder = 1)
plt.show()

No shapefile borders! Why?

Michael Boles

As @ImportanceOfBeingErnest and @swatchai suggest, the CRS (coordinate reference system) parameter in ShapelyFeature cartopy.feature.ShapelyFeature() was incorrect.

The proper EPSG (European Petroleum Survey Group?) code can be found in one of the .xml files included with the shapefile:

   <gco:CharacterString>26910</gco:CharacterString>
</code>
<codeSpace>
   <gco:CharacterString>EPSG</gco:CharacterString>

and passing this as the second parameter in ShapelyFeature() is all it takes to get the shapefile to plot the city borders properly:

# Add city borders
filename = r'./shapefile/ba_cities.shp'
shape_feature = ShapelyFeature(Reader(filename).geometries(), ccrs.epsg(26910), 
                               linewidth = 1, facecolor = (1, 1, 1, 0), 
                               edgecolor = (0.5, 0.5, 0.5, 1))
ax.add_feature(shape_feature)
plt.show()

City borders now plotted

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Plot map of a city without google map API in r

分類Dev

R: Gradient plot on a shapefile

分類Dev

Cartopy map fill entire axis

分類Dev

Using ggplot to plot shapefile and gganimate for animation

分類Dev

Add a relief (GEOtiff, .tif) to a ggplot of the swiss country borders ((polygon) shapefile, .shp)

分類Dev

specify non-transparent color for missing data in Cartopy map

分類Dev

How to make top/bottom borders appear ontop of left/right borders crossbrowser css

分類Dev

scroll a div automatically when a child is at the top / bottom borders

分類Dev

Display shapefile map on web browser using angular2

分類Dev

jFreechart Map plot to the next plot value

分類Dev

R load shapefile: "Error in plot.window(...) : need finite 'ylim' values"

分類Dev

ggplot2 moving legend in to plot area without borders around key

分類Dev

Creating custom canvas on top of openlayers map

分類Dev

Creating custom canvas on top of openlayers map

分類Dev

Is it possible to overlay a marker on top of a plotly.js box plot?

分類Dev

Keep map zoom (inside same city) while changing attributes in shiny|mapdeck

分類Dev

Adding borders to triangle built as borders

分類Dev

How to extract x top int values from a map in Golang?

分類Dev

d3 tooltip not showing on top of leaflet map

分類Dev

Plot dynamic heatmap/ color map in while loop Python

分類Dev

How to highlight / plot one latitude line on map ggplot2

分類Dev

Convert Tables in Postgresql to Shapefile

分類Dev

Vertical lines in a polygon shapefile

分類Dev

plotting single 3D point on top of plot_surface in python matplotlib

分類Dev

ggplot2: How to crop out of the blank area on top and bottom of a plot?

分類Dev

How to only plot the top n highest values in stacked bar chart in a pandas df?

分類Dev

How to plot a dataframe with 4 columns that need top be grouped in to just 2 in R?

分類Dev

Cartopy - manual set_extent

分類Dev

Preventing "double" borders in CSS

Related 関連記事

  1. 1

    Plot map of a city without google map API in r

  2. 2

    R: Gradient plot on a shapefile

  3. 3

    Cartopy map fill entire axis

  4. 4

    Using ggplot to plot shapefile and gganimate for animation

  5. 5

    Add a relief (GEOtiff, .tif) to a ggplot of the swiss country borders ((polygon) shapefile, .shp)

  6. 6

    specify non-transparent color for missing data in Cartopy map

  7. 7

    How to make top/bottom borders appear ontop of left/right borders crossbrowser css

  8. 8

    scroll a div automatically when a child is at the top / bottom borders

  9. 9

    Display shapefile map on web browser using angular2

  10. 10

    jFreechart Map plot to the next plot value

  11. 11

    R load shapefile: "Error in plot.window(...) : need finite 'ylim' values"

  12. 12

    ggplot2 moving legend in to plot area without borders around key

  13. 13

    Creating custom canvas on top of openlayers map

  14. 14

    Creating custom canvas on top of openlayers map

  15. 15

    Is it possible to overlay a marker on top of a plotly.js box plot?

  16. 16

    Keep map zoom (inside same city) while changing attributes in shiny|mapdeck

  17. 17

    Adding borders to triangle built as borders

  18. 18

    How to extract x top int values from a map in Golang?

  19. 19

    d3 tooltip not showing on top of leaflet map

  20. 20

    Plot dynamic heatmap/ color map in while loop Python

  21. 21

    How to highlight / plot one latitude line on map ggplot2

  22. 22

    Convert Tables in Postgresql to Shapefile

  23. 23

    Vertical lines in a polygon shapefile

  24. 24

    plotting single 3D point on top of plot_surface in python matplotlib

  25. 25

    ggplot2: How to crop out of the blank area on top and bottom of a plot?

  26. 26

    How to only plot the top n highest values in stacked bar chart in a pandas df?

  27. 27

    How to plot a dataframe with 4 columns that need top be grouped in to just 2 in R?

  28. 28

    Cartopy - manual set_extent

  29. 29

    Preventing "double" borders in CSS

ホットタグ

アーカイブ