Exemple #1
0
    def handle_row(self, row):
        self.log_verbose(row)

        # check the physical location
        ok, x, y = self.check_coords(row)
        if not ok: return

        # check the species (if any)
        ok, species = self.check_species(row)
        if not ok: return

        # check the proximity (try to match up with existing trees)
        if (species):
            tree = Tree(species=species[0])
        else:
            tree = Tree()
        tree.geometry = Point(x, y, srid=4326)
        
        ok, tree = self.check_proximity(tree, species, row)
        if not ok: return

        pnt = tree.geometry

        if row.get('ADDRESS') and not tree.address_street:
            tree.address_street = str(row['ADDRESS']).title()
            
        # find the neighborhood, if any
        if not tree.neighborhood:
            n = Neighborhood.objects.filter(geometry__contains=pnt)
            if n: 
                tree.neighborhood = n[0]
                tree.address_city = n[0].city
            else:
                tree.neighborhood = None

        # find the zip code, if any
        if not tree.zipcode:
            z = ZipCode.objects.filter(geometry__contains=pnt)
            if z: 
                tree.zipcode = z[0]
                tree.address_zip = z[0].zip
            else:
                tree.zipcode = None

        # FIXME: get this from the config?
        tree.address_state = 'PA'

        tree.import_event = self.import_event
        tree.last_updated_by = self.updater
        tree.data_owner = self.data_owner
        tree.owner_additional_properties = self.file_name
        if row.get('ID'):
            tree.owner_id = row['ID']
        
        if row.get('PLOTTYPE'):
            for k, v in Choices().get_field_choices('plot'):
                if v == row['PLOTTYPE']:
                    tree.plot_type = v
                    break;
        if row.get('PLOTLENGTH'): 
            tree.plot_length = row['PLOTLENGTH']

        if row.get('PLOTWIDTH'): 
            tree.plot_width = row['PLOTWIDTH']            

        # if powerline is specified, then we want to set our boolean
        # attribute; otherwise leave it alone.
        powerline = row.get('POWERLINE')
        if powerline is None or powerline.strip() == "":
            pass
        elif powerline is True or powerline.lower() == "true" or powerline.lower() == 'yes':
            tree.powerline_conflict_potential = 'Yes'
        else:
            tree.powerline_conflict_potential = 'No'

        sidewalk_damage = row.get('SIDEWALK')
        if sidewalk_damage is None or sidewalk_damage.strip() == "":
            pass
        elif sidewalk_damage is True or sidewalk_damage.lower() == "true" or sidewalk_damage.lower() == 'yes':
            tree.sidewalk_damage = 2
        else:
            tree.sidewalk_damage = 1

        if row.get('OWNER'):
            tree.tree_owner = str(row["OWNER"])

        if row.get('STEWARD'):
            tree.steward_name = str(row["STEWARD"])

        if row.get('SPONSOR'):
            tree.sponsor = str(row["SPONSOR"])

        if row.get('DATEPLANTED'):
            tree.date_planted = str(row['DATEPLANTED'])

        if row.get('DIAMETER'):
            tree.dbh = row['DIAMETER']

        if row.get('HEIGHT'):
            tree.height = row['HEIGHT']

        if row.get('CANOPYHEIGHT'):
            tree.canopy_height = row['CANOPYHEIGHT']

        if row.get('CONDITION'):
            for k, v in Choices().get_field_choices('condition'):
                if v == row['CONDITION']:
                    tree.condition = v
                    break;

        if row.get('CANOPYCONDITION'):
            for k, v in Choices().get_field_choices('canopy_condition'):
                if v == row['CANOPYCONDITION']:
                    tree.canopy_condition = v
                    break;

        tree.quick_save()

        if row.get('PROJECT_1'):
            pass
        if row.get('PROJECT_2'):
            pass
        if row.get('PROJECT_3'):
            pass


        # rerun validation tests and store results
        tree.validate_all()