Select to view content in your preferred language

Unable to install Cartopy in online Notebook

2070
4
04-14-2023 01:35 PM
Labels (1)
PeterDouglas
Emerging Contributor

Hello,

I'm trying to use a sample notebook to generate a report from a feature layer in Arc Online/Field Maps, and run into trouble when I try to import Carotpy.  I get ModuleNotFoundError: No module named "Cartopy'.  After looking around it seems like one solution would be installing conda, though I'm not sure how to do that with an online Notbook and the sample I have does not mention anything about that.  I'm new to python and any direction would be greatly appreciated.  The code for this is:

import cartopy.crs as ccrs
%matplotlib agg
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from cartopy.io.img_tiles import GoogleTiles
​
def create_map(feature):
    fig=plt.figure(figsize=(5,5))
    url = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}.jpg'
    image = GoogleTiles(url=url)
    ax = fig.add_subplot(1,1,1, projection=image.crs)
    ax.add_image(image, 15)
    xmin = feature["geometry"]["x"] - 500
    xmax = feature["geometry"]["x"] + 500
    ymin = feature["geometry"]["y"] - 500
    ymax = feature["geometry"]["y"] + 500
    ax.set_extent([xmin, xmax, ymin, ymax], crs=image.crs)
    ax.scatter(feature["geometry"]["x"], feature["geometry"]["y"],
         color='green', linewidth=2, marker='o'
         )
    io_bytes = io.BytesIO()
    plt.savefig(io_bytes,  format='jpeg', bbox_inches='tight')
    io_bytes.seek(0)
    return base64.b64encode(io_bytes.read()).decode()
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [6], in <cell line: 2>()
      1 get_ipython().run_line_magic('matplotlib', 'agg')
----> 2 import cartopy.crs as ccrs
      3 import matplotlib.pyplot as plt
      4 from cartopy.io.img_tiles import GoogleTiles

ModuleNotFoundError: No module named 'cartopy'
4 Replies
Tyler_M
Esri Contributor

Hi!  Thanks for posting to Esri Community, we're glad to have you here!

It may be easier to install cartopy for importing through the command prompt (if on Windows) or terminal (MacOS/Unix/Linux) rather than directly through the online notebook.

You should be able to use 'pip' instead of the 'conda' command for this in the command line/terminal:

pip install cartopy

 

After that, you should be able to import cartopy in your notebook!

I've attached some documentation on cartopy installation, conda, and pip below.  Have a good day!

Installing cartopy 

conda 

pip 

0 Kudos
Tyler_M
Esri Contributor

Hi!  Just following up.  If you are using an ArcGIS Online notebook and not a separate Jupyter notebook, the 'cartopy' module may be unavailable.

This links to the currently available python libraries in ArcGIS Online notebook.

0 Kudos
xlt208
by Esri Contributor
Esri Contributor

Hello Peter,

If you are running the notebook outside of ArcGIS Online, you can use the information that Tyler provided above to install cartopy in a local Python environment. 

If you are working within ArcGIS Online, when you open a notebook, the Python environment supporting the notebook editor experience already includes conda, so you do not need to install it on your own. However, as Tyler mentioned, the cartopy library is not included by default. You would need to install the library before you can import and use it in ArcGIS Online. 

I just ran the command below in a notebook in ArcGIS Online, and I was able to import cartopy with the library installed. 

 

 

conda install -c conda-forge cartopy

 

 

 

2023-04-18_14-20-11.png

 

 

Please note that it might take a couple of minutes for this command to finish running, and you would need to run this command to install cartopy every time you start a new notebook session. For example, say you saved and closed the notebook for the day, and when you open it the next morning, you would need to run the command again. 

 

Thank you,

Lingtao | Product Engineer for ArcGIS Notebooks

PeterDouglas
Emerging Contributor

This appears to have worked, thank you very much!

0 Kudos