Repository: networkx/networkx

Issue:
contextily.add_basemap failing in some geospatial gallery examples
Followup issue to #7299 - the `contextily.add_basemap` had been intermittently failing in some of the geospatial gallery examples, e.g. `plot_lines.py` and `plot_delaunay.py`. These have been temporarily renamed to stop them from running during the doc build to avoid CI blockers (#7299). I haven't looked into it in detail, but my initial guess would be it has to do with a network timeout or some other issue accessing resources over the web. A full traceback can be found at the [end of this log](https://github.com/networkx/networkx/actions/runs/7916641025/job/21610909161#step:7:3889).

@ljwolf I know this isn't specifically a geopandas issue, but is this something you've encountered before?

#7299 should be reverted once a fix is found!


Discussion:
I haven't seen this error before, but it looks like something easy to replicate/fix. I'll take a look. Also, tagging @martinfleis in case he can get to it sooner than me! 
That is the same issue that showed up in our CI today https://github.com/geopandas/contextily/actions/runs/7909639251/job/21591004622 It seems to be cause by some network issue and, in our case, simply re-triggering CI fixed it. 

Have you encountered the issue in any other case than that single time linked above?
> It seems to be cause by some network issue and, in our case, simply re-triggering CI fixed it.

Yeah that's what I suspected.

> Have you encountered the issue in any other case than that single time linked above?

Unfortunately yes. It happens less frequently in the github workflows but has been pretty consistent circleci. Based on your experience it seems like we might need to tweak a timeout somewhere to make this more reliable... I'll look into it when I get a chance. Thanks for connections & info @ljwolf @martinfleis !
I've seen this one first time today, so I'm not entirely sure what's causing it. The easiest solution may be changing the source of the baseline that is used. If it is caused by the server that is serving the tiles, different one might be a solution. 

Are you aware of any difference between CircleCI and GHA that could cause this?
> Are you aware of any difference between CircleCI and GHA that could cause this?

No, not concretely though different runtime limits might have an effect. In further testing I was able to also intermittently reproduce it locally so CI configuration is not the root cause.
I'll have a look if we can build some additional robustness to contextily. We do have retries on request but if the request returns invalid data like in this case, we do not try to fetch the tile again. I suppose we should.

Resolving patch:
diff --git a/examples/geospatial/delaunay.py b/examples/geospatial/plot_delaunay.py
similarity index 96%
rename from examples/geospatial/delaunay.py
rename to examples/geospatial/plot_delaunay.py
--- a/examples/geospatial/delaunay.py
+++ b/examples/geospatial/plot_delaunay.py
@@ -58,7 +58,11 @@
 
 # Now, we can plot with a nice basemap.
 ax = cells.plot(facecolor="lightblue", alpha=0.50, edgecolor="cornsilk", linewidth=2)
-add_basemap(ax)
+try:  # Try-except for issues with timeout/parsing failures in CI
+    add_basemap(ax)
+except:
+    pass
+
 ax.axis("off")
 nx.draw(
     delaunay_graph,
diff --git a/examples/geospatial/lines.py b/examples/geospatial/plot_lines.py
similarity index 94%
rename from examples/geospatial/lines.py
rename to examples/geospatial/plot_lines.py
--- a/examples/geospatial/lines.py
+++ b/examples/geospatial/plot_lines.py
@@ -76,7 +76,10 @@
 for i, facet in enumerate(ax):
     facet.set_title(("Streets", "Graph")[i])
     facet.axis("off")
-    add_basemap(facet)
+    try:  # For issues with downloading/parsing in CI
+        add_basemap(facet)
+    except:
+        pass
 nx.draw(
     G_primal, {n: [n[0], n[1]] for n in list(G_primal.nodes)}, ax=ax[1], node_size=50
 )
@@ -92,7 +95,10 @@
 for i, facet in enumerate(ax):
     facet.set_title(("Streets", "Graph")[i])
     facet.axis("off")
-    add_basemap(facet)
+    try:  # For issues with downloading/parsing in CI
+        add_basemap(facet)
+    except:
+        pass
 nx.draw(G_dual, {n: [n[0], n[1]] for n in list(G_dual.nodes)}, ax=ax[1], node_size=50)
 plt.show()
 
diff --git a/examples/geospatial/plot_points.py b/examples/geospatial/plot_points.py
--- a/examples/geospatial/plot_points.py
+++ b/examples/geospatial/plot_points.py
@@ -51,7 +51,10 @@
 f, ax = plt.subplots(1, 2, figsize=(8, 4))
 for i, facet in enumerate(ax):
     cases.plot(marker=".", color="orangered", ax=facet)
-    add_basemap(facet)
+    try:  # For issues with downloading/parsing basemaps in CI
+        add_basemap(facet)
+    except:
+        pass
     facet.set_title(("KNN-3", "50-meter Distance Band")[i])
     facet.axis("off")
 nx.draw(knn_graph, positions, ax=ax[0], node_size=5, node_color="b")


Test patch: