def load_neighborhoods(self): ds = DataSource(str(NEIGHBORHOOD_SHAPEFILE)) layer = ds[0] nyc_features = [ feature for feature in layer if str(feature['City']) == 'New York' ] # The same neighborhoods are present multiple times # in different counties, but they cover the same # geographic areas and have the same names, so we'll # coalesce them. # # We'll use the "RegionID" for this, which represents # the shape of the area. This allows us to disambiguate # between neighborhoods in different counties with the # same name that actually *do* represent different # geographic areas, like Murray Hill. region_counties: Dict[str, List[str]] = defaultdict(list) for feature in nyc_features: region = str(feature['RegionID']) region_counties[region].append(str(feature['County'])) regions: Set[str] = set() for feature in nyc_features: region = str(feature['RegionID']) if region in regions: continue regions.add(region) name = str(feature['Name']) county = ' / '.join(region_counties[region]) instance = get_or_construct(Neighborhood, name=name, county=county) geom = feature.geom geom.transform(4326) instance.geom = to_multipolygon(geom.geos) print(f"Saving neighborhood {instance}.") instance.save()
class CountyFactory(factory.django.DjangoModelFactory): class Meta: model = County state = "NY" name = "Funkypants" geom = to_multipolygon(POLY_1)
def load_counties(self): ds = DataSource(str(NYS_COUNTIES_SHAPEFILE)) layer = ds[0] for feature in layer: name = str(feature["NAME"]) state = US_STATE_CHOICES.NY instance = get_or_construct(County, state=state, name=name) instance.geom = to_multipolygon(feature.geom.geos) print(f"Saving county {instance}.") instance.save()
def load_community_districts(self): ds = DataSource(str(COMMUNITY_DISTRICT_SHAPEFILE)) layer = ds[0] for feature in layer: boro_cd = str(feature['boro_cd']) name = CommunityDistrict.boro_cd_to_name(boro_cd) instance = get_or_construct(CommunityDistrict, boro_cd=boro_cd) geom = feature.geom geom.transform(4326) instance.geom = to_multipolygon(geom.geos) instance.name = name print(f"Saving {instance.name}.") instance.save()
def load_zipcodes(self): ds = DataSource(str(ZIPCODE_SHAPEFILE)) layer = ds[0] zipcodes: Dict[str, Any] = {} for feature in layer: zipcode = str(feature['ZIPCODE']) geom = feature.geom geom.transform(4326) if zipcode in zipcodes: zipcodes[zipcode] = zipcodes[zipcode].union(geom.geos) else: zipcodes[zipcode] = geom.geos for zipcode, geos_geom in zipcodes.items(): print(f"Saving zipcode {zipcode}.") instance = get_or_construct(Zipcode, zipcode=zipcode) instance.geom = to_multipolygon(geos_geom) instance.save() print(f"Loaded {len(zipcodes)} zipcodes across {len(layer)} features.")
def create_cd(boro_cd="164", name="Central Park", geom=POLY_1): cd = CommunityDistrict(boro_cd=boro_cd, name=name, geom=to_multipolygon(geom)) cd.save() return cd
def create_neighborhood(name="Dumbo", county="Kings", geom=POLY_1): neighborhood = Neighborhood(name=name, county=county, geom=to_multipolygon(geom)) neighborhood.save() return neighborhood
def create_borough(code=1, name="Manhattan", geom=POLY_1): borough = Borough(code=code, name=name, geom=to_multipolygon(geom)) borough.save() return borough
def create_zipcode(zipcode="11201", geom=POLY_1): zc = Zipcode(zipcode=zipcode, geom=to_multipolygon(geom)) zc.save() return zc
def test_to_multipolygon_passes_through_multipolygons(): mp = MultiPolygon(POLY_1) assert to_multipolygon(mp) is mp
def test_to_multipolygon_converts_polygons(): p = POLY_1 mp = to_multipolygon(p) assert isinstance(mp, MultiPolygon) assert mp[0] == p