Python PyCharm - cannot install contextily

108 views Asked by At

I am using Python with PyCharm and I am trying to run this code that I found here:

from libpysal import weights, examples
from contextily import add_basemap
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import geopandas

cases = geopandas.read_file("cholera_cases.gpkg")

coordinates = np.column_stack((cases.geometry.x, cases.geometry.y))

knn3 = weights.KNN.from_dataframe(cases, k=3)

dist = weights.DistanceBand.from_array(coordinates, threshold=50)

knn_graph = knn3.to_networkx()
dist_graph = dist.to_networkx()

positions = dict(zip(knn_graph.nodes, coordinates))

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)
    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")
nx.draw(dist_graph, positions, ax=ax[1], node_size=5, node_color="b")
plt.show()

The problem is that, after I installed the imported libraries, I get:

Traceback (most recent call last):
  File "...my.path...", line 14, in <module>
    from contextily import add_basemap
ModuleNotFoundError: No module named 'contextily'

If I try to install the missing module (pip install contextily) I get:

...
Collecting rasterio
  Using cached rasterio-1.2.10.tar.gz (2.3 MB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error

  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [2 lines of output]
      INFO:root:Building on Windows requires extra options to setup.py to locate needed GDAL files. More information is available in the README.
      ERROR: A GDAL API version must be specified. Provide a path to gdal-config using a GDAL_CONFIG environment variable or use a GDAL_VERSION environment variable.
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

I need your help: the answers from related questions did not work well. What can I do?

1

There are 1 answers

1
user16171413 On

According to PyPi, Rasterio 1.3 works with Python 3.8+, Numpy 1.18+, and GDAL 3.1+. PyPi also lists the python versions that are currently supported by the latest version of rasterio (1.3.9) i.e. Python 3.8 - 3.11.

Try upgrading your python version to any of these supported versions. You may want to refer here https://pypi.org/project/rasterio/ for more information.