NYC Open Data Examples




Pay Notebook Creator: Roy Hyunjin Han0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0

Define Geometry from Addresses in New York City

Use this tool to convert a list of NYC addresses into a LineString or Polygon in WKT format. If you want to specify addresses outside NYC, use this tool or notebook.

{ address_table : Addresses in New York City }

In [ ]:
# CrossCompute
target_folder = '/tmp'
address_table_path = 'addresses.csv'
In [ ]:
import pandas as pd
address_table = pd.read_csv(address_table_path)
address_table[:2]
In [ ]:
import requests
from pandas import Series

base_url = 'https://api.cityofnewyork.us/geoclient/v1/address.json'
# Register for your own NYC Geoclient API key at https://developer.cityofnewyork.us/api/geoclient-api
geoclient_id = '89f55ba2'
geoclient_key = '3c19c2a1701bf0814a81acb99782f8ea'

def expand_row(row):
    response = requests.get(base_url, {
        'houseNumber': row['House Number'],
        'street': row['Street Name'],
        'borough': row['Borough Name'],
        'app_id': geoclient_id,
        'app_key': geoclient_key,
    })
    response_json = response.json()
    d = response_json['address']
    return Series([
        d['zipCode'],
        d['buildingIdentificationNumber'],
        d['ntaName'],
        d['nta'],
        d['communityDistrict'],
        d['longitude'],
        d['latitude'],
        d['xCoordinate'],
        d['yCoordinate'],
        d['censusTract2010'],
        d['censusBlock2010'],
        d['censusTract2000'],
        d['censusBlock2000']])

geoclient_table = address_table.apply(expand_row, axis=1)
geoclient_table.columns = [
    'ZipCode',
    'BuildingIdNumber',
    'NeighborhoodName',
    'NeighborhoodCode',
    'CommunityDistrict',
    'Longitude',
    'Latitude',
    'XCoordinate',
    'YCoordinate',
    '2010CensusTract',
    '2010CensusBlock',
    '2000CensusTract',
    '2000CensusBlock']
expanded_address_table = address_table.join(geoclient_table)
expanded_address_table[:2]
In [ ]:
target_path = target_folder + '/expanded-addresses.csv'
expanded_address_table.to_csv(target_path, index=False)
# Show table in result
print('expanded_address_table_path = %s' % target_path)
In [ ]:
def get_lonlat(row):
    return row['Longitude'], row['Latitude']

xys = list(expanded_address_table.apply(get_lonlat, axis=1))
xys
In [ ]:
from shapely.geometry import LineString
line = LineString(xys)
line
In [ ]:
from shapely.geometry import Polygon
polygon = Polygon(xys + [xys[0]])
polygon
In [ ]:
longitude_latitude_wkts = [
    line.wkt,
    polygon.wkt,
]
longitude_latitude_wkts
In [ ]:
def get_xy(row):
    return row['XCoordinate'], row['YCoordinate']

xys = list(expanded_address_table.apply(get_lonlat, axis=1))
xy_wkts = [
    LineString(xys).wkt,
    Polygon(xys + [xys[0]]).wkt,
]
xy_wkts
In [ ]:
target_path = target_folder + '/map.csv'
pd.DataFrame(longitude_latitude_wkts, columns=[
    'wkt']).to_csv(target_path, index=False)
print('map_geotable_path = %s' % target_path)
In [ ]:
target_path = target_folder + '/longitude-latitude-wkts.txt'
open(target_path, 'wt').write('\n'.join(longitude_latitude_wkts))
print('longitude_latitude_wkt_text_path = %s' % target_path)
In [ ]:
target_path = target_folder + '/xy-wkts.txt'
open(target_path, 'wt').write('\n'.join(xy_wkts))
print('xy_wkt_text_path = %s' % target_path)

Generated LineString and Polygon in WKT Format

{ expanded_address_table : Expanded Address Table }

{ map_geotable : Map of Generated LineString and Polygon }

{ longitude_latitude_wkt_text : Generated LineString and Polygon in WKT Format using EPSG:4326 }

The geodesic EPSG:4326 spatial reference specifies longitude and latitude coordinates. Here is the proj4 specification:

+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs

{ xy_wkt_text : Generated LineString and Polygon in WKT Format using EPSG:2263 }

The Euclidean EPSG:2263 spatial reference specifies X and Y coordinates in feet (not meters). Here is the proj4 specification:

+proj=lcc +lat_1=41.03333333333333 +lat_2=40.66666666666666 +lat_0=40.16666666666666 +lon_0=-74 +x_0=300000.0000000001 +y_0=0 +ellps=GRS80 +datum=NAD83 +to_meter=0.3048006096012192 +no_defs