def get_brt(): """ Check address data source to get brt number. Update database accordingly. """ from .models import LotProfile queryset = queryset_iterator(LotProfile.objects.filter(brt_id__exact='')) for lot_profile in queryset: lon = lot_profile.get_center().x lat = lot_profile.get_center().y source = settings.PHL_DATA["ADDRESSES"] + "query" params = {"geometry":"%f, %f" % (lon, lat), "geometryType":"esriGeometryPoint", "returnGeometry":"false", "inSR":"4326", "spatialRel":"esriSpatialRelWithin", "outFields":"BRT_ID", "f":"json"} data = fetch_json(source, params) if data: if "error" in data: print(data["error"]["details"]) elif "features" in data: features = data["features"] if features: if features[0]: attributes = features[0]["attributes"] lot_profile.brt_id = attributes["BRT_ID"] lot_profile.save(update_fields=["brt_id",]) print("%s: %s updated" % (lot_profile.id, lot_profile.address)) else: print("%s" % (lot.id)) else: print("No features for %s" % (lot.id)) else: print("No Data")
def check_public(): """ Check papl assets data source to get public status. Update database accordingly. """ from apps.lotxlot.models import Lot t = datetime.datetime.now() - datetime.timedelta(days=1) queryset = queryset_iterator(Lot.objects.vacant()) #queryset = queryset_iterator(Lot.objects.filter(created__gt=t)) for lot in queryset: lon = lot.coord.x lat = lot.coord.y source = settings.PHL_DATA["PAPL_ASSETS"] + "query" params = {"geometry":"%f, %f" % (lon, lat), "geometryType":"esriGeometryPoint", "returnGeometry":"false", "inSR":"4326", "spatialRel":"esriSpatialRelWithin", "outFields":"C_DIG3", "f":"json"} req = requests.get(source, params=params) data = json.loads(req.text) if data: if "features" in data: features = data["features"] if features: if features[0]: lot.is_public = True lot.save(update_fields=["is_public",]) print("updated lot %s") % lot.address
def get_basereg(): """ Check papl parcel data source to get basereg number. Update database accordingly. """ from .models import LotProfile queryset = queryset_iterator(LotProfile.objects.filter(basereg='')) for lot_profile in queryset: lon = lot_profile.get_center().x lat = lot_profile.get_center().y source = settings.PHL_DATA["PAPL_PARCELS"] + "query" params = {"geometry":"%f, %f" % (lon, lat), "geometryType":"esriGeometryPoint", "returnGeometry":"false", "inSR":"4326", "spatialRel":"esriSpatialRelWithin", "outFields":"BASEREG", "f":"json"} req = requests.get(source, params=params) data = json.loads(req.text) if data: if "features" in data: features = data["features"] if features: if features[0]: attributes = features[0]["attributes"] lot_profile.basereg = attributes["BASEREG"] lot_profile.save(update_fields=["basereg",]) print("updated lot %s") % lot_profile.address
def delete_duplicate_lots(): queryset = queryset_iterator(Lot.objects.all) for row in queryset: if Lot.objects.filter(opa_code=row.opa_code).count() > 1: row.delete() print "Deleted" + row.address else: print row.address
def fix_addresses(): queryset = queryset_iterator(Lot.objects.filter(geom__isnull=True)) for lot in queryset: print(lot.address) address = lot.address address = ' '.join(address.split()).upper() lot.address = address lot.save() print(lot.address)
def update_vacancy_license(): """ Check Philadelphia gis datasource to get lots with vacancy licenses """ queryset = queryset_iterator(Lot.objects.filter(is_vacant=True)) for lot in queryset: lot.vacancy_license_id = lot._get_vacancy_license_id() lot.save(update_fields=["vacancy_license_id",]) print("%s - %s: %s" % (lot.id, lot.address, lot.vacancy_license_id))
def update_vacancy_violation(): """ Check Philadelphia gis datasource to get lots with vacancy violations """ queryset = queryset_iterator(Lot.objects.all()) for lot in queryset: lot.vacancy_violation_id = lot._get_vacancy_violation_id() lot.save(update_fields=["vacancy_violation_id",]) print("%s - %s: %s" % (lot.id, lot.address, lot.vacancy_violation_id))
def update_papl_listing(): """ Check Philadelphia gis datasource to get available (for sale) publicly owned vacant land id """ queryset = queryset_iterator(Lot.objects.filter(is_vacant=True).filter(papl_listing_id__isnull=True)) for lot in queryset: lot.papl_listing_id = lot._get_papl_listing_id() lot.save(update_fields=["papl_listing_id",]) print("%s - %s: %s" % (lot.id, lot.address, lot.papl_listing_id))
def update_demolition_permit(): """ Check Philadelphia gis datasource to get lots with L&I demolition permits """ queryset = queryset_iterator(Lot.objects.filter(is_vacant=True)) for lot in queryset: lot.demolition_permit_id = lot._get_demolition_permit_id() lot.save(update_fields=["demolition_permit_id",]) print("%s - %s: %s" % (lot.id, lot.address, lot.demolition_permit_id))
def update_vacancy(public=False): """ Check vacancy indicators and mark is_vacant appropriately """ from apps.lotxlot.models import Lot t = datetime.datetime.now() - datetime.timedelta(days=14) if public: queryset = queryset_iterator(Lot.objects.public()) else: queryset = queryset_iterator( Lot.objects.filter(is_vacant=False, updated__lt=t)) for lot in queryset: vacant = lot.is_vacant if lot.profile.is_bldg_desc_vacant: vacant = True indicator = "bldg desc" elif lot.profile.is_land_use_vacant: vacant = True indicator = "land use" elif lot.profile.has_license: vacant = True indicator = "license" elif lot.profile.has_violation: vacant = True indicator = "violation" elif lot.profile.is_for_sale: vacant = True indicator = "for sale" else: vacant = False indicator = "Not Vacant" lot.is_vacant = vacant lot.save(update_fields=["is_vacant", "updated"]) print("updated lot %s: %s") % (lot.id, indicator)
def update_vacancy(public=False): """ Check vacancy indicators and mark is_vacant appropriately """ from apps.lotxlot.models import Lot t = datetime.datetime.now() - datetime.timedelta(days=14) if public: queryset = queryset_iterator(Lot.objects.public()) else: queryset = queryset_iterator(Lot.objects.filter(is_vacant=False, updated__lt=t)) for lot in queryset: vacant = lot.is_vacant if lot.profile.is_bldg_desc_vacant: vacant=True indicator="bldg desc" elif lot.profile.is_land_use_vacant: vacant=True indicator="land use" elif lot.profile.has_license: vacant=True indicator="license" elif lot.profile.has_violation: vacant=True indicator="violation" elif lot.profile.is_for_sale: vacant=True indicator="for sale" else: vacant=False indicator="Not Vacant" lot.is_vacant = vacant lot.save(update_fields=["is_vacant", "updated"]) print("updated lot %s: %s") % (lot.id, indicator)
def check_landuse_vacancy(): """ Check landuse data source to get vacancy status. Update database accordingly. """ from apps.lotxlot.models import Lot t = datetime.datetime.now() - datetime.timedelta(days=1) queryset = queryset_iterator(Lot.objects.filter(created__gt=t)) for lot in queryset: lon = lot.coord.x lat = lot.coord.y source = settings.PHL_DATA["LAND_USE"] + "query" params = { "geometry": "%f, %f" % (lon, lat), "geometryType": "esriGeometryPoint", "returnGeometry": "false", "inSR": "4326", "spatialRel": "esriSpatialRelWithin", "outFields": "C_DIG3", "f": "json" } req = requests.get(source, params=params) data = json.loads(req.text) if data: if "features" in data: features = data["features"] if features[0]: attributes = features[0]["attributes"] if "C_DIG3" in attributes: if attributes["C_DIG3"] == 911: lot.is_vacant = True lot.save(update_fields=[ "is_vacant", ]) print("updated lot %s") % lot.address else: print(lot.id)
def get_basereg(): """ Check papl parcel data source to get basereg number. Update database accordingly. """ from .models import LotProfile queryset = queryset_iterator(LotProfile.objects.filter(basereg='')) for lot_profile in queryset: lon = lot_profile.get_center().x lat = lot_profile.get_center().y source = settings.PHL_DATA["PAPL_PARCELS"] + "query" params = { "geometry": "%f, %f" % (lon, lat), "geometryType": "esriGeometryPoint", "returnGeometry": "false", "inSR": "4326", "spatialRel": "esriSpatialRelWithin", "outFields": "BASEREG", "f": "json" } req = requests.get(source, params=params) data = json.loads(req.text) if data: if "features" in data: features = data["features"] if features: if features[0]: attributes = features[0]["attributes"] lot_profile.basereg = attributes["BASEREG"] lot_profile.save(update_fields=[ "basereg", ]) print("updated lot %s") % lot_profile.address
def get_missing_geom(): queryset = queryset_iterator(Lot.objects.filter(geom__isnull=True)) source = settings.PHL_DATA["ADDRESSES"] + "query" for lot in queryset: id = lot._get_address_id() if id: params = {"where":"OBJECTID='%s'" % (id), "outSR":4236, "returnGeometry":"true", "f":"json"} data = fetch_json(source, params) if data and "features" in data: if data["features"]: coords = data["features"][0]["geometry"]["rings"] geoJSON = json.dumps({"type": "Polygon", "coordinates": coords}) geom = GEOSGeometry(geoJSON) geom = MultiPolygon(geom) lot.geom = geom else: lot.address_id = id lot.save() print("%s - %s: %s" % (lot.id, lot.address, lot.geom)) else: f = open('missing.txt', 'a') f.write(lot.address + '\n') f.close print("%s - %s: missing" % (lot.id, lot.address))