Content from Geocoding and OSM


Last updated on 2025-08-11 | Edit this page

Estimated time: 105 minutes

Overview

Questions

  • What are the various geocoding services that can be used?
  • What are some limitations of the geocoding API?
  • What are some categories of places that can be geocoded using the Open Street Map (OSM) API?

Objectives

  • Learn how to use the Census geocoding API
  • Learn how to use the Nominatim and Overpass APIs to access Open Street Map (OSM) data

Introduction


The geocoding API is a service that converts street addresses into geographic coordinates (e.g., 610 Purdue Mall, West Lafayette, IN 47907, into latitudes and longitudes). This can be done either way, converting geographic coordinates back to street addresses. Features of geocoding APIs include:

  1. Geocoding – addresses to geographic coordinates.
  2. Reverse geocoding – geographic coordinates to address.
  3. Region Biasing – constrain results to specific region, county, or postal code.
  4. Place IDs – place ID to address and vice versa.

There are several practical applications of geocoding such as:

  1. Location Based Services: Delivery companies like Uber and DoorDash convert user addresses into GPS coordinates for efficient dispatching.
  2. Mapping and GIS: Real estate platforms use geocoding to display properties of interest on maps.
  3. Emergency Services: Police, fire, and medical services use geocoding for precise identification and to reduce response times by finding the fastest route.

Geocoding APIs also have certain limitations that need to be taken into account when using them. They are designed to convert predefined, static addresses and can result in an error if the address is incorrect. Several APIs may also impose daily usage limits for free access, potentially leading to additional charges if exceeded. Finally, geographic data involving geocoding needs to be constantly updated to avoid inaccuracies over time.

Open Street Map

This tutorial demonstrates how to use the U.S. Census Geocoding API and OpenStreetMap (OSM) via the geopy library to convert addresses into geographic coordinates (latitude and longitude) and plot them using the folium library.

Part 1: Census Geocoding API

This part explores a simple walkthrough of geocoding street addresses using the Census API. The initial setup includes importing essential libraries: - geopy.geocoders: Interfaces with OSM’s Nominatim or Census API to geocode. - geopandas: Manages geospatial data for spatial operations. - pandas: Handles tabular data manipulation. - folium: Creates interactive maps to visualize geocoded locations. - requests: Facilitates HTTP requests to the U.S. Census Geocoding API. - time.sleep: Introduces delays to avoid overwhelming APIs with rapid requests, especially for batch processing.

A function get_coordinates is defined to geocode a single address: - Constructs an API request URL with parameters such as address, benchmark (e.g., Public_AR_Current for up-to-date public addresses), and JSON format. - Sends a request and checks for a successful response (200). - Parses the JSON response to extract coordinates if the address matches. - Returns latitude (y) and longitude (x) if successful, or None if not.

The function is applied to a pandas DataFrame (e.g., reading a museums.txt file with museum names and addresses) to fetch coordinates, which are then added as new columns (Latitude and Longitude) for plotting with folium. Note that inaccurate addresses in the input file may result in None values.

Part 2: Open Street Map User Input Query

This part explores querying OSM for specific location types (e.g., supermarkets like Walmart) in a given area using the Overpass API. The notebook includes the following functions:

  • get_locations: Queries the Overpass API for locations matching specific categories, queries, and cities, with an optional “Brand” filter (e.g., “Walmart”).
    • Creates an empty list all_locations to append addresses.
    • Checks for valid input and stops if invalid.
    • Sends requests and stores results in all_locations.
  • plot_locations and display_locations: Import results (latitude, longitude, and name) from all_locations, displaying them using folium and saving them in a pandas DataFrame for further analysis.

The final part of the code accepts user input queries, which are passed through these functions to get results. Refer to the OSM query keywords webpage for desired outcomes (e.g., supermarkets in Indianapolis). An example use case is fetching coordinates of fast-food chains in West Lafayette by inputting relevant query words. This is reverse geocoding, as it passes a query to grab locations.

Regular geocoding was demonstrated by fetching coordinates based on addresses (e.g., random U.S. museums with their street addresses), passed to the Census Geocoder, which requires precise U.S. addresses to avoid null returns and unplotted locations. Locations of Supermarkets in Indianapolis were plotted using this OSM query approach.

Key Points
  • Geocoding APIs can be used to identify locations of specific categories such as grocery stores or restaurants

Module Overview

Lesson Overview
Beginner Introduction to Geocoding using .txt files and Open Street Map.
Advanced (to be added)

Content from Network Analysis


Last updated on 2025-08-11 | Edit this page

Estimated time: 102 minutes

Overview

Questions

  • How can network analysis be used to assess food desert accessibility in different regions?
  • What factors should be considered when calculating the shortest path between grocery stores and census tracts?
  • How does the quality of OpenStreetMap data impact the accuracy of network analysis for urban planning?

Objectives

  • Demonstrate how to use Python libraries like osmnx and networkx to perform road network analysis for food desert studies.
  • Analyze the relationship between distance, transportation networks, and food accessibility using geospatial data.
  • Evaluate the limitations of network analysis tools and suggest improvements for real-world applications.

Overview


This tutorial provides a practical introduction to performing road network analysis using Python, focusing on analyzing road networks in a specified area (e.g., West Lafayette, Indiana) to study food deserts. It uses libraries such as networkx, osmnx, folium, pandas, geopandas, and matplotlib to fetch, visualize, and analyze road networks, compute centroid nodes, and calculate the shortest path based on travel time. The tutorial also applies this analysis to food accessibility data in Indiana.

The coordinates of grocery stores in Indiana were fetched using OpenStreetMap (OSM). Network analysis is used to calculate distances and times from grocery store locations to the center of areas of interest, considering factors such as the number of supermarkets, income, and vehicle accessibility. Distances are classified as good (around 1 mile) or low-accessible (over 5 miles), depending on rural or urban settings. Low Income and Low Access maps were created for each census tract in Indiana and compared to the USDA food desert dataset.

Why Network Analysis for Food Deserts?

  1. Mapping Accessibility: Models connections between grocery stores and transportation systems to identify areas with limited healthy food access due to distance or lack of transportation.
  2. Area Development: Helps improve accessibility and quality of life in underserved regions.
  3. Promotes Equity: Highlights disparities to create solutions for equitable access to nutritious food.
  4. Optimization of Resources: Ensures equal distribution of resources for all individuals.

Environment Setup

Libraries imported for this tutorial: - osmnx: Fetches and processes OpenStreetMap road network data. - networkx: Performs graph-based computations, such as shortest path calculations. - folium: Enables interactive map visualizations. - geopandas and shapely: Handle geospatial data and geometry operations. - matplotlib: Generates static plots, including network visualization. - geopy: Calculates geodesic distances for spatial analysis.

Data Acquisition

The road network for West Lafayette, Indiana, is fetched using ox.graph.from_place("West Lafayette, Indiana", network_type="drive"), retrieving the drivable road network from OpenStreetMap as a graph (nodes as intersections, edges as road segments). The graph can be saved as a GraphML file (e.g., westlafayette_indiana_network.graphml) using ox.save_graphml to avoid redundant downloads. This can be adapted for any U.S. area with a single line of code.

Applications

  • Urban Planning: Analyzing road connectivity and accessibility in cities.
  • Transportation Studies: Optimizing routes based on travel time or distance.
  • Geospatial Analysis: Studying spatial relationships in infrastructure networks.
  • Emergency Response: Identifying the fastest routes for first responders.

Visualization

A folium map example shows a blue star indicating the centroid of West Lafayette to a random point (green flag), with the shortest path marked by a red line/polygon.

Limitations

  • Data Dependency: Relies on OpenStreetMap data, which may vary in quality or availability by region.
  • Performance: Large networks may require significant computational resources for fetching and processing.
Key Points
  • OSM is a great alternative to map real time locations using Folium feature of Python.

Module Overview

Lesson Overview
Beginner Introduction to Network Analysis and color coding distances.
Advanced Obtains coordinates from OSM and uses centroid analysis to calculate distances and travel times for multiple points of interests.

Content from Spatial Analysis


Last updated on 2025-08-11 | Edit this page

Estimated time: 101 minutes

Overview

Questions

  • How do you write a lesson using Markdown and sandpaper?

Objectives

  • Explain how to use markdown with The Carpentries Workbench
  • Demonstrate how to include pieces of code, figures, and nested challenge blocks

Introduction


This is a lesson created via The Carpentries Workbench. It is written in Pandoc-flavored Markdown for static files and R Markdown for dynamic files that can render code into output. Please refer to the Introduction to The Carpentries Workbench for full documentation.

What you need to know is that there are three sections required for a valid Carpentries lesson:

  1. questions are displayed at the beginning of the episode to prime the learner for the content.
  2. objectives are the learning objectives for an episode displayed with the questions.
  3. keypoints are displayed at the end of the episode to reinforce the objectives.

Inline instructor notes can help inform instructors of timing challenges associated with the lessons. They appear in the “Instructor View”

Challenge

Challenge 1: Can you do it?

What is the output of this command?

R

paste("This", "new", "lesson", "looks", "good")

OUTPUT

[1] "This new lesson looks good"
Challenge

Challenge 2: how do you nest solutions within challenge blocks?

You can add a line with at least three colons and a solution tag.

Figures


You can use standard markdown for static figures with the following syntax:

![optional caption that appears below the figure](figure url){alt='alt text for accessibility purposes'}

Blue Carpentries hex person logo with no text.
You belong in The Carpentries!
Callout

Callout sections can highlight information.

They are sometimes used to emphasise particularly important points but are also used in some lessons to present “asides”: content that is not central to the narrative of the lesson, e.g. by providing the answer to a commonly-asked question.

Math


One of our episodes contains \(\LaTeX\) equations when describing how to create dynamic reports with {knitr}, so we now use mathjax to describe this:

$\alpha = \dfrac{1}{(1 - \beta)^2}$ becomes: \(\alpha = \dfrac{1}{(1 - \beta)^2}\)

Cool, right?

Key Points
  • Use .md files for episodes when you want static content
  • Use .Rmd files for episodes when you need to generate output
  • Run sandpaper::check_lesson() to identify any issues with your lesson
  • Run sandpaper::build_lesson() to preview your lesson locally