示例#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()
        
        try:
            if (self.base_srid != 4326):
                geom = Point(x, y, srid=self.base_srid)
                geom.transform(self.tf)
                self.log_verbose(geom)
                tree.geometry = geom
            else:        
                tree.geometry = Point(x, y, srid=4326)
        except:
            self.log_error("ERROR: Geometry failed to transform", row)
            return
        
        ok, tree = self.check_proximity(tree, species, row)
        if not ok: return

        if row.get('ADDRESS') and not tree.address_street:
            tree.address_street = str(row['ADDRESS']).title()
            tree.geocoded_address = str(row['ADDRESS']).title()
        
        if not tree.geocoded_address: 
            tree.geocoded_address = ""
            
            
        # FIXME: get this from the config?
        tree.address_state = 'CA'

        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_orig_id = row['ID']
        
        if row.get('ORIGID'):
            tree.owner_additional_properties = "ORIGID=" + str(row['ORIGID'])

        if row.get('PLOTTYPE'):
            for k, v in Choices().get_field_choices('plot'):
                if v == row['PLOTTYPE']:
                    tree.plot_type = k
                    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'):
            date = str(row['DATEPLANTED'])
            date = datetime.strptime(date, "%m/%d/%Y")
            tree.date_planted = date.strftime("%Y-%m-%d")

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

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

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

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

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


        pnt = tree.geometry

        n = Neighborhood.objects.filter(geometry__contains=pnt)
        z = ZipCode.objects.filter(geometry__contains=pnt)
        
        if n:
            tree.neighborhoods = ""
            for nhood in n:
                if nhood:
                    tree.neighborhoods = tree.neighborhoods + " " + nhood.id.__str__()
        else: 
            tree.neighborhoods = ""

        tree.quick_save()

        tree.neighborhood.clear()
        tree.zipcode = None
        if n:
            for nhood in n:
                if nhood:
                    tree.neighborhood.add(nhood)
        if z: tree.zipcode = z[0]

        if row.get('PROJECT_1'):
            for k, v in Choices().get_field_choices('local'):
                if v == row['PROJECT_1']:
                    local = TreeFlags(key=k,tree=tree,reported_by=self.updater)
                    local.save()
                    break;
        if row.get('PROJECT_2'):            
            for k, v in Choices().get_field_choices('local'):
                if v == row['PROJECT_2']:
                    local = TreeFlags(key=k,tree=tree,reported_by=self.updater)
                    local.save()
                    break;
        if row.get('PROJECT_3'):           
            for k, v in Choices().get_field_choices('local'):
                if v == row['PROJECT_3']:
                    local = TreeFlags(key=k,tree=tree,reported_by=self.updater)
                    local.save()
                    break;


        # rerun validation tests and store results
        tree.validate_all()
示例#2
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()