Home Artificial Intelligence Geospatial Data Evaluation in Python Introduction Demonstration Conclusion Contacts

Geospatial Data Evaluation in Python Introduction Demonstration Conclusion Contacts

0
Geospatial Data Evaluation in Python
Introduction
Demonstration
Conclusion
Contacts

Getting began with performing geographical data evaluation in Python using OSMnx and Kepler.gl

Photo by Aaron Burden on Unsplash

Geospatial data is ubiquitous and used for many alternative applications across all businesses (e.g. calculating the danger of properties depending on their location, designing recent architecture development, planning shipment of products, and finding possible routes between different locations).

Geospatial data is usually stored in two possible formats: and :

  • Rasters represent data as a matrix of pixels (subsequently having a set resolution). On this representation, each pixel could be assigned a distinct value and multiple grids stacked together could be used in an effort to augment much more the identical image. For instance, the identical image could possibly be stored using 3 channels/bands (e.g. RGB — Red, Green, Blue) or with a single channel.
  • Vectors could be used to abstract geometries of the real-world using elements corresponding to points, lines, polygons, etc… they usually can normally be stored together with some useful metadata concerning the objects they’re representing (e.g. name, address, owner, etc…). Since they’re stored as mathematical objects, it is feasible to zoom in on vectors without compromising resolution.

Vector data is usually stored in file formats corresponding to SVG and Shapefiles, while raster data in TIFF, JPG, PNG, etc…

When working with geospatial data, many alternative types of operations/transformations are often required, some examples are:

  • Conversion from non-tabular/raw binary formats to vector/raster.
  • Bucketing continuous data into discrete categories.
  • Extracting polygons/features from data.
  • Handling no data and outliers.
  • Reprojecting in numerous coordinate systems.
  • Generating lower resolution overviews of information to handle different zoom levels and never overloading rendering of images.

For those who are considering learning specifically how image data could be utilized in Machine Learning systems, you’ll find additional information in my previous article.

As a part of this text, we at the moment are going to walk through a practical demonstration of find out how to analyze geospatial vector data to discover a selected location and calculate the shortest path to succeed in it. All of the code used throughout this text (and more!) is out there on my GitHub and Kaggle accounts.

Initially, we want to ensure we’ve all of the dependencies crucial installed in our surroundings.

pip install osmnx
pip install folium
pip install keplergl

In this instance, we’re going to explore the Altstadt district in Zurich (Switzerland). Making use of it might be as easy as using two lines of code to retrieve and visualize the information we want (Figure 1). OSMnx has actually been designed to fetch and use OpenStreetMap data within the easiest way possible. OpenStreetMap is a free worldwide geographic database maintained by a community of volunteers.

import osmnx as ox
import networkx as nx
import matplotlib.pyplot as plt
import folium
from keplergl import KeplerGl

place_name = "Altstadt, Zurich, Switzerland"

graph = ox.graph_from_place(place_name)

ox.plot_graph_folium(graph)

Figure 1: Graph of the world under examination (Image by Creator).

We at the moment are able to dig into the several ways we are able to retrieve our data. Initially, we are able to get the polygon of the world we’re representing as a base for our exploration. Once retrieved the information, it’s then represented in a data frame containing all the knowledge of interest concerning the area (Figure 2). Geo Pandas is an open-source library specifically designed to increase Pandas capabilities to handle geospatial data.

As a way to create any visual representation, the column is used as a degree of reference, in each row of this column are actually represented all of the coordinates crucial to create an object on a map (e.g. Polygon, Line, Point, MultiPolygon). In this instance, is used because the text markup language for representing our vector geometry objects but other formats can generally be used corresponding to . Moreover, each of those values within the GeoSeries is stored in a in order that to make it as easy as possible to perform operations and transformations on it.

area = ox.geocode_to_gdf(place_name)
area.head()
Figure 2: GeoPandas dataset example (Image by Creator).

At this point, we are able to just repeat the identical procedure to retrieve different points of interest we wish to plot on our map. On this case, let’s imagine we’re in Altstadt, Zurich for a vacation and we wish to look at all the choices we’ve to go to a restaurant. As a way to make our research easier we are able to then get all of the nodes and edges that represent the streets within the districts and all of the buildings and restaurants available in order that we are able to orient ourselves on the map.

buildings = ox.geometries_from_place(place_name, tags={'constructing':True})
restaurants = ox.geometries_from_place(place_name,
tags={"amenity": "restaurant"})
nodes, edges = ox.graph_to_gdfs(graph)

Once we’ve loaded all the information, we are able to then plot each of the characteristics independently (Figure 3).

Figure 3: Area, Buildings, Restaurants, Nodes, and Edges representation (Image by Creator).

Using the code below, this could finally be nicely summarized in the only graph shown below (Figure 4).

fig, ax = plt.subplots(figsize=(10, 14))
area.plot(ax=ax, facecolor='white')
edges.plot(ax=ax, linewidth=1, edgecolor='blue')
buildings.plot(ax=ax, facecolor='yellow', alpha=0.7)
restaurants.plot(ax=ax, color='red', alpha=0.9, markersize=12)
plt.tight_layout()
Figure 4: Evaluation Summary (Image by Creator).

As a way to make our evaluation more interactive, we could then also make use of additional libraries corresponding to . KeplerGL is an open-source Geospatial toolbox developed by Uber to create high-performance web-based geo applications.

Using KeplerGL it might then be very easy for us to overlap our map on an actual worldwide map and apply various transformations and filtering on it on the fly (Figure 5).

K_map = KeplerGl()
K_map.add_data(data=restaurants, name='Restaurants')
K_map.add_data(data=buildings, name='Buildings')
K_map.add_data(data=edges, name='Edges')
K_map.add_data(data=area, name='Area')
K_map.save_to_html()
Figure 5: Interactive Evaluation Summary with KaplerGL (Image by Creator).

Now that we’ve constructed our map and we’ve an interactive tool to look at that data we are able to finally attempt to narrow down our research to a single restaurant. On this case, we first restrict our focus to only Italian restaurants after which select Antica Roma as our place of alternative.

Now, we just have to specify our initial position coordinates and place of destination to begin searching for the most effective path to walk through (Figure 6).

it_resturants = restaurants.loc[restaurants.cuisine.str.contains('italian')
.fillna(False)].dropna(axis=1, how='all')
resturant_choice = it_resturants[it_resturants['name'] == 'Antica Roma']
orig = list(graph)[993]
dest = ox.nearest_nodes(graph,
resturant_choice['geometry'][0].xy[0][0],
resturant_choice['geometry'][0].xy[1][0])
nodes[nodes.index == orig]
Figure 6: Starting Position representation (Image by Creator).

As a way to perform this task, we are able to make use of to routinely run the and check out to optimize our path to reduce its overall length (Figure 7).

route = nx.shortest_path(graph, orig, dest, weight='length')
ox.plot_route_folium(graph, route, route_linewidth=6, node_size=0)
Figure 7: Shortest Path to destination (Image by Creator).

Finally, to validate our research we are able to attempt to perform the identical query on Google Maps and as shown below the outcomes are pretty close to one another! (Figure 8).

Figure 8: Google Maps Shortest Path to destination (Image by Creator).

To summarize, in this text, we first introduced how Geospatial data is used across businesses, the way it is usually stored/processed, and went through a practical example in an effort to discover the shortest paths between two different points. In fact, we are able to perform an analogous kind of research using UI based tools corresponding to the OpenStreetMap App or Google Maps, although learning these foundations can still be extremely invaluable as they could be applied in lots of other types of network-based problems (e.g. Travel Salesman Problem).

If you desire to keep updated with my latest articles and projects follow me on Medium and subscribe to my mailing list. These are a few of my contacts details:

LEAVE A REPLY

Please enter your comment!
Please enter your name here