예제 #1
0
def make_building(location, bldg_id, city, feed_source, parcel_id=None, bldg_type=None, no_units=None, sqft=None):
    """
    add the building to the database
    """

    full_city = '%s, IN, USA' % city.name
    match = False
    #find an address to use
    for geo_source in location.sources:
        if not match:
            source_list = location.get_source(geo_source)
            if len(source_list) and source_list[0]['place'] and source_list[0]['place'] != full_city:
                print "using: %s to check: %s" % (geo_source, source_list[0]['place'])
                match = True

                #TODO: process this a bit more...
                #probably don't want city and zip here:

                #keeping city and zip minimizes chance for overlap
                #especially since this is used as a key
                #can always take it out on display, if necessary
                cur_address = source_list[0]['place']


                if parcel_id == None:
                    cid = "%s-%s" % (city.tag, bldg_id)
                else:
                    cid = parcel_id

                print "Checking parcel id: %s" % (cid)

                parcels = Parcel.objects.filter(custom_id=cid)
                if parcels.exists():
                    parcel = parcels[0]
                    print "Already had parcel: %s" % parcel.custom_id
                else:
                    parcel = Parcel()
                    parcel.custom_id = cid
                    parcel.save()
                    print "Created new parcel: %s" % parcel.custom_id


                buildings = Building.objects.filter(city=city).filter(address=cur_address)

                bldg = None
                #check if a previous building object in the db exists
                if buildings.exists():
                    bldg = buildings[0]
                    print "Already had: %s" % bldg.address
                else:
                    #if not, 
                    #CREATE A NEW BUILDING OBJECT HERE
                    #cur_building = Building()
                    bldg = Building()

                    bldg.address = source_list[0]['place']
                    bldg.latitude = float(source_list[0]['lat'])
                    bldg.longitude = float(source_list[0]['lng'])

                    bldg.parcel = parcel
                    bldg.geocoder = geo_source
                    bldg.source = feed_source

                    bldg.city = city
                    bldg.state = city.state

                    if bldg_type:
                        bldg.type = bldg_type
                    if no_units:
                        bldg.number_of_units = no_units
                    if sqft:
                        bldg.sqft = sqft

                    bldg.save()


                    print "Created new building: %s" % bldg.address

                return bldg
            else:
                print "Skipping: %s with value: %s" % (geo_source, source_list[0]['place'])