Cartopy error when attempting to plot rivers

2024/11/15 13:50:09

When attempting to use Cartopy to plot rivers, I'm getting a URL error. I'm not even sure the rivers feature will plot what I want...I'm attempting to get the Galveston Ship Channel to show on my map.

Here's the error I get:

C:\ProgramData\Anaconda3\lib\site-packages\cartopy\io\__init__.py:260: DownloadWarning: Downloading: https://naciscdn.org/naturalearth/10m/physical/ne_10m_rivers_lake_centerlines.zipwarnings.warn('Downloading: {}'.format(url), DownloadWarning)
Traceback (most recent call last):File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 1354, in do_openh.request(req.get_method(), req.selector, req.data, headers,File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1255, in requestself._send_request(method, url, body, headers, encode_chunked)File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1301, in _send_requestself.endheaders(body, encode_chunked=encode_chunked)File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1250, in endheadersself._send_output(message_body, encode_chunked=encode_chunked)File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1010, in _send_outputself.send(msg)File "C:\ProgramData\Anaconda3\lib\http\client.py", line 950, in sendself.connect()File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1417, in connectsuper().connect()File "C:\ProgramData\Anaconda3\lib\http\client.py", line 921, in connectself.sock = self._create_connection(File "C:\ProgramData\Anaconda3\lib\socket.py", line 787, in create_connectionfor res in getaddrinfo(host, port, 0, SOCK_STREAM):File "C:\ProgramData\Anaconda3\lib\socket.py", line 918, in getaddrinfofor res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

And here's the code:

import matplotlib.pyplot as plt
import datetime as dt
import cartopy.crs as ccrs
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
import cartopy.feature as cfeature
from metpy.plots import USCOUNTIESship_lon, ship_lat = -94.80234, 29.31221ax = plt.axes(projection=ccrs.PlateCarree())
ax.set_extent([-95.5, -94.1, 28.8, 29.8])
ax.add_feature(USCOUNTIES.with_scale('500k'),facecolor='none', edgecolor='gray',linewidth=0.5)ax.add_feature(cfeature.NaturalEarthFeature('physical', 'land', '10m', edgecolor='black', facecolor='#d4d4d4'))
ax.add_feature(cfeature.NaturalEarthFeature('physical', 'ocean', '10m', facecolor='lightblue'))
ax.add_feature(cfeature.NaturalEarthFeature('physical', 'rivers_lake_centerlines', '10m', facecolor='lightblue'))ax.set_title('Hunter-T Approximate Location at Time of Incident',loc='left',fontsize=10,fontweight='bold')
plt.scatter(ship_lon, ship_lat, marker='*', color='black',s=12, zorder=10)
plt.text(ship_lon, ship_lat+.03, 'Vessel Location', fontsize=5, fontweight='bold',horizontalalignment='center')plt.savefig(fname='vessel_location.png',bbox_inches='tight', dpi=600)
plt.close()

If I leave the rivers line out, this is my output. The marker with the vessel location is actually a ship channel. I'm trying to get that body of water to display. I tried searching for a shapefile, but I'm unsure of what shapefile might plot that feature. Or is there a better way to go about plotting a high-res map of this area?

Output Image

Answer

You can try the url it prints in the warning in a browser, and that'll show that it's simply not available at that location. It is when you change https to http. You could try to download and extract it manually to the location as shown in cartopy.config['data_dir'].

But it's probably best to simply update your cartopy version, since it should be downloading the Natural Earth data from AWS, for example from:
https://naturalearth.s3.amazonaws.com/10m_physical/ne_10m_rivers_lake_centerlines.zip

When using cartopy version 0.19 it works fine for me. You do however probably want to change the facecolor to be "none" and only set the edgecolor for the rivers. Since it will otherwise plot the line as if it's a polygon/patch.

ax.add_feature(cfeature.NaturalEarthFeature('physical', 'rivers_lake_centerlines', '10m', linewidth=2, edgecolor='lightblue', facecolor='none',),
)

Zooming out a little to show the rivers more clearly, for me it looks like:

enter image description here

https://en.xdnf.cn/q/119311.html

Related Q&A

Have the address of the client in python

My request is : I have my web pages created in python (wherein there is html code), each pages has a button to go to the next page. Is it possible to get the address of the client when we submit an htm…

try/except issue in Python

I ran the following code as a practice purpose from one of the Python book and I want the output as shown in the URL provided below. So, when I am running the first activity in the question (from book …

Counting the number of vowels in a string using for loops in Python

Python Code:sentence = input(Enter a string:) vowel = A,a,E,e,I,i,O,o,U,u Count = 0 for vowel in sentence:Count += 1 print(There are {} vowels in the string: \{}\.format(Count,sentence))I am trying to …

Sum the values of specific rows if the rows have same values in specific column

I have a data frame like this:a b c 12456 11 123.1 12678 19 345.67 13278 19 1235.345or in another format <table><tr><td>12456</td><td>11</td><td>1…

Plotting Interpolated 3D Data As A 2D Image using Matplotlib

The data set is made of a list dfList containing pandas DataFrames, each DataFrame consisting of the column Y and an identical index column. I am trying to plot all the DataFrames as a 2D plot with pix…

Plotting a flow duration curve for a range of several timeseries in Python

Flow duration curves are a common way in hydrology (and other fields) to visualize timeseries. They allow an easy assessment of the high and low values in a timeseries and how often certain values are …

HTML variable value is not changing in Flask

I have written this code in Flaskans = 999 @app.route(/, methods=[POST, GET]) def home():flag = 0global anssession["ans"] = 0if (request.method == "POST"):jsdata = request.form[data…

How to add two lists with the same amount of indexs in python

I am still new to coding so i apologize for the basic question. How do I add to elements of two seperate lists? listOne = [0, 1 , 7, 8] listTwo = [3, 4, 5, 6] listThree = []for i in listOne:listAdd = …

How to crawl thousands of pages using scrapy?

Im looking at crawling thousands of pages and need a solution. Every site has its own html code - they are all unique sites. No clean datafeed or API is available. Im hoping to load the captured data i…

Object Transmission in Python using Pickle [duplicate]

This question already has answers here:Send and receive objects through sockets in Python(3 answers)Closed last year.I have the following class, a Point objectclass Point:def __init__(self):passdef __i…