Interactively Edit Geographic Data in Jupyter Notebook

Did you know you can manipulate vector data in a Jupyter Notebook environment with drawing tools? Follow this step by step to discover how.

Interactively Edit Geographic Data in Jupyter Notebook
Photo by Glenn Carstens-Peters

Did you know you can directly edit and add vector data while analyzing and working it within a Jupyter-Notebook? That's right.

In most cases, people analyze data with interactive Notebooks and when it comes to making a few changes, Pandas is usually the tool for the job. This article reduces the amount of code one needs to write in order to manipulate vector data.

I will introduce a python package, leafmap.

Leafmap is a Python package for interactive mapping and geospatial analysis with minimal coding in a Jupyter environment.

The list of Geospatial libraries in Python is almost impossible to finish. A good thing is that most of them are usually open-source geospatial libraries.

You can check out my previous article on some of these packages just to get a glimpse.

Open-Source Python Geospatial Libraries to Explore
Open-Source has become the most common reason for rapid software development. Here is a list of Open-Source Geospatial Libraries that you can leverage to build geospatial applications with Python.

Like most packages, you need to install them first. Without installation, it's like trying to drive a car without fuel👀.

Within your JupyterNotebook, within a cell type !pip install leafmap and let the package install itself.

Some prefer or a familiar with Conda, you can run conda install -c conda-forge leafmap in the terminal.

Once that has been executed successfully, Leafmap will also include all its dependencies, so if you are already running JupyterNotebooks for sometime, do not worry about the warning messages.

💡
Also, make sure you have GeoPandas installed. You can do this with a pip install geopandas the same way you installed leafmap

When the fuel is in, we need to tell our Jupyter Environment that we need to use our leafmap package.

To do that, we import leafmap like so;

from leafmap import leafmap

Let's create an interactive map now.

m = leafmap.Map(center=(-19.0154,29.1549), zoom=12)
m.add_basemap()
m

Let's add some vector data to our map. For this, we will use data in GeoJSON format from one of my Open-Data repositories.

GitHub - Surveyor-Jr/Zimbabwe-Open-Data: A container repository for all Open and Public Data in Zimbabwe
A container repository for all Open and Public Data in Zimbabwe - GitHub - Surveyor-Jr/Zimbabwe-Open-Data: A container repository for all Open and Public Data in Zimbabwe

We will grab the Zimbabwe Districts GeoJSON file for this visualization.

url = "https://raw.githubusercontent.com/Surveyor-Jr/Zimbabwe-Open-Data/main/adminstrative_boundaries/zimbabwe_districts.geojson"
m.edit_vector(url)

This will add Edit privileges to the vector data already added within the notebook.

Use the drawing tools and once you are done, you can then save the result vector data with the code below.

m.save_draw_features("my_vector/data.geojson")

Where my_vector is the folder to which you want to store your final outputs.

Hope this will help out in manipulating Vector data in a notebook environment.

You can also check out more information about the leafmap package.

Check out the source code for this article in the PyBlog repository.

GitHub - Surveyor-Jr/PyBlog: A repository containing source code that I use for creating online tutorials, blog posts, articles and e-books on different and several platforms.
A repository containing source code that I use for creating online tutorials, blog posts, articles and e-books on different and several platforms. - GitHub - Surveyor-Jr/PyBlog: A repository conta...