Map API

CS50’s Map API provides programmatic access via HTTP to data from map.harvard.edu.

Buildings

Getting Buildings

GET /map/buildings
Synopsis

Returns a JSON array of objects, each of which represents a building. For example, https://api.cs50.io/map/buildings.

Query Parameters
  • address – If provided, any building whose address contains address will be returned.

  • name – If provided, building whose name contains name will be returned.

Status Codes
  • 200 OK – Always returned, even if no buildings match address or name.

Response JSON Array of Objects
  • address (string) – A building’s (street) address.

  • city (string) – A building’s city (in Massachusetts).

  • geometry (object) – An object with two keys: point, the value of which is an array with two values, each of which is a float, representing a building’s latitude and longitude, respectively; and polygons, which is an array of arrays, each of which represents a polygon that outlines (part of) a building’s footprint, each of whose values is an array with two values, each of which is a float, representing the latitude and longitude of a vertex of the polygon.

  • id (integer) – A building’s unique identifer. Usable as a primary key in a databse.

  • image (string) – URL of a building’s image, if any.

  • name (string) – A building’s name, if any.

Example #1: Getting All Buildings

https://api.cs50.io/map/buildings

import requests

# Get buildings
response = requests.get("https://api.cs50.io/map/buildings")

# Convert JSON to list of dicts
buildings = response.json()

# Print each building's name
for building in buildings:
    print(building["name"])

Example #2: Getting All Wigglesworth Buildings

https://api.cs50.io/map/buildings?name=wigglesworth

import requests

# Get buildings
response = requests.get("https://api.cs50.io/map/buildings?name=wigglesworth")

# Convert JSON to list of dicts
buildings = response.json()

# Print each building's name
for building in buildings:
    print(building["name"])

Example #3: Getting All Buildings on Oxford Street

https://api.cs50.io/map/buildings?address=oxford%20street

import requests

# Get buildings
response = requests.get("https://api.cs50.io/map/buildings?name=oxford%20street")

# Convert JSON to list of dicts
buildings = response.json()

# Print each building's name
for building in buildings:
    print(building["name"])

Getting a Building

GET /map/buildings/(id)
Synopsis

Returns a JSON object that represents a building, where id is that building’s unique identifier. For example, https://api.cs50.io/map/buildings/1358 represents Mather House, while https://api.cs50.io/map/buildings/1145 represents Matthews Hall.

Parameters
  • id – A building’s unique identifier.

Status Codes
  • 200 OK – Returned if a building with id exists.

  • 404 Not Found – Returned if no building with id exists.

Response JSON Object
  • address (string) – A building’s (street) address.

  • city (string) – A building’s city (in Massachusetts).

  • geometry (object) – An object with two keys: point, the value of which is an array with two values, each of which is a float, representing a building’s latitude and longitude, respectively; and polygons, which is an array of arrays, each of which represents a polygon that outlines (part of) a building’s footprint, each of whose values is an array with two values, each of which is a float, representing the latitude and longitude of a vertex of the polygon.

  • id (integer) – A building’s unique identifer. Usable as a primary key in a databse.

  • image (string) – URL of a building’s image, if any.

  • name (string) – A building’s name, if any.

Example #1: Getting Mather House

https://api.cs50.io/map/buildings/1358

import requests

# Get building
response = requests.get("https://api.cs50.io/map/buildings/1358")

# Convert JSON to dict
building = response.json()

# Print building's name
print(building["name"])

Acknowledgements

Special thanks to CS50’s friends at Harvard’s Center for Geographic Analysis for this API’s data!