0

I have a shapefile of New York ZCTAs here

I also have these neighborhood definitions that map zip codes to New York City Boroughs.

I'd like to take my "all of New York zip code" data and:

  1. Clump up zip codes into their boroughs
  2. Ignore all non-NYC zip codes given in the file

That is, I want to no longer have zip-code level polygons, but borough-level polygons with borough-level features.names, and some capacity to map new zip code data back onto the correct data, something like features.zctalist = ["old", "zipcodes"]

Looking at the start of this file, it has:

head -c 1000 ny_new_york_zip_codes_geo.min.json 
{"type":"FeatureCollection",
"features":[
   {"type":"Feature",
    "properties":
        {"STATEFP10":"36","ZCTA5CE10":"12205","GEOID10":"3612205","CLASSFP10":"B5","MTFCC10":"G6350","FUNCSTAT10":"S","ALAND10":40906445,"AWATER10":243508,"INTPTLAT10":"+42.7187855","INTPTLON10":"-073.8292399","PARTFLG10":"N"}

Part of my file is...

# borough.json
{"Bronx" : [
"10453", "10457", "10460",
"10458", "10467", "10468",
"10451", "10452", "10456",
"10454", "10455", "10459", 
"10474", "10463", "10471", 
"10466", "10469", "10470", 
"10475", "10461", "10462",
"10464", "10465", "10472", 
"10473"],
"Brooklyn" : [
"11212", "11213", "11216",
"11233", "11238", "11209",
"11214", "11228", "11204",
"11218", "11219", "11230",
"11234", "11236", "11239",
"11223", "11224", "11229",
"11235", "11201", "11205",
"11215", "11217", "11231",
"11203", "11210", "11225",
"11226", "11207", "11208",
"11211", "11222", "11220",
"11232", "11206", "11221",
"11237"
],
"Manhattan" : [
"10026", "10027", "10030", 
"10037", "10039", "10001", 
"10011", "10018", "10019", 
"10020", "10036", "10029", 
"10035", "10010", "10016", 
"10017", "10022", "10012", 
"10013", "10014", "10004", 
"10005", "10006", "10007", 
"10038", "10280", "10002", 
"10003", "10009", "10021", 
"10028", "10044", "10065", 
"10075", "10128", "10023", 
"10024", "10025", "10031", 
"10032", "10033", "10034",
"10040"]
}

I believe ideally, I'd have something like:

{"type":"FeatureCollection",
{ features :   {"type":"Feature",
    "properties":
        {"borough_name":"Bronx", 
    "STATEFP10" : ["36", ....]
    zctas: [10453, 10457, 10460,
    10458, 10467, 10468
    10451, 10452, 10456
    10454, 10455, 10459, 10474
    10463, 10471
    10466, 10469, 10470, 10475
    10461, 10462,10464, 10465, 10472, 10473],
...etc...

There are about 12 fields I'd need each to roll into a list, if that's the right way of thinking about it, where then the

"geometry":{"type":"Polygon","coordinates":[ ... union of coordinates ... ]

I can do some ugly list merging in Python, but I imagine this is a standard operation.

Questions:

  • Is using lists in features correct, or something I should avoid?
  • How do I accomplish this operation using common tools?

I /think/ something with ogr

ogr2ogr outputfile.json ny_new_york_zip_codes_geo.min.json
 -dialect sqlite -sql “SELECT ZCTA5CE10,ST_Union(geometry) as geometry FROM inputfile GROUP BY dissolvefield”
# how do I include my external json into this?

I see related questions like this here:

But I am not fully sure of the name of the operation I am trying to do in order to research it better, or generally how to do this operation in commandline/Python. (If you can suggest a Python solution I am using Folium and geopandas).

Edit 1

Also tried this:

import geopandas
import json
ny = geopandas.read_file('ny_new_york_zip_codes_geo.min.json')
boroughs = json.load(open("borough.json"))
import matplotlib as plt
ny['borough'] = ''

for k,v in boroughs.items():
    ny.borough = boroughs.keys()[k] if v  == ny.ZCTA5CE10  else ''

# ValueError: Arrays were different lengths: 1794 vs 25# FAIL
  • Please ask only one question per Question. "GeoJSON shapefile" is a rough concept, since they are mutually exclusive data formats. – Vince Nov 29 '18 at 2:59
  • I removed the redundancy, sorry. – Mittenchops Nov 29 '18 at 4:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.