예제 #1
0
    def test_unknown_field(self):
        bie = ImportEvent(file_name="fn")
        bie.save()

        ie = TreeImportEvent(file_name='file',
                             owner=self.user,
                             base_import_event=bie)
        ie.save()

        base_rows = TreeImportRow.objects.count()

        c = self.write_csv([['address', 'name', 'age', 'point x', 'point y'],
                            ['123 Beach St', 'a', 'b', '5', '5'],
                            ['222 Main St', 'a', 'b', '8', '8']])

        create_rows_for_event(ie, c)
        rslt = ie.validate_main_file()

        self.assertFalse(rslt)

        ierrors = json.loads(ie.errors)

        # Should be x/y point error
        self.assertTrue(len(ierrors), 1)
        etpl = (ierrors[0]['code'], ierrors[0]['msg'], False)

        self.assertEqual(etpl, errors.UNMATCHED_FIELDS)
        self.assertEqual(set(ierrors[0]['data']), set(['name', 'age']))
예제 #2
0
    def test_missing_point_field(self):
        bie = ImportEvent(file_name="fn")
        bie.save()

        ie = TreeImportEvent(file_name='file',
                             owner=self.user,
                             base_import_event=bie)
        ie.save()

        base_rows = TreeImportRow.objects.count()

        c = self.write_csv([['address', 'plot width', 'plot_length'],
                            ['123 Beach St', '5', '5'],
                            ['222 Main St', '8', '8']])

        create_rows_for_event(ie, c)
        rslt = ie.validate_main_file()

        self.assertFalse(rslt)

        ierrors = json.loads(ie.errors)

        # Should be x/y point error
        self.assertTrue(len(ierrors), 1)
        etpl = (ierrors[0]['code'], ierrors[0]['msg'], True)

        self.assertEqual(etpl, errors.MISSING_POINTS)
예제 #3
0
def process_csv(request, fileconstructor, **kwargs):
    files = request.FILES
    filename = files.keys()[0]
    fileobj = files[filename]

    owner = request.user
    ie = fileconstructor(file_name=filename, owner=owner, **kwargs)

    # If this is a tree import event it also needs an
    # 'old style' import event
    bie = ImportEvent(file_name=filename)
    bie.save()

    if hasattr(ie, 'base_import_event_id'):
        ie.base_import_event = bie

    ie.save()

    try:
        rows = create_rows_for_event(ie, fileobj)

        transaction.commit()

        if rows:
            run_import_event_validation.delay(ie)

    except Exception, e:
        ie.append_error(errors.GENERIC_ERROR, [], data=str(e))
        ie.status = GenericImportEvent.FAILED_FILE_VERIFICATION
        ie.save()
예제 #4
0
def process_csv(request, fileconstructor, **kwargs):
    files = request.FILES
    filename = files.keys()[0]
    fileobj = files[filename]

    owner = request.user
    ie = fileconstructor(file_name=filename,
                         owner=owner,
                         **kwargs)

    # If this is a tree import event it also needs an
    # 'old style' import event
    bie = ImportEvent(file_name=filename)
    bie.save()

    if hasattr(ie, 'base_import_event_id'):
        ie.base_import_event = bie

    ie.save()

    try:
        rows = create_rows_for_event(ie, fileobj)

        transaction.commit()

        if rows:
            run_import_event_validation.delay(ie)

    except Exception, e:
        ie.append_error(errors.GENERIC_ERROR, [], data=str(e))
        ie.status = GenericImportEvent.FAILED_FILE_VERIFICATION
        ie.save()
예제 #5
0
    def test_empty_file_error(self):
        bie = ImportEvent(file_name="fn")
        bie.save()

        ie = TreeImportEvent(file_name='file',
                             owner=self.user,
                             base_import_event=bie)
        ie.save()

        base_rows = TreeImportRow.objects.count()

        c = self.write_csv([['header_field1','header_fields2','header_field3']])

        create_rows_for_event(ie, c)
        rslt = ie.validate_main_file()

        # No rows added and validation failed
        self.assertEqual(TreeImportRow.objects.count(), base_rows)
        self.assertFalse(rslt)

        ierrors = json.loads(ie.errors)

        # The only error is a bad file error
        self.assertTrue(len(ierrors), 1)
        etpl = (ierrors[0]['code'], ierrors[0]['msg'], True)

        self.assertEqual(etpl, errors.EMPTY_FILE)
예제 #6
0
    def test_missing_point_field(self):
        bie = ImportEvent(file_name="fn")
        bie.save()

        ie = TreeImportEvent(file_name='file',
                             owner=self.user,
                             base_import_event=bie)
        ie.save()

        base_rows = TreeImportRow.objects.count()

        c = self.write_csv([['address','plot width','plot_length'],
                            ['123 Beach St','5','5'],
                            ['222 Main St','8','8']])

        create_rows_for_event(ie, c)
        rslt = ie.validate_main_file()

        self.assertFalse(rslt)

        ierrors = json.loads(ie.errors)

        # Should be x/y point error
        self.assertTrue(len(ierrors), 1)
        etpl = (ierrors[0]['code'], ierrors[0]['msg'], True)

        self.assertEqual(etpl, errors.MISSING_POINTS)
예제 #7
0
    def handle(self, *args, **options):
        """ Create some seed data """
        instance, user = self.setup_env(*args, **options)

        species_qs = instance.scope_model(Species)

        n = options['n']
        self.stdout.write("Will create %s plots" % n)

        get_prob = lambda option: float(min(100, max(0, option))) / 100.0
        tree_prob = get_prob(options['ptree'])
        species_prob = get_prob(options['pspecies'])
        diameter_prob = get_prob(options['pdiameter'])
        max_radius = options['radius']

        center_x = instance.center.x
        center_y = instance.center.y

        import_event = ImportEvent(imported_by=user)
        import_event.save()

        ct = 0
        cp = 0
        for i in xrange(0, n):
            mktree = random.random() < tree_prob
            radius = random.gauss(0.0, max_radius)
            theta = random.random() * 2.0 * math.pi

            x = math.cos(theta) * radius + center_x
            y = math.sin(theta) * radius + center_y

            plot = Plot(instance=instance,
                        geom=Point(x, y),
                        import_event=import_event)

            plot.save_with_user(user)
            cp += 1

            if mktree:
                add_species = random.random() < species_prob
                if add_species:
                    species = random.choice(species_qs)
                else:
                    species = None

                add_diameter = random.random() < diameter_prob
                if add_diameter:
                    diameter = 2 + random.random() * 18
                else:
                    diameter = None

                tree = Tree(plot=plot,
                            import_event=import_event,
                            species=species,
                            diameter=diameter,
                            instance=instance)
                tree.save_with_user(user)
                ct += 1

        self.stdout.write("Created %s trees and %s plots" % (ct, cp))
예제 #8
0
    def test_empty_file_error(self):
        bie = ImportEvent(file_name="fn")
        bie.save()

        ie = TreeImportEvent(file_name='file',
                             owner=self.user,
                             base_import_event=bie)
        ie.save()

        base_rows = TreeImportRow.objects.count()

        c = self.write_csv(
            [['header_field1', 'header_fields2', 'header_field3']])

        create_rows_for_event(ie, c)
        rslt = ie.validate_main_file()

        # No rows added and validation failed
        self.assertEqual(TreeImportRow.objects.count(), base_rows)
        self.assertFalse(rslt)

        ierrors = json.loads(ie.errors)

        # The only error is a bad file error
        self.assertTrue(len(ierrors), 1)
        etpl = (ierrors[0]['code'], ierrors[0]['msg'], True)

        self.assertEqual(etpl, errors.EMPTY_FILE)
예제 #9
0
    def test_unknown_field(self):
        bie = ImportEvent(file_name="fn")
        bie.save()

        ie = TreeImportEvent(file_name='file',
                             owner=self.user,
                             base_import_event=bie)
        ie.save()

        base_rows = TreeImportRow.objects.count()

        c = self.write_csv([['address','name','age','point x','point y'],
                            ['123 Beach St','a','b','5','5'],
                            ['222 Main St','a','b','8','8']])

        create_rows_for_event(ie, c)
        rslt = ie.validate_main_file()

        self.assertFalse(rslt)

        ierrors = json.loads(ie.errors)

        # Should be x/y point error
        self.assertTrue(len(ierrors), 1)
        etpl = (ierrors[0]['code'], ierrors[0]['msg'], False)

        self.assertEqual(etpl, errors.UNMATCHED_FIELDS)
        self.assertEqual(set(ierrors[0]['data']), set(['name','age']))
예제 #10
0
    def setUp(self):
        self.user = User(username='******')
        self.user.save()

        bie = ImportEvent(file_name="fn")
        bie.save()

        self.ie = TreeImportEvent(file_name='file',
                                  owner=self.user,
                                  base_import_event=bie)
        self.ie.save()
예제 #11
0
    def setUp(self):
        self.user = User(username='******')
        self.user.save()

        bie = ImportEvent(file_name="fn")
        bie.save()

        self.ie = TreeImportEvent(file_name='file',
                                  owner=self.user,
                                  base_import_event=bie)
        self.ie.save()
예제 #12
0
    def handle(self, *args, **options):
        self.wrote_headers = False
        try:
            self.file_name = args[0]
            in_file = self.file_name
            err_file = in_file + ".err"
            self.verbose = options.get('verbose')
            self.user_id = args[1]
            if len(args) > 2:
                self.base_srid = int(args[2])
                self.tf = CoordTransform(SpatialReference(self.base_srid),
                                         SpatialReference(4326))
                print "Using transformaton object: %s" % self.tf
            else:
                self.base_srid = 4326
            if len(args) > 3:
                self.readonly = bool(args[3])
                print "Setting readonly flag to %s" % self.readonly
            else:
                self.readonly = False
        except:
            print "Arguments:  Input_File_Name.[dbf|csv], Data_Owner_User_Id, (Base_SRID optional)"
            print "Options:    --verbose"
            return

        self.err_writer = csv.writer(open(err_file, 'wb'))

        if self.file_name.endswith('.csv'):
            rows = self.get_csv_rows(in_file)
        if self.file_name.endswith('.dbf'):
            rows = self.get_dbf_rows(in_file)

        self.data_owner = User.objects.get(pk=self.user_id)
        self.updater = User.objects.get(pk=1)

        self.import_event = ImportEvent(file_name=self.file_name)
        self.import_event.save()

        print 'Importing %d records' % len(rows)
        for i, row in enumerate(rows):
            self.handle_row(row)

            j = i + 1
            if j % 50 == 0:
                print 'Loaded %d...' % j
            self.log_verbose("item %d" % i)

        print "Finished data load. "

        print "Calculating new species tree counts... "
        for s in Species.objects.all():
            s.save()
        print "Done."
예제 #13
0
    def test_proximity(self):
        setupTreemapEnv()

        user = User.objects.get(username="******")
        bie1 = ImportEvent(file_name="bie1")
        bie2 = ImportEvent(file_name="bie2")
        bie3 = ImportEvent(file_name="bie3")
        bie4 = ImportEvent(file_name="bie4")

        for bie in [bie1, bie2, bie3, bie4]:
            bie.save()

        p1 = mkPlot(user, geom=Point(25.0000001, 25.0000001))
        p1.import_event = bie1
        p1.save()

        p2 = mkPlot(user, geom=Point(25.0000002, 25.0000002))
        p2.import_event = bie2
        p2.save()

        p3 = mkPlot(user, geom=Point(25.0000003, 25.0000003))
        p3.import_event = bie3
        p3.save()

        p4 = mkPlot(user, geom=Point(27.0000001, 27.0000001))
        p4.import_event = bie4
        p4.save()

        n1 = {p.pk for p in [p1, p2, p3]}
        n2 = {p4.pk}

        i = self.mkrow({'point x': '25.00000025', 'point y': '25.00000025'})
        r = i.validate_row()

        self.assertHasError(i, errors.NEARBY_TREES, n1, set)

        i = self.mkrow({'point x': '27.00000015', 'point y': '27.00000015'})
        r = i.validate_row()

        self.assertHasError(i, errors.NEARBY_TREES, n2, set)

        i = self.mkrow({'point x': '30.00000015', 'point y': '30.00000015'})
        r = i.validate_row()

        self.assertNotHasError(i, errors.NEARBY_TREES)
예제 #14
0
    def handle(self, *args, **options):
        """ Create some seed data """
        instance, user = self.setup_env(*args, **options)

        n = options['n']
        print("Will create %s plots" % n)

        tree_prob = float(max(100, min(0, options['ptree']))) / 100.0
        max_radius = options['radius']

        center_x = instance.center.x
        center_y = instance.center.y

        import_event = ImportEvent(imported_by=user)
        import_event.save()

        ct = 0
        cp = 0
        for i in xrange(0, n):
            mktree = random.random() < tree_prob
            radius = random.gauss(0.0, max_radius)
            theta = random.random() * 2.0 * math.pi

            x = math.cos(theta) * radius + center_x
            y = math.sin(theta) * radius + center_y

            plot = Plot(instance=instance,
                        geom=Point(x, y),
                        import_event=import_event)

            plot.save_with_user(user)
            cp += 1

            if mktree:
                tree = Tree(plot=plot,
                            import_event=import_event,
                            instance=instance)
                tree.save_with_user(user)
                ct += 1

        print("Created %s trees and %s plots" % (ct, cp))
예제 #15
0
    def handle(self, *args, **options):
        self.wrote_headers = False
        try:
            self.file_name = args[0]
            in_file = self.file_name
            err_file = in_file + ".err"
            self.verbose = options.get('verbose')
            self.user_id = args[1]
            if len(args) > 2:
                self.base_srid = int(args[2])
                self.tf = CoordTransform(SpatialReference(self.base_srid), SpatialReference(4326))
                print "Using transformation object: %s" % self.tf
            else:
                self.base_srid = 4326
            if len(args) > 3:
                self.readonly = bool(args[3])
                print "Setting readonly flag to %s" % self.readonly
            else:
                self.readonly = False
        except:
            print "Arguments:  Input_File_Name.[dbf|csv] Data_Owner_User_Id (Optional: Base_SRID ReadOnly-bool)"
            print "Options:    --verbose"
            return

        self.err_writer = csv.writer(open(err_file, 'wb'))

        if self.file_name.endswith('.csv'):
            rows = self.get_csv_rows(in_file)
        if self.file_name.endswith('.dbf'):
            rows = self.get_dbf_rows(in_file)

        self.data_owner = User.objects.get(pk=self.user_id)
        self.updater = User.objects.get(pk=1)

        self.import_event = ImportEvent(file_name=self.file_name)
        self.import_event.save()

        print 'Importing %d records' % len(rows)
        for i, row in enumerate(rows):
            self.handle_row(row)

            j = i + 1
            if j % 50 == 0:
               print 'Loaded %d...' % j
            self.log_verbose("item %d" % i)

        print "Finished data load. "

        print "Calculating new species tree counts... "
        for s in Species.objects.all():
            s.save()
        print "Done."
예제 #16
0
파일: models.py 프로젝트: mmcfarland/OTM2
    def setUp(self):
        self.instance = make_instance(name='Test Instance')

        self.species = Species(instance=self.instance,
                               common_name='Test Common Name',
                               genus='Test Genus',
                               cultivar='Test Cultivar',
                               species='Test Species')
        self.species.save_base()

        self.user = make_user(username='******', password='******')

        self.import_event = ImportEvent(imported_by=self.user)
        self.import_event.save_base()

        self.plot = Plot(geom=Point(0, 0), instance=self.instance,
                         address_street="123 Main Street")

        self.plot.save_base()

        self.tree = Tree(plot=self.plot, instance=self.instance)

        self.tree.save_base()

        self.boundary = make_simple_boundary("Test Boundary")

        self.role = make_commander_role(self.instance)
        self.role.name = "Test Role"
        self.role.save()

        self.field_permission = FieldPermission(
            model_name="Tree",
            field_name="readonly",
            permission_level=FieldPermission.READ_ONLY,
            role=self.role,
            instance=self.instance)
        self.field_permission.save_base()

        self.audit = Audit(action=Audit.Type.Update,
                           model="Tree",
                           field="readonly",
                           model_id=1,
                           user=self.user,
                           previous_value=True,
                           current_value=False)
        self.audit.save_base()

        self.reputation_metric = ReputationMetric(instance=self.instance,
                                                  model_name="Tree",
                                                  action="Test Action")
        self.reputation_metric.save_base()
예제 #17
0
    def setUp(self):
        ######
        # Request/Render mock
        ######
        def local_render_to_response(*args, **kwargs):
            from django.template import loader, RequestContext
            from django.http import HttpResponse

            httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)}
            hr = HttpResponse(loader.render_to_string(*args, **kwargs),
                              **httpresponse_kwargs)

            if hasattr(args[1], 'dicts'):
                hr.request_context = args[1].dicts

            return hr

        django.shortcuts.render_to_response = local_render_to_response

        ######
        # Content types
        ######
        r1 = ReputationAction(name="edit verified", description="blah")
        r2 = ReputationAction(name="edit tree", description="blah")
        r3 = ReputationAction(name="Administrative Action", description="blah")
        r4 = ReputationAction(name="add tree", description="blah")
        r5 = ReputationAction(name="edit plot", description="blah")
        r6 = ReputationAction(name="add plot", description="blah")
        r7 = ReputationAction(name="add stewardship", description="blah")
        r8 = ReputationAction(name="remove stewardship", description="blah")

        self.ra = [r1, r2, r3, r4, r5, r6, r7, r8]

        for r in self.ra:
            r.save()

        ######
        # Set up benefit values
        ######
        bv = BenefitValues(co2=0.02,
                           pm10=9.41,
                           area="InlandValleys",
                           electricity=0.1166,
                           voc=4.69,
                           ozone=5.0032,
                           natural_gas=1.25278,
                           nox=12.79,
                           stormwater=0.0078,
                           sox=3.72,
                           bvoc=4.96)

        bv.save()
        self.bv = bv

        dbh = "[1.0, 2.0, 3.0]"

        rsrc = Resource(meta_species="BDM_OTHER",
                        electricity_dbh=dbh,
                        co2_avoided_dbh=dbh,
                        aq_pm10_dep_dbh=dbh,
                        region="Sim City",
                        aq_voc_avoided_dbh=dbh,
                        aq_pm10_avoided_dbh=dbh,
                        aq_ozone_dep_dbh=dbh,
                        aq_nox_avoided_dbh=dbh,
                        co2_storage_dbh=dbh,
                        aq_sox_avoided_dbh=dbh,
                        aq_sox_dep_dbh=dbh,
                        bvoc_dbh=dbh,
                        co2_sequestered_dbh=dbh,
                        aq_nox_dep_dbh=dbh,
                        hydro_interception_dbh=dbh,
                        natural_gas_dbh=dbh)
        rsrc.save()
        self.rsrc = rsrc

        ######
        # Users
        ######
        u = User.objects.filter(username="******")

        if u:
            u = u[0]
        else:
            u = User.objects.create_user("jim", "*****@*****.**", "jim")
            u.is_staff = True
            u.is_superuser = True
            u.save()
            up = UserProfile(user=u)
            u.reputation = Reputation(user=u)
            u.reputation.save()

        self.u = u

        #######
        # Setup geometries -> Two stacked 100x100 squares
        #######
        n1geom = MultiPolygon(
            Polygon(((0, 0), (100, 0), (100, 100), (0, 100), (0, 0))))
        n2geom = MultiPolygon(
            Polygon(((0, 101), (101, 101), (101, 200), (0, 200), (0, 101))))

        n1 = Neighborhood(name="n1",
                          region_id=2,
                          city="c1",
                          state="PA",
                          county="PAC",
                          geometry=n1geom)
        n2 = Neighborhood(name="n2",
                          region_id=2,
                          city="c2",
                          state="NY",
                          county="NYC",
                          geometry=n2geom)

        n1.save()
        n2.save()

        z1geom = MultiPolygon(
            Polygon(((0, 0), (100, 0), (100, 100), (0, 100), (0, 0))))
        z2geom = MultiPolygon(
            Polygon(((0, 100), (100, 100), (100, 200), (0, 200), (0, 100))))

        z1 = ZipCode(zip="19107", geometry=z1geom)
        z2 = ZipCode(zip="10001", geometry=z2geom)

        z1.save()
        z2.save()

        exgeom1 = MultiPolygon(
            Polygon(((0, 0), (25, 0), (25, 25), (0, 25), (0, 0))))
        ex1 = ExclusionMask(geometry=exgeom1, type="building")

        ex1.save()

        agn1 = AggregateNeighborhood(annual_stormwater_management=0.0,
                                     annual_electricity_conserved=0.0,
                                     annual_energy_conserved=0.0,
                                     annual_natural_gas_conserved=0.0,
                                     annual_air_quality_improvement=0.0,
                                     annual_co2_sequestered=0.0,
                                     annual_co2_avoided=0.0,
                                     annual_co2_reduced=0.0,
                                     total_co2_stored=0.0,
                                     annual_ozone=0.0,
                                     annual_nox=0.0,
                                     annual_pm10=0.0,
                                     annual_sox=0.0,
                                     annual_voc=0.0,
                                     annual_bvoc=0.0,
                                     total_trees=0,
                                     total_plots=0,
                                     location=n1)

        agn2 = AggregateNeighborhood(annual_stormwater_management=0.0,
                                     annual_electricity_conserved=0.0,
                                     annual_energy_conserved=0.0,
                                     annual_natural_gas_conserved=0.0,
                                     annual_air_quality_improvement=0.0,
                                     annual_co2_sequestered=0.0,
                                     annual_co2_avoided=0.0,
                                     annual_co2_reduced=0.0,
                                     total_co2_stored=0.0,
                                     annual_ozone=0.0,
                                     annual_nox=0.0,
                                     annual_pm10=0.0,
                                     annual_sox=0.0,
                                     annual_voc=0.0,
                                     annual_bvoc=0.0,
                                     total_trees=0,
                                     total_plots=0,
                                     location=n2)

        agn1.save()
        agn2.save()

        self.agn1 = agn1
        self.agn2 = agn2

        self.z1 = z1
        self.z2 = z2
        self.n1 = n1
        self.n2 = n2

        ######
        # And we could use a few species...
        ######
        s1 = Species(symbol="s1", genus="testus1", species="specieius1")
        s2 = Species(symbol="s2", genus="testus2", species="specieius2")

        s1.save()
        s2.save()

        self.s1 = s1
        self.s2 = s2

        #######
        # Create some basic plots
        #######
        ie = ImportEvent(file_name='site_add')
        ie.save()

        self.ie = ie

        p1_no_tree = Plot(geometry=Point(50, 50),
                          last_updated_by=u,
                          import_event=ie,
                          present=True,
                          data_owner=u)
        p1_no_tree.save()

        p2_tree = Plot(geometry=Point(51, 51),
                       last_updated_by=u,
                       import_event=ie,
                       present=True,
                       data_owner=u)
        p2_tree.save()

        p3_tree_species1 = Plot(geometry=Point(50, 100),
                                last_updated_by=u,
                                import_event=ie,
                                present=True,
                                data_owner=u)
        p3_tree_species1.save()

        p4_tree_species2 = Plot(geometry=Point(50, 150),
                                last_updated_by=u,
                                import_event=ie,
                                present=True,
                                data_owner=u)
        p4_tree_species2.save()

        t1 = Tree(plot=p2_tree,
                  species=None,
                  last_updated_by=u,
                  import_event=ie)
        t1.present = True
        t1.save()

        t2 = Tree(plot=p3_tree_species1,
                  species=s1,
                  last_updated_by=u,
                  import_event=ie)
        t2.present = True
        t2.save()

        t3 = Tree(plot=p4_tree_species2,
                  species=s2,
                  last_updated_by=u,
                  import_event=ie)
        t3.present = True
        t3.save()

        self.p1_no_tree = p1_no_tree
        self.p2_tree = p2_tree
        self.p3_tree_species1 = p3_tree_species1
        self.p4_tree_species2 = p4_tree_species2

        self.plots = [p1_no_tree, p2_tree, p3_tree_species1, p4_tree_species2]

        self.t1 = t1
        self.t2 = t2
        self.t3 = t3
예제 #18
0
파일: models.py 프로젝트: mmcfarland/OTM2
class ModelUnicodeTests(TestCase):

    def setUp(self):
        self.instance = make_instance(name='Test Instance')

        self.species = Species(instance=self.instance,
                               common_name='Test Common Name',
                               genus='Test Genus',
                               cultivar='Test Cultivar',
                               species='Test Species')
        self.species.save_base()

        self.user = make_user(username='******', password='******')

        self.import_event = ImportEvent(imported_by=self.user)
        self.import_event.save_base()

        self.plot = Plot(geom=Point(0, 0), instance=self.instance,
                         address_street="123 Main Street")

        self.plot.save_base()

        self.tree = Tree(plot=self.plot, instance=self.instance)

        self.tree.save_base()

        self.boundary = make_simple_boundary("Test Boundary")

        self.role = make_commander_role(self.instance)
        self.role.name = "Test Role"
        self.role.save()

        self.field_permission = FieldPermission(
            model_name="Tree",
            field_name="readonly",
            permission_level=FieldPermission.READ_ONLY,
            role=self.role,
            instance=self.instance)
        self.field_permission.save_base()

        self.audit = Audit(action=Audit.Type.Update,
                           model="Tree",
                           field="readonly",
                           model_id=1,
                           user=self.user,
                           previous_value=True,
                           current_value=False)
        self.audit.save_base()

        self.reputation_metric = ReputationMetric(instance=self.instance,
                                                  model_name="Tree",
                                                  action="Test Action")
        self.reputation_metric.save_base()

    def test_instance_model(self):
        self.assertEqual(unicode(self.instance), "Test Instance")

    def test_species_model(self):
        self.assertEqual(
            unicode(self.species),
            "Test Common Name [Test Genus Test Species 'Test Cultivar']")

    def test_user_model(self):
        self.assertEqual(unicode(self.user), 'commander')

    def test_import_event_model(self):
        today = datetime.datetime.today().strftime('%Y-%m-%d')
        self.assertEqual(unicode(self.import_event),
                         'commander - %s' % today)

    def test_plot_model(self):
        self.assertEqual(unicode(self.plot),
                         'X: 0.0, Y: 0.0 - 123 Main Street')

    def test_tree_model(self):
        self.assertEqual(unicode(self.tree), '')

    def test_boundary_model(self):
        self.assertEqual(unicode(self.boundary), 'Test Boundary')

    def test_role_model(self):
        self.assertEqual(unicode(self.role), 'Test Role (%s)' % self.role.pk)

    def test_field_permission_model(self):
        self.assertEqual(unicode(self.field_permission),
                         'Tree.readonly - Test Role (%s)' % self.role.pk)

    def test_audit_model(self):
        self.assertEqual(
            unicode(self.audit),
            'pk=%s - action=Update - Tree.readonly:(1) - True => False'
            % self.audit.pk)

    def test_reputation_metric_model(self):
        self.assertEqual(unicode(self.reputation_metric),
                         'Test Instance - Tree - Test Action')
예제 #19
0
파일: test_utils.py 프로젝트: nittt3/Code
def setupTreemapEnv():
    settings.GEOSERVER_GEO_LAYER = ""
    settings.GEOSERVER_GEO_STYLE = ""
    settings.GEOSERVER_URL = ""

    r1 = ReputationAction(name="edit verified", description="blah")
    r2 = ReputationAction(name="edit tree", description="blah")
    r3 = ReputationAction(name="Administrative Action", description="blah")
    r4 = ReputationAction(name="add tree", description="blah")
    r5 = ReputationAction(name="edit plot", description="blah")
    r6 = ReputationAction(name="add plot", description="blah")

    for r in [r1, r2, r3, r4, r5, r6]:
        r.save()

    bv = BenefitValues(
        co2=0.02,
        pm10=9.41,
        area="InlandValleys",
        electricity=0.1166,
        voc=4.69,
        ozone=5.0032,
        natural_gas=1.25278,
        nox=12.79,
        stormwater=0.0078,
        sox=3.72,
        bvoc=4.96)

    bv.save()

    dbh = "[1.0, 2.0, 3.0]"

    rsrc = Resource(
        meta_species="BDM_OTHER",
        electricity_dbh=dbh,
        co2_avoided_dbh=dbh,
        aq_pm10_dep_dbh=dbh,
        region="Sim City",
        aq_voc_avoided_dbh=dbh,
        aq_pm10_avoided_dbh=dbh,
        aq_ozone_dep_dbh=dbh,
        aq_nox_avoided_dbh=dbh,
        co2_storage_dbh=dbh,
        aq_sox_avoided_dbh=dbh,
        aq_sox_dep_dbh=dbh,
        bvoc_dbh=dbh,
        co2_sequestered_dbh=dbh,
        aq_nox_dep_dbh=dbh,
        hydro_interception_dbh=dbh,
        natural_gas_dbh=dbh)
    rsrc.save()

    u = User.objects.filter(username="******")

    if u:
        u = u[0]
    else:
        u = User.objects.create_user("jim", "*****@*****.**", "jim")
        u.is_staff = True
        u.is_superuser = True
        u.save()
        up = UserProfile(user=u)
        up.save()
        u.reputation = Reputation(user=u)
        u.reputation.save()

    amy_filter_result = User.objects.filter(username="******")
    if not amy_filter_result:
        amy = User.objects.create_user("amy", "*****@*****.**", "amy")
    else:
        amy = amy_filter_result[0]
    amy.is_staff = False
    amy.is_superuser = False
    amy.save()
    amy_profile = UserProfile(user=amy)
    amy_profile.save()
    amy.reputation = Reputation(user=amy)
    amy.reputation.save()

    n1geom = MultiPolygon(
        Polygon(((0, 0), (100, 0), (100, 100), (0, 100), (0, 0))))
    n2geom = MultiPolygon(
        Polygon(((0, 101), (101, 101), (101, 200), (0, 200), (0, 101))))

    n1 = Neighborhood(
        name="n1",
        region_id=2,
        city="c1",
        state="PA",
        county="PAC",
        geometry=n1geom)
    n2 = Neighborhood(
        name="n2",
        region_id=2,
        city="c2",
        state="NY",
        county="NYC",
        geometry=n2geom)

    n1.save()
    n2.save()

    z1geom = MultiPolygon(
        Polygon(((0, 0), (100, 0), (100, 100), (0, 100), (0, 0))))
    z2geom = MultiPolygon(
        Polygon(((0, 100), (100, 100), (100, 200), (0, 200), (0, 100))))

    z1 = ZipCode(zip="19107", geometry=z1geom)
    z2 = ZipCode(zip="10001", geometry=z2geom)

    z1.save()
    z2.save()

    exgeom1 = MultiPolygon(
        Polygon(((0, 0), (25, 0), (25, 25), (0, 25), (0, 0))))
    ex1 = ExclusionMask(geometry=exgeom1, type="building")

    ex1.save()

    agn1 = AggregateNeighborhood(
        annual_stormwater_management=0.0,
        annual_electricity_conserved=0.0,
        annual_energy_conserved=0.0,
        annual_natural_gas_conserved=0.0,
        annual_air_quality_improvement=0.0,
        annual_co2_sequestered=0.0,
        annual_co2_avoided=0.0,
        annual_co2_reduced=0.0,
        total_co2_stored=0.0,
        annual_ozone=0.0,
        annual_nox=0.0,
        annual_pm10=0.0,
        annual_sox=0.0,
        annual_voc=0.0,
        annual_bvoc=0.0,
        total_trees=0,
        total_plots=0,
        location=n1)

    agn2 = AggregateNeighborhood(
        annual_stormwater_management=0.0,
        annual_electricity_conserved=0.0,
        annual_energy_conserved=0.0,
        annual_natural_gas_conserved=0.0,
        annual_air_quality_improvement=0.0,
        annual_co2_sequestered=0.0,
        annual_co2_avoided=0.0,
        annual_co2_reduced=0.0,
        total_co2_stored=0.0,
        annual_ozone=0.0,
        annual_nox=0.0,
        annual_pm10=0.0,
        annual_sox=0.0,
        annual_voc=0.0,
        annual_bvoc=0.0,
        total_trees=0,
        total_plots=0,
        location=n2)

    agn1.save()
    agn2.save()

    s1 = Species(symbol="s1", genus="testus1", species="specieius1")
    s2 = Species(symbol="s2", genus="testus2", species="specieius2")

    s1.save()
    s2.save()

    ie = ImportEvent(file_name='site_add')
    ie.save()
예제 #20
0
    def handle(self, *args, **options):
        """ Create some seed data """
        instance = Instance.objects.get(pk=options['instance'])

        user = User.objects.filter(is_superuser=True)

        if len(user) == 0:
            print('Error: Could not find a superuser to use')
            return 1
        else:
            user = user[0]

        if user.roles.count() == 0:
            print('Added global role to user')
            r = Role(name='global', rep_thresh=0, instance=instance)
            r.save()
            user.roles.add(r)
            user.save()

        for field in ('geom', 'created_by', 'import_event'):
            _, c = FieldPermission.objects.get_or_create(
                model_name='Plot',
                field_name=field,
                role=user.roles.all()[0],
                instance=instance,
                permission_level=FieldPermission.WRITE_DIRECTLY)
            if c:
                print('Created plot permission for field "%s"' % field)

        for field in ('plot', 'created_by'):
            _, c = FieldPermission.objects.get_or_create(
                model_name='Tree',
                field_name=field,
                role=user.roles.all()[0],
                instance=instance,
                permission_level=FieldPermission.WRITE_DIRECTLY)
            if c:
                print('Created tree permission for field "%s"' % field)

        dt = 0
        dp = 0
        if options.get('delete', False):
            for t in Tree.objects.all():
                t.delete_with_user(user)
                dt += 1
            for p in Plot.objects.all():
                p.delete_with_user(user)
                dp += 1

            print("Deleted %s trees and %s plots" % (dt, dp))

        n = options['n']
        print("Will create %s plots" % n)

        tree_prob = float(max(100, min(0, options['ptree']))) / 100.0
        max_radius = options['radius']

        center_x = instance.center.x
        center_y = instance.center.y

        import_event = ImportEvent(imported_by=user)
        import_event.save()

        ct = 0
        cp = 0
        for i in xrange(0, n):
            mktree = random.random() < tree_prob
            radius = random.gauss(0.0, max_radius)
            theta = random.random() * 2.0 * math.pi

            x = math.cos(theta) * radius + center_x
            y = math.sin(theta) * radius + center_y

            plot = Plot(instance=instance,
                        geom=Point(x, y),
                        created_by=user,
                        import_event=import_event)

            plot.save_with_user(user)
            cp += 1

            if mktree:
                tree = Tree(plot=plot,
                            created_by=user,
                            import_event=import_event,
                            instance=instance)
                tree.save_with_user(user)
                ct += 1

        print("Created %s trees and %s plots" % (ct, cp))
예제 #21
0
    def setUp(self):
        ######
        # Request/Render mock
        ######
        def local_render_to_response(*args, **kwargs):
            from django.template import loader, RequestContext
            from django.http import HttpResponse

            httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)}
            hr = HttpResponse(
                loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)

            if hasattr(args[1], 'dicts'):
                hr.request_context = args[1].dicts

            return hr

        django.shortcuts.render_to_response = local_render_to_response
    
        ######
        # Content types
        ######
        r1 = ReputationAction(name="edit verified", description="blah")
        r2 = ReputationAction(name="edit tree", description="blah")
        r3 = ReputationAction(name="Administrative Action", description="blah")
        r4 = ReputationAction(name="add tree", description="blah")
        r5 = ReputationAction(name="edit plot", description="blah")
        r6 = ReputationAction(name="add plot", description="blah")
        r7 = ReputationAction(name="add stewardship", description="blah")
        r8 = ReputationAction(name="remove stewardship", description="blah")

        self.ra = [r1,r2,r3,r4,r5,r6,r7,r8]

        for r in self.ra:
            r.save()

        ######
        # Set up benefit values
        ######
        bv = BenefitValues(co2=0.02, pm10=9.41, area="InlandValleys",
                           electricity=0.1166,voc=4.69,ozone=5.0032,natural_gas=1.25278,
                           nox=12.79,stormwater=0.0078,sox=3.72,bvoc=4.96)

        bv.save()
        self.bv = bv


        dbh = "[1.0, 2.0, 3.0]"

        rsrc = Resource(meta_species="BDM_OTHER", electricity_dbh=dbh, co2_avoided_dbh=dbh,
                        aq_pm10_dep_dbh=dbh, region="Sim City", aq_voc_avoided_dbh=dbh,
                        aq_pm10_avoided_dbh=dbh, aq_ozone_dep_dbh=dbh, aq_nox_avoided_dbh=dbh,
                        co2_storage_dbh=dbh,aq_sox_avoided_dbh=dbh, aq_sox_dep_dbh=dbh,
                        bvoc_dbh=dbh, co2_sequestered_dbh=dbh, aq_nox_dep_dbh=dbh,
                        hydro_interception_dbh=dbh, natural_gas_dbh=dbh)
        rsrc.save()
        self.rsrc = rsrc

        ######
        # Users
        ######
        u = User.objects.filter(username="******")
            
        if u:
            u = u[0]
        else:
            u = User.objects.create_user("jim","*****@*****.**","jim")
            u.is_staff = True
            u.is_superuser = True
            u.save()
            up = UserProfile(user=u)
            u.reputation = Reputation(user=u)
            u.reputation.save()

        self.u = u
        

        #######
        # Setup geometries -> Two stacked 100x100 squares
        #######
        n1geom = MultiPolygon(Polygon(((0,0),(100,0),(100,100),(0,100),(0,0))))
        n2geom = MultiPolygon(Polygon(((0,101),(101,101),(101,200),(0,200),(0,101))))

        n1 = Neighborhood(name="n1", region_id=2, city="c1", state="PA", county="PAC", geometry=n1geom)
        n2 = Neighborhood(name="n2", region_id=2, city="c2", state="NY", county="NYC", geometry=n2geom)

        n1.save()
        n2.save()

        z1geom = MultiPolygon(Polygon(((0,0),(100,0),(100,100),(0,100),(0,0))))
        z2geom = MultiPolygon(Polygon(((0,100),(100,100),(100,200),(0,200),(0,100))))

        z1 = ZipCode(zip="19107",geometry=z1geom)
        z2 = ZipCode(zip="10001",geometry=z2geom)

        z1.save()
        z2.save()

        exgeom1 = MultiPolygon(Polygon(((0,0),(25,0),(25,25),(0,25),(0,0))))
        ex1 = ExclusionMask(geometry=exgeom1, type="building")

        ex1.save()

        agn1 = AggregateNeighborhood(
            annual_stormwater_management=0.0,
            annual_electricity_conserved=0.0,
            annual_energy_conserved=0.0,
            annual_natural_gas_conserved=0.0,
            annual_air_quality_improvement=0.0,
            annual_co2_sequestered=0.0,
            annual_co2_avoided=0.0,
            annual_co2_reduced=0.0,
            total_co2_stored=0.0,
            annual_ozone=0.0,
            annual_nox=0.0,
            annual_pm10=0.0,
            annual_sox=0.0,
            annual_voc=0.0,
            annual_bvoc=0.0,
            total_trees=0,
            total_plots=0,
            location = n1)

        agn2 = AggregateNeighborhood(
            annual_stormwater_management=0.0,
            annual_electricity_conserved=0.0,
            annual_energy_conserved=0.0,
            annual_natural_gas_conserved=0.0,
            annual_air_quality_improvement=0.0,
            annual_co2_sequestered=0.0,
            annual_co2_avoided=0.0,
            annual_co2_reduced=0.0,
            total_co2_stored=0.0,
            annual_ozone=0.0,
            annual_nox=0.0,
            annual_pm10=0.0,
            annual_sox=0.0,
            annual_voc=0.0,
            annual_bvoc=0.0,
            total_trees=0,
            total_plots=0,
            location = n2)

        agn1.save()
        agn2.save()

        self.agn1 = agn1
        self.agn2 = agn2

        self.z1 = z1
        self.z2 = z2
        self.n1 = n1
        self.n2 = n2

        ######
        # And we could use a few species...
        ######
        s1 = Species(symbol="s1",genus="testus1",species="specieius1")
        s2 = Species(symbol="s2",genus="testus2",species="specieius2")
        
        s1.save()
        s2.save()

        self.s1 = s1
        self.s2 = s2

        #######
        # Create some basic plots
        #######
        ie = ImportEvent(file_name='site_add')
        ie.save()

        self.ie = ie

        p1_no_tree = Plot(geometry=Point(50,50), last_updated_by=u, import_event=ie,present=True, data_owner=u)
        p1_no_tree.save()

        p2_tree = Plot(geometry=Point(51,51), last_updated_by=u, import_event=ie,present=True, data_owner=u)
        p2_tree.save()

        p3_tree_species1 = Plot(geometry=Point(50,100), last_updated_by=u, import_event=ie,present=True, data_owner=u)
        p3_tree_species1.save()

        p4_tree_species2 = Plot(geometry=Point(50,150), last_updated_by=u, import_event=ie,present=True, data_owner=u)
        p4_tree_species2.save()

        t1 = Tree(plot=p2_tree, species=None, last_updated_by=u, import_event=ie)
        t1.present = True
        t1.save()
        
        t2 = Tree(plot=p3_tree_species1, species=s1, last_updated_by=u, import_event=ie)
        t2.present = True
        t2.save()

        t3 = Tree(plot=p4_tree_species2, species=s2, last_updated_by=u, import_event=ie)
        t3.present = True
        t3.save()

        self.p1_no_tree = p1_no_tree
        self.p2_tree = p2_tree
        self.p3_tree_species1 = p3_tree_species1;
        self.p4_tree_species2 = p4_tree_species2;

        self.plots = [p1_no_tree, p2_tree, p3_tree_species1, p4_tree_species2]

        self.t1 = t1
        self.t2 = t2
        self.t3 = t3
예제 #22
0
class Command(BaseCommand):
    args = '<input_file_name, data_owner_id, base_srid, read_only>'
    option_list = BaseCommand.option_list + (make_option(
        '--verbose',
        action='store_true',
        dest='verbose',
        default=False,
        help='Show verbose debug text'), )

    def write_headers_if_needed(self):
        if not self.wrote_headers:
            self.err_writer.writerow(self.headers)
            self.wrote_headers = True

    def get_dbf_rows(self, in_file):
        reader = dbf.Dbf(in_file)
        print 'Opening input file: %s ' % in_file

        self.headers = reader.fieldNames

        print 'Converting input file to dictionary'
        rows = []
        for dbf_row in reader:
            d = {}
            for name in self.headers:
                d[name] = dbf_row[name]
            rows.append(d)
        return rows

    def get_csv_rows(self, in_file):
        reader = csv.DictReader(open(in_file, 'r'), restval=123)
        print 'Opening input file: %s ' % in_file

        rows = list(reader)

        self.headers = reader.fieldnames

        return rows

    def handle(self, *args, **options):
        self.wrote_headers = False
        try:
            self.file_name = args[0]
            in_file = self.file_name
            err_file = in_file + ".err"
            self.verbose = options.get('verbose')
            self.user_id = args[1]
            if len(args) > 2:
                self.base_srid = int(args[2])
                self.tf = CoordTransform(SpatialReference(self.base_srid),
                                         SpatialReference(4326))
                print "Using transformaton object: %s" % self.tf
            else:
                self.base_srid = 4326
            if len(args) > 3:
                self.readonly = bool(args[3])
                print "Setting readonly flag to %s" % self.readonly
            else:
                self.readonly = False
        except:
            print "Arguments:  Input_File_Name.[dbf|csv], Data_Owner_User_Id, (Base_SRID optional)"
            print "Options:    --verbose"
            return

        self.err_writer = csv.writer(open(err_file, 'wb'))

        if self.file_name.endswith('.csv'):
            rows = self.get_csv_rows(in_file)
        if self.file_name.endswith('.dbf'):
            rows = self.get_dbf_rows(in_file)

        self.data_owner = User.objects.get(pk=self.user_id)
        self.updater = User.objects.get(pk=1)

        self.import_event = ImportEvent(file_name=self.file_name)
        self.import_event.save()

        print 'Importing %d records' % len(rows)
        for i, row in enumerate(rows):
            self.handle_row(row)

            j = i + 1
            if j % 50 == 0:
                print 'Loaded %d...' % j
            self.log_verbose("item %d" % i)

        print "Finished data load. "

        print "Calculating new species tree counts... "
        for s in Species.objects.all():
            s.save()
        print "Done."

    def log_verbose(self, msg):
        if self.verbose: print msg

    def log_error(self, msg, row):
        print "ERROR: %s" % msg
        columns = [row[s] for s in self.headers]
        self.write_headers_if_needed()
        self.err_writer.writerow(columns)

    def check_coords(self, row):
        try:
            x = float(row.get('POINT_X', 0))
            y = float(row.get('POINT_Y', 0))
        except:
            self.log_error("  Invalid coords, might not be numbers", row)
            return (False, 0, 0)

        ok = x and y
        if not ok:
            self.log_error("  Invalid coords", row)
        self.log_verbose("  Passed coordinate check")
        return (ok, x, y)

    def check_species(self, row):
        # locate the species and instanciate the tree instance
        if not row.get('SCIENTIFIC') and not row.get('GENUS'):
            self.log_verbose("  No species information")
            return (True, None)

        if row.get('SCIENTIFIC'):
            name = str(row['SCIENTIFIC']).strip()
            species = Species.objects.filter(scientific_name__iexact=name)
            self.log_verbose("  Looking for species: %s" % name)
        else:
            genus = str(row['GENUS']).strip()
            species = ''
            cultivar = ''
            gender = ''
            name = genus
            if row.get('SPECIES'):
                species = str(row['SPECIES']).strip()
                name = name + " " + species
            if row.get('CULTIVAR'):
                cultivar = str(row['CULTIVAR']).strip()
                name = name + " " + cultivar
            if row.get('GENDER'):
                gender = str(row['GENDER']).strip()
                name = name + " " + gender
            species = Species.objects.filter(scientific_name__iexact=name)
            self.log_verbose("  Looking for species: %s %s %s %s" %
                             (genus, species, cultivar, gender))

        if species:  #species match found
            self.log_verbose("  Found species %r" % species[0])
            return (True, species)

        #species data but no match, check it manually
        self.log_error("ERROR:  Unknown species %r" % name, row)
        return (False, None)

    def check_tree_info(self, row):
        tree_info = False
        fields = [
            'STEWARD', 'SPONSOR', 'DATEPLANTED', 'DIAMETER', 'HEIGHT',
            'CANOPYHEIGHT', 'CONDITION', 'CANOPYCONDITION', 'PROJECT_1',
            'PROJECT_2', 'PROJECT_3', 'OWNER'
        ]

        for f in fields:
            # field exists and there's something in it
            if row.get(f) and str(row[f]).strip():
                tree_info = True
                self.log_verbose(
                    '  Found tree data in field %s, creating a tree' % f)
                break

        return tree_info

    def check_proximity(self, plot, tree, species, row):
        # check for nearby plots
        collisions = plot.validate_proximity(True, 0)

        # if there are no collisions, then proceed as planned
        if not collisions:
            self.log_verbose("  No collisions found")
            return (True, plot, tree)
        self.log_verbose("  Initial proximity test count: %d" %
                         collisions.count())

        # exclude collisions from the same file we're working in
        collisions = collisions.exclude(import_event=self.import_event)
        if collisions:
            self.log_verbose("  All collisions are from this import file")
            return (True, plot, tree)
        self.log_verbose("  Secondary proximity test count: %d" %
                         collisions.count())

        # if we have multiple collitions, check for same species or unknown species
        # and try to associate with one of them otherwise abort
        if collisions.count() > 1:
            # get existing trees for the plots that we collided with
            tree_ids = []
            for c in collisions:
                if c.current_tree():
                    tree_ids.append(c.current_tree().id)

            trees = Tree.objects.filter(id__in=tree_ids)

            # Precedence: single same species, single unknown
            # return false for all others and log
            if species:
                same = trees.filter(species=species[0])
                if same.count() == 1 and same[0].species == species[0]:
                    self.log_verbose(
                        "  Using single nearby plot with tree of same species")
                    return (True, c, same[0])

            unk = trees.filter(species=None)

            if unk.count() == 1:
                self.log_verbose(
                    "  Using single nearby plot with tree of unknown species")
                return (True, c, unk[0])

            self.log_error(
                "ERROR:  Proximity test failed (near %d plots)" %
                collisions.count(), row)
            return (False, None, None)

        # one nearby match found, use it as base
        plot = collisions[0]
        plot_tree = plot.current_tree()
        self.log_verbose("  Found one tree nearby")

        # if the nearby plot doesn't have a tree, don't bother doing species matching
        if not plot_tree:
            self.log_verbose(
                "  No tree found for plot, using %d as base plot record" %
                plot.id)
            return (True, plot, tree)

        # if neither have a species, then we're done and we need to use
        # the tree we collided with.
        if not plot_tree.species and not species:
            self.log_verbose(
                "  No species info for either record, using %d as base tree record"
                % plot_tree.id)
            return (True, plot, plot_tree)

        # if only the new one has a species, update the tree we collided
        # with and then return it
        if not plot_tree.species:
            # we need to update the collision tree with the new species
            plot_tree.set_species(species[0].id, False)  # save later
            self.log_verbose("  Species match, using update file species: %s" %
                             species[0])
            return (True, plot, plot_tree)

        # if only the collision tree has a species, we're done.
        if not species or species.count() == 0:
            self.log_verbose(
                "  No species info for import record, using %d as base record"
                % plot_tree.id)
            return (True, plot, plot_tree)

        # in this case, both had a species. we should check to see if
        # the species are the same.
        if plot_tree.species != species[0]:
            # now that we have a new species, we want to update the
            # collision tree's species and delete all the old status
            # information.
            self.log_verbose(
                "  Species do not match, using update file species: %s" %
                species[0])
            plot_tree.set_species(species[0].id, False)
            plot_tree.dbh = None
            plot_tree.height = None
            plot_tree.canopy_height = None
            plot_tree.condition = None
            plot_tree.canopy_condition = None
        return (True, plot, plot_tree)

    def handle_row(self, row):
        self.log_verbose(row)

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

        plot = Plot()

        try:
            if self.base_srid != 4326:
                geom = Point(x, y, srid=self.base_srid)
                geom.transform(self.tf)
                self.log_verbose(geom)
                plot.geometry = geom
            else:
                plot.geometry = Point(x, y, srid=4326)
        except:
            self.log_error("ERROR: Geometry failed to transform", row)
            return

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

        # check for tree info, should we create a tree or just a plot
        if species or self.check_tree_info(row):
            tree = Tree(plot=plot)
        else:
            tree = None

        if tree and species:
            tree.species = species[0]

        # check the proximity (try to match up with existing trees)
        # this may return a different plot/tree than created just above,
        # so don't set anything else on either until after this point
        ok, plot, tree = self.check_proximity(plot, tree, species, row)
        if not ok: return

        if row.get('ADDRESS') and not plot.address_street:
            plot.address_street = str(row['ADDRESS']).title()
            plot.geocoded_address = str(row['ADDRESS']).title()

        if not plot.geocoded_address:
            plot.geocoded_address = ""

        # FIXME: get this from the config?
        plot.address_state = 'CA'
        plot.import_event = self.import_event
        plot.last_updated_by = self.updater
        plot.data_owner = self.data_owner
        plot.readonly = self.readonly

        if row.get('PLOTTYPE'):
            for k, v in choices['plot_types']:
                if v == row['PLOTTYPE']:
                    plot.type = k
                    break

        if row.get('PLOTLENGTH'):
            plot.length = row['PLOTLENGTH']

        if row.get('PLOTWIDTH'):
            plot.width = row['PLOTWIDTH']

        if row.get('ID'):
            plot.owner_orig_id = row['ID']

        if row.get('ORIGID'):
            plot.owner_additional_properties = "ORIGID=" + str(row['ORIGID'])

        if row.get('OWNER_ADDITIONAL_PROPERTIES'):
            plot.owner_additional_properties = str(
                plot.owner_additional_properties) + " " + str(
                    row['OWNER_ADDITIONAL_PROPERTIES'])

        if row.get('OWNER_ADDITIONAL_ID'):
            plot.owner_additional_id = str(row['OWNER_ADDITIONAL_ID'])

        if row.get('POWERLINE'):
            for k, v in choices['powerlines']:
                if v == row['POWERLINE']:
                    plot.powerline = k
                    break

        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':
            plot.sidewalk_damage = 2
        else:
            plot.sidewalk_damage = 1

        plot.quick_save()

        pnt = plot.geometry
        n = Neighborhood.objects.filter(geometry__contains=pnt)
        z = ZipCode.objects.filter(geometry__contains=pnt)

        plot.neighborhoods = ""
        plot.neighborhood.clear()
        plot.zipcode = None

        if n:
            for nhood in n:
                if nhood:
                    plot.neighborhoods = plot.neighborhoods + " " + nhood.id.__str__(
                    )
                    plot.neighborhood.add(nhood)

        if z: plot.zipcode = z[0]

        plot.quick_save()

        if tree:
            tree.plot = plot
            tree.readonly = self.readonly
            tree.import_event = self.import_event
            tree.last_updated_by = self.updater

            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_string = str(row['DATEPLANTED'])
                try:
                    date = datetime.strptime(date_string, "%m/%d/%Y")
                except:
                    pass
                try:
                    date = datetime.strptime(date_string, "%Y/%m/%d")
                except:
                    pass
                if not date:
                    raise ValueError(
                        "Date strings must be in mm/dd/yyyy or yyyy/mm/dd format"
                    )

                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['conditions']:
                    if v == row['CONDITION']:
                        tree.condition = k
                        break

            if row.get('CANOPYCONDITION'):
                for k, v in choices['canopy_conditions']:
                    if v == row['CANOPYCONDITION']:
                        tree.canopy_condition = k
                        break
            # FOR OTM INDIA
            # GIRTH_CM", "GIRTH_M", "HEIGHT_FT", "HEIGHT_M", "NEST", "BURROWS", "FLOWERS", "FRUITS", "NAILS", "POSTER", "WIRES", "TREE_GUARD", "NUISANCE", "NUISANCE_DESC", "HEALTH_OF_TREE", "FOUND_ON_GROUND", "GROUND_DESCRIPTION", "RISK_ON_TREE", "RISK_DESC", "RARE", "ENDANGERED", "VULNERABLE", "PEST_AFFECTED", "REFER_TO_DEPT", "SPECIAL_OTHER", "SPECIAL_OTHER_DESCRIPTION", "LATITUDE", "LONGITUDE", "CREATION_DATE", "DEVICE_ID", "TIME", "DATE"])

            if row.get('GIRTH_M'):
                tree.girth_m = float(row['GIRTH_M'])

            if row.get('HEIGHT_M'):
                tree.height_m = float(row['HEIGHT_M'])

            if row.get('NEST'):
                tree.nest = str(row['NEST'])

            if row.get('BURROWS'):
                tree.burrows = str(row['BURROWS'])

            if row.get('FLOWERS'):
                tree.flowers = str(row['FLOWERS'])

            if row.get('FRUITS'):
                tree.fruits = str(row['FRUITS'])

            if row.get('NAILS'):
                tree.nails = str(row['NAILS'])

            if row.get('POSTER'):
                tree.poster = str(row['POSTER'])

            if row.get('WIRES'):
                tree.wires = str(row['WIRES'])

            if row.get('TREE_GUARD'):
                tree.tree_guard = str(row['TREE_GUARD'])

            if row.get('NUISANCE'):
                tree.other_nuisance = bool(row['NUISANCE'])

            if row.get('NUISANCE_DESC'):
                tree.other_nuisance_desc = str(row['NUISANCE_DESC'])

            if row.get('HEALTH_OF_TREE'):
                tree.health_of_tree = str(row['HEALTH_OF_TREE'])

            if row.get('FOUND_ON_GROUND'):
                tree.found_on_ground = str(row['FOUND_ON_GROUND'])

            if row.get('GROUND_DESCRIPTION'):
                tree.ground_description = str(row['GROUND_DESCRIPTION'])

            if row.get('RISK_ON_TREE'):
                tree.risk_on_tree = str(row['RISK_ON_TREE'])

            if row.get('RISK_DESC'):
                tree.risk_desc = str(row['RISK_DESC'])

            if row.get('PEST_AFFECTED'):
                tree.pests = str(row['PEST_AFFECTED'])

            if row.get('REFER_TO_DEPT'):
                tree.refer_to_dept = str(row['REFER_TO_DEPT'])

            if row.get('SPECIAL_OTHER'):
                tree.special_other = str(row['SPECIAL_OTHER'])

            if row.get('SPECIAL_OTHER_DESCRIPTION'):
                tree.special_other_description = str(
                    row['SPECIAL_OTHER_DESCRIPTION'])

            if row.get('LATITUDE'):
                tree.latitude = str(row['LATITUDE'])

            if row.get('LONGITUDE'):
                tree.longitude = str(row['LONGITUDE'])

            if row.get('PRABHAG_ID'):
                tree.prabhag_id = str(row['PRABHAG_ID'])

            if row.get('CLUSTER_ID'):
                tree.cluster_id = str(row['CLUSTER_ID'])

            #if row.get('ID'):
            #    tree.id = str(row['ID'])

            #import pdb; pdb.set_trace()

            f = open("trees_in_otm_obj.log", "w")
            f.write("b4 save")
            f.write(str(tree.__dict__))
            tree.quick_save()
            f.write("after save \n")
            f.write(str(tree.__dict__))
            f.close()

            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()
예제 #23
0
def setupTreemapEnv():
    Choices(field="plot_type", key="blah", value="blah", key_type="str").save()

    r1 = ReputationAction(name="edit verified", description="blah")
    r2 = ReputationAction(name="edit tree", description="blah")
    r3 = ReputationAction(name="Administrative Action", description="blah")
    r4 = ReputationAction(name="add tree", description="blah")
    r5 = ReputationAction(name="edit plot", description="blah")
    r6 = ReputationAction(name="add plot", description="blah")

    for r in [r1, r2, r3, r4, r5, r6]:
        r.save()

    bv = BenefitValues(co2=0.02,
                       pm10=9.41,
                       area="InlandValleys",
                       electricity=0.1166,
                       voc=4.69,
                       ozone=5.0032,
                       natural_gas=1.25278,
                       nox=12.79,
                       stormwater=0.0078,
                       sox=3.72,
                       bvoc=4.96)

    bv.save()

    dbh = "[1.0, 2.0, 3.0]"

    rsrc = Resource(meta_species="BDM_OTHER",
                    electricity_dbh=dbh,
                    co2_avoided_dbh=dbh,
                    aq_pm10_dep_dbh=dbh,
                    region="Sim City",
                    aq_voc_avoided_dbh=dbh,
                    aq_pm10_avoided_dbh=dbh,
                    aq_ozone_dep_dbh=dbh,
                    aq_nox_avoided_dbh=dbh,
                    co2_storage_dbh=dbh,
                    aq_sox_avoided_dbh=dbh,
                    aq_sox_dep_dbh=dbh,
                    bvoc_dbh=dbh,
                    co2_sequestered_dbh=dbh,
                    aq_nox_dep_dbh=dbh,
                    hydro_interception_dbh=dbh,
                    natural_gas_dbh=dbh)
    rsrc.save()

    u = User.objects.filter(username="******")

    if u:
        u = u[0]
    else:
        u = User.objects.create_user("jim", "*****@*****.**", "jim")
        u.is_staff = True
        u.is_superuser = True
        u.save()
        up = UserProfile(user=u)
        u.reputation = Reputation(user=u)
        u.reputation.save()

    n1geom = MultiPolygon(
        Polygon(((0, 0), (100, 0), (100, 100), (0, 100), (0, 0))))
    n2geom = MultiPolygon(
        Polygon(((0, 101), (101, 101), (101, 200), (0, 200), (0, 101))))

    n1 = Neighborhood(name="n1",
                      region_id=2,
                      city="c1",
                      state="PA",
                      county="PAC",
                      geometry=n1geom)
    n2 = Neighborhood(name="n2",
                      region_id=2,
                      city="c2",
                      state="NY",
                      county="NYC",
                      geometry=n2geom)

    n1.save()
    n2.save()

    z1geom = MultiPolygon(
        Polygon(((0, 0), (100, 0), (100, 100), (0, 100), (0, 0))))
    z2geom = MultiPolygon(
        Polygon(((0, 100), (100, 100), (100, 200), (0, 200), (0, 100))))

    z1 = ZipCode(zip="19107", geometry=z1geom)
    z2 = ZipCode(zip="10001", geometry=z2geom)

    z1.save()
    z2.save()

    exgeom1 = MultiPolygon(
        Polygon(((0, 0), (25, 0), (25, 25), (0, 25), (0, 0))))
    ex1 = ExclusionMask(geometry=exgeom1, type="building")

    ex1.save()

    agn1 = AggregateNeighborhood(annual_stormwater_management=0.0,
                                 annual_electricity_conserved=0.0,
                                 annual_energy_conserved=0.0,
                                 annual_natural_gas_conserved=0.0,
                                 annual_air_quality_improvement=0.0,
                                 annual_co2_sequestered=0.0,
                                 annual_co2_avoided=0.0,
                                 annual_co2_reduced=0.0,
                                 total_co2_stored=0.0,
                                 annual_ozone=0.0,
                                 annual_nox=0.0,
                                 annual_pm10=0.0,
                                 annual_sox=0.0,
                                 annual_voc=0.0,
                                 annual_bvoc=0.0,
                                 total_trees=0,
                                 total_plots=0,
                                 location=n1)

    agn2 = AggregateNeighborhood(annual_stormwater_management=0.0,
                                 annual_electricity_conserved=0.0,
                                 annual_energy_conserved=0.0,
                                 annual_natural_gas_conserved=0.0,
                                 annual_air_quality_improvement=0.0,
                                 annual_co2_sequestered=0.0,
                                 annual_co2_avoided=0.0,
                                 annual_co2_reduced=0.0,
                                 total_co2_stored=0.0,
                                 annual_ozone=0.0,
                                 annual_nox=0.0,
                                 annual_pm10=0.0,
                                 annual_sox=0.0,
                                 annual_voc=0.0,
                                 annual_bvoc=0.0,
                                 total_trees=0,
                                 total_plots=0,
                                 location=n2)

    agn1.save()
    agn2.save()

    s1 = Species(symbol="s1", genus="testus1", species="specieius1")
    s2 = Species(symbol="s2", genus="testus2", species="specieius2")

    s1.save()
    s2.save()

    ie = ImportEvent(file_name='site_add')
    ie.save()
예제 #24
0
def setupTreemapEnv():
    settings.GEOSERVER_GEO_LAYER = ""
    settings.GEOSERVER_GEO_STYLE = ""
    settings.GEOSERVER_URL = ""

    def local_render_to_response(*args, **kwargs):
        from django.template import loader, RequestContext
        from django.http import HttpResponse

        httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)}
        hr = HttpResponse(loader.render_to_string(*args, **kwargs),
                          **httpresponse_kwargs)

        if hasattr(args[1], 'dicts'):
            hr.request_context = args[1].dicts

        return hr

    django.shortcuts.render_to_response = local_render_to_response

    r1 = ReputationAction(name="edit verified", description="blah")
    r2 = ReputationAction(name="edit tree", description="blah")
    r3 = ReputationAction(name="Administrative Action", description="blah")
    r4 = ReputationAction(name="add tree", description="blah")
    r5 = ReputationAction(name="edit plot", description="blah")
    r6 = ReputationAction(name="add plot", description="blah")
    r7 = ReputationAction(name="add stewardship", description="blah")
    r8 = ReputationAction(name="remove stewardship", description="blah")

    for r in [r1, r2, r3, r4, r5, r6, r7, r8]:
        r.save()

    bv = BenefitValues(co2=0.02,
                       pm10=9.41,
                       area="InlandValleys",
                       electricity=0.1166,
                       voc=4.69,
                       ozone=5.0032,
                       natural_gas=1.25278,
                       nox=12.79,
                       stormwater=0.0078,
                       sox=3.72,
                       bvoc=4.96)

    bv.save()

    dbh = "[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]"
    dbh2 = "[2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]"

    rsrc1 = Resource(meta_species="BDM OTHER", region="NoEastXXX")
    rsrc2 = Resource(meta_species="BDL OTHER", region="NoEastXXX")

    rsrc1.save()
    rsrc2.save()

    u = User.objects.filter(username="******")

    if u:
        u = u[0]
    else:
        u = User.objects.create_user("jim", "*****@*****.**", "jim")
        u.is_staff = True
        u.is_superuser = True
        u.save()
        up = UserProfile(user=u)
        up.save()
        u.reputation = Reputation(user=u)
        u.reputation.save()

    amy_filter_result = User.objects.filter(username="******")
    if not amy_filter_result:
        amy = User.objects.create_user("amy", "*****@*****.**", "amy")
    else:
        amy = amy_filter_result[0]
        amy.is_staff = False
        amy.is_superuser = False
        amy.save()
        amy_profile = UserProfile(user=amy)
        amy_profile.save()
        amy.reputation = Reputation(user=amy)
        amy.reputation.save()

    olivia_filter_result = User.objects.filter(username="******")
    if not amy_filter_result:
        olivia = User.objects.create_user("olivia", "*****@*****.**",
                                          "olivia")
    else:
        olivia = olivia_filter_result[0]
        olivia.is_staff = False
        olivia.is_superuser = False
        olivia.save()
        olivia_profile = UserProfile(user=olivia)
        olivia_profile.save()
        olivia.reputation = Reputation(user=olivia)
        olivia.reputation.save()

    n1geom = MultiPolygon(
        Polygon(((0, 0), (100, 0), (100, 100), (0, 100), (0, 0))))
    n2geom = MultiPolygon(
        Polygon(((0, 101), (101, 101), (101, 200), (0, 200), (0, 101))))

    n1 = Neighborhood(name="n1",
                      region_id=2,
                      city="c1",
                      state="PA",
                      county="PAC",
                      geometry=n1geom)
    n2 = Neighborhood(name="n2",
                      region_id=2,
                      city="c2",
                      state="NY",
                      county="NYC",
                      geometry=n2geom)

    n1.save()
    n2.save()

    z1geom = MultiPolygon(
        Polygon(((0, 0), (100, 0), (100, 100), (0, 100), (0, 0))))
    z2geom = MultiPolygon(
        Polygon(((0, 100), (100, 100), (100, 200), (0, 200), (0, 100))))

    z1 = ZipCode(zip="19107", geometry=z1geom)
    z2 = ZipCode(zip="10001", geometry=z2geom)

    z1.save()
    z2.save()

    exgeom1 = MultiPolygon(
        Polygon(((0, 0), (25, 0), (25, 25), (0, 25), (0, 0))))
    ex1 = ExclusionMask(geometry=exgeom1, type="building")

    ex1.save()

    agn1 = AggregateNeighborhood(annual_stormwater_management=0.0,
                                 annual_electricity_conserved=0.0,
                                 annual_energy_conserved=0.0,
                                 annual_natural_gas_conserved=0.0,
                                 annual_air_quality_improvement=0.0,
                                 annual_co2_sequestered=0.0,
                                 annual_co2_avoided=0.0,
                                 annual_co2_reduced=0.0,
                                 total_co2_stored=0.0,
                                 annual_ozone=0.0,
                                 annual_nox=0.0,
                                 annual_pm10=0.0,
                                 annual_sox=0.0,
                                 annual_voc=0.0,
                                 annual_bvoc=0.0,
                                 total_trees=0,
                                 total_plots=0,
                                 location=n1)

    agn2 = AggregateNeighborhood(annual_stormwater_management=0.0,
                                 annual_electricity_conserved=0.0,
                                 annual_energy_conserved=0.0,
                                 annual_natural_gas_conserved=0.0,
                                 annual_air_quality_improvement=0.0,
                                 annual_co2_sequestered=0.0,
                                 annual_co2_avoided=0.0,
                                 annual_co2_reduced=0.0,
                                 total_co2_stored=0.0,
                                 annual_ozone=0.0,
                                 annual_nox=0.0,
                                 annual_pm10=0.0,
                                 annual_sox=0.0,
                                 annual_voc=0.0,
                                 annual_bvoc=0.0,
                                 total_trees=0,
                                 total_plots=0,
                                 location=n2)

    agn1.save()
    agn2.save()

    s1 = Species(symbol="s1",
                 genus="testus1",
                 species="specieius1",
                 cultivar_name='',
                 family='',
                 alternate_symbol='a1')
    s2 = Species(symbol="s2",
                 genus="testus2",
                 species="specieius2",
                 cultivar_name='',
                 family='',
                 alternate_symbol='a2')
    s3 = Species(symbol="s3",
                 genus="testus2",
                 species="specieius3",
                 cultivar_name='',
                 family='',
                 alternate_symbol='a3')

    s1.native_status = 'True'
    s1.fall_conspicuous = True
    s1.flower_conspicuous = True
    s1.palatable_human = True

    s2.native_status = 'True'
    s2.fall_conspicuous = False
    s2.flower_conspicuous = True
    s2.palatable_human = False
    s2.wildlife_value = True

    s3.wildlife_value = True

    s1.save()
    s2.save()
    s3.save()

    s1.resource.add(rsrc1)
    s2.resource.add(rsrc2)
    s3.resource.add(rsrc2)

    ie = ImportEvent(file_name='site_add')
    ie.save()
예제 #25
0
class Command(BaseCommand):
    args = '<input_file_name, data_owner_id, base_srid, read_only>'
    option_list = BaseCommand.option_list + (
        make_option('--verbose',
            action='store_true',
            dest='verbose',
            default=False,
            help='Show verbose debug text'),
    )

    def write_headers_if_needed(self):
        if not self.wrote_headers:
            self.err_writer.writerow(self.headers)
            self.wrote_headers = True

    def get_dbf_rows(self, in_file):
        reader = dbf.Dbf(in_file)
        print 'Opening input file: %s ' % in_file

        self.headers = reader.fieldNames

        print 'Converting input file to dictionary'
        rows = []
        for dbf_row in reader:
            d = {}
            for name in self.headers:
                d[name] = dbf_row[name]
            rows.append(d)
        return rows

    def get_csv_rows(self, in_file):
        reader = csv.DictReader(open(in_file, 'r' ), restval=123)
        print 'Opening input file: %s ' % in_file

        rows = list(reader)

        self.headers = reader.fieldnames

        return rows

    def handle(self, *args, **options):
        self.wrote_headers = False
        try:
            self.file_name = args[0]
            in_file = self.file_name
            err_file = in_file + ".err"
            self.verbose = options.get('verbose')
            self.user_id = args[1]
            if len(args) > 2:
                self.base_srid = int(args[2])
                self.tf = CoordTransform(SpatialReference(self.base_srid), SpatialReference(4326))
                print "Using transformation object: %s" % self.tf
            else:
                self.base_srid = 4326
            if len(args) > 3:
                self.readonly = bool(args[3])
                print "Setting readonly flag to %s" % self.readonly
            else:
                self.readonly = False
        except:
            print "Arguments:  Input_File_Name.[dbf|csv] Data_Owner_User_Id (Optional: Base_SRID ReadOnly-bool)"
            print "Options:    --verbose"
            return

        self.err_writer = csv.writer(open(err_file, 'wb'))

        if self.file_name.endswith('.csv'):
            rows = self.get_csv_rows(in_file)
        if self.file_name.endswith('.dbf'):
            rows = self.get_dbf_rows(in_file)

        self.data_owner = User.objects.get(pk=self.user_id)
        self.updater = User.objects.get(pk=1)

        self.import_event = ImportEvent(file_name=self.file_name)
        self.import_event.save()

        print 'Importing %d records' % len(rows)
        for i, row in enumerate(rows):
            self.handle_row(row)

            j = i + 1
            if j % 50 == 0:
               print 'Loaded %d...' % j
            self.log_verbose("item %d" % i)

        print "Finished data load. "

        print "Calculating new species tree counts... "
        for s in Species.objects.all():
            s.save()
        print "Done."

    def log_verbose(self, msg):
        if self.verbose: print msg

    def log_error(self, msg, row):
        print "ERROR: %s" % msg
        columns = [row[s] for s in self.headers]
        self.write_headers_if_needed()
        self.err_writer.writerow(columns)

    def check_coords(self, row):
        try:
            x = float(row.get('POINT_X', 0))
            y = float(row.get('POINT_Y', 0))
        except:
            self.log_error("  Invalid coords, might not be numbers", row)
            return (False, 0, 0)

        ok = x and y
        if not ok:
            self.log_error("  Invalid coords", row)
        self.log_verbose("  Passed coordinate check")
        return (ok, x, y)

    def check_species(self, row):
        # locate the species and instanciate the tree instance
        if not row.get('SCIENTIFIC') and not row.get('GENUS'):
            self.log_verbose("  No species information")
            return (True, None)

        if row.get('SCIENTIFIC'):
            name = str(row['SCIENTIFIC']).strip()
            species_obj = Species.objects.filter(scientific_name__iexact=name)
            self.log_verbose("  Looking for species: %s" % name)
        else:
            genus = str(row['GENUS']).strip()
            species_field = ''
            cultivar = ''
            gender = ''
            name = genus
            if row.get('SPECIES'):
                species_field = str(row['SPECIES']).strip()
                name = name + " " + species_field
            if row.get('CULTIVAR'):
                cultivar = str(row['CULTIVAR']).strip()
                name = name + " " + cultivar
            if row.get('GENDER'):
                gender = str(row['GENDER']).strip()
                name = name + " " + gender
            species_obj = Species.objects.filter(genus__iexact=genus,species__iexact=species_field,cultivar_name__iexact=cultivar,gender__iexact=gender)
            self.log_verbose("  Looking for species: %s" % name)


        if species_obj: #species match found
            self.log_verbose("  Found species %r" % species_obj[0])
            return (True, species_obj)

        #species data but no match, check it manually
        self.log_error("ERROR:  Unknown species %r" % name, row)
        return (False, None)

    def check_tree_info(self, row):
        tree_info = False
        fields = ['STEWARD', 'SPONSOR', 'DATEPLANTED', 'DIAMETER', 'HEIGHT', 'CANOPYHEIGHT',
                  'CONDITION', 'CANOPYCONDITION', 'PROJECT_1', 'PROJECT_2', 'PROJECT_3', 'OWNER']

        for f in fields:
            # field exists and there's something in it
            if row.get(f) and str(row[f]).strip():
                tree_info = True
                self.log_verbose('  Found tree data in field %s, creating a tree' % f)
                break

        return tree_info

    def check_proximity(self, plot, tree, species, row):
        # check for nearby plots
        collisions = plot.validate_proximity(True, 0)

        # if there are no collisions, then proceed as planned
        if not collisions:
            self.log_verbose("  No collisions found")
            return (True, plot, tree)
        self.log_verbose("  Initial proximity test count: %d" % collisions.count())

        # exclude collisions from the same file we're working in
        collisions = collisions.exclude(import_event=self.import_event)
        if not collisions:
            self.log_verbose("  All collisions are from this import file")
            return (True, plot, tree)
        self.log_verbose("  Secondary proximity test count: %d" % collisions.count())

        # if we have multiple collitions, check for same species or unknown species
        # and try to associate with one of them otherwise abort
        if collisions.count() > 1:
            # get existing trees for the plots that we collided with
            tree_ids = []
            for c in collisions:
                if c.current_tree():
                    tree_ids.append(c.current_tree().id)

            trees = Tree.objects.filter(id__in=tree_ids)

            # Precedence: single same species, single unknown
            # return false for all others and log
            if species:
                same = trees.filter(species=species[0])
                if same.count() == 1 and same[0].species == species[0]:
                    self.log_verbose("  Using single nearby plot with tree of same species")
                    return (True, c, same[0])

            unk = trees.filter(species=None)

            if unk.count() == 1:
                self.log_verbose("  Using single nearby plot with tree of unknown species")
                return (True, c,  unk[0])

            self.log_error("ERROR:  Proximity test failed (near %d plots)" % collisions.count(), row)
            return (False, None, None)

        # one nearby match found, use it as base
        plot = collisions[0]
        plot_tree = plot.current_tree()
        self.log_verbose("  Found one tree nearby")

        # if the nearby plot doesn't have a tree, don't bother doing species matching
        if not plot_tree:
            self.log_verbose("  No tree found for plot, using %d as base plot record" % plot.id)
            return (True, plot, tree)

        # if neither have a species, then we're done and we need to use
        # the tree we collided with.
        if not plot_tree.species and not species:
            self.log_verbose("  No species info for either record, using %d as base tree record" % plot_tree.id)
            return (True, plot, plot_tree)

        # if only the new one has a species, update the tree we collided
        # with and then return it
        if not plot_tree.species:
            # we need to update the collision tree with the new species
            plot_tree.set_species(species[0].id, False) # save later
            self.log_verbose("  Species match, using update file species: %s" % species[0])
            return (True, plot, plot_tree)

        # if only the collision tree has a species, we're done.
        if not species or species.count() == 0:
            self.log_verbose("  No species info for import record, using %d as base record" % plot_tree.id)
            return (True, plot, plot_tree)

        # in this case, both had a species. we should check to see if
        # the species are the same.
        if plot_tree.species != species[0]:
            # now that we have a new species, we want to update the
            # collision tree's species and delete all the old status
            # information.
            self.log_verbose("  Species do not match, using update file species: %s" % species[0])
            plot_tree.set_species(species[0].id, False)
            plot_tree.dbh = None
            plot_tree.height = None
            plot_tree.canopy_height = None
            plot_tree.condition = None
            plot_tree.canopy_condition = None
        return (True, plot, plot_tree)

    def handle_row(self, row):
        self.log_verbose(row)

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

        plot = Plot()

        try:
            if self.base_srid != 4326:
                geom = Point(x, y, srid=self.base_srid)
                geom.transform(self.tf)
                self.log_verbose(geom)
                plot.geometry = geom
            else:
                plot.geometry = Point(x, y, srid=4326)
        except:
            self.log_error("ERROR: Geometry failed to transform", row)
            return

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

        # check for tree info, should we create a tree or just a plot
        if species or self.check_tree_info(row):
            tree = Tree(plot=plot)
        else:
            tree = None

        if tree and species:
            tree.species = species[0]


        # check the proximity (try to match up with existing trees)
        # this may return a different plot/tree than created just above,
        # so don't set anything else on either until after this point
        ok, plot, tree = self.check_proximity(plot, tree, species, row)
        if not ok: return


        if row.get('ADDRESS') and not plot.address_street:
            plot.address_street = str(row['ADDRESS']).title()
            plot.geocoded_address = str(row['ADDRESS']).title()

        if not plot.geocoded_address:
            plot.geocoded_address = ""

        # FIXME: get this from the config?
        plot.address_state = 'CA'
        plot.import_event = self.import_event
        plot.last_updated_by = self.updater
        plot.data_owner = self.data_owner
        plot.readonly = self.readonly

        if row.get('PLOTTYPE'):
            for k, v in choices['plot_types']:
                if v == row['PLOTTYPE']:
                    plot.type = k
                    break;

        if row.get('PLOTLENGTH'):
            plot.length = row['PLOTLENGTH']

        if row.get('PLOTWIDTH'):
            plot.width = row['PLOTWIDTH']

        if row.get('ID'):
            plot.owner_orig_id = row['ID']

        if row.get('ORIGID'):
            plot.owner_additional_properties = "ORIGID=" + str(row['ORIGID'])

        if row.get('OWNER_ADDITIONAL_PROPERTIES'):
            plot.owner_additional_properties = str(plot.owner_additional_properties) + " " + str(row['OWNER_ADDITIONAL_PROPERTIES'])

        if row.get('OWNER_ADDITIONAL_ID'):
            plot.owner_additional_id = str(row['OWNER_ADDITIONAL_ID'])

        if row.get('POWERLINE'):
            for k, v in choices['powerlines']:
                if v == row['POWERLINE']:
                    plot.powerline_conflict_potential = k
                    break;

        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':
            plot.sidewalk_damage = 2
        else:
            plot.sidewalk_damage = 1

        plot.quick_save()

        pnt = plot.geometry
        n = Neighborhood.objects.filter(geometry__contains=pnt)
        z = ZipCode.objects.filter(geometry__contains=pnt)

        plot.neighborhoods = ""
        plot.neighborhood.clear()
        plot.zipcode = None

        if n:
            for nhood in n:
                if nhood:
                    plot.neighborhoods = plot.neighborhoods + " " + nhood.id.__str__()
                    plot.neighborhood.add(nhood)

        if z: plot.zipcode = z[0]

        plot.quick_save()

        if tree:
            tree.plot = plot
            tree.readonly = self.readonly
            tree.import_event = self.import_event
            tree.last_updated_by = self.updater

            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_string = str(row['DATEPLANTED'])
                try:
                    date = datetime.strptime(date_string, "%m/%d/%Y")
                except:
                    pass
                try:
                    date = datetime.strptime(date_string, "%Y/%m/%d")
                except:
                    pass
                if not date:
                    raise ValueError("Date strings must be in mm/dd/yyyy or yyyy/mm/dd format")

                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['conditions']:
                    if v == row['CONDITION']:
                        tree.condition = k
                        break;

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

            tree.quick_save()

            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()
예제 #26
0
class Command(BaseCommand):
    args = "<input_file_name, data_owner_id, base_srid, read_only>"
    option_list = BaseCommand.option_list + (
        make_option("--verbose", action="store_true", dest="verbose", default=False, help="Show verbose debug text"),
    )

    def write_headers_if_needed(self):
        if not self.wrote_headers:
            self.err_writer.writerow(self.headers)
            self.wrote_headers = True

    def get_dbf_rows(self, in_file):
        reader = dbf.Dbf(in_file)
        print "Opening input file: %s " % in_file

        self.headers = reader.fieldNames

        print "Converting input file to dictionary"
        rows = []
        for dbf_row in reader:
            d = {}
            for name in self.headers:
                d[name] = dbf_row[name]
            rows.append(d)
        return rows

    def get_csv_rows(self, in_file):
        reader = csv.DictReader(open(in_file, "r"), restval=123)
        print "Opening input file: %s " % in_file

        rows = list(reader)

        self.headers = reader.fieldnames

        return rows

    def handle(self, *args, **options):
        self.wrote_headers = False
        try:
            self.file_name = args[0]
            in_file = self.file_name
            err_file = in_file + ".err"
            self.verbose = options.get("verbose")
            self.user_id = args[1]
            if len(args) > 2:
                self.base_srid = int(args[2])
                self.tf = CoordTransform(SpatialReference(self.base_srid), SpatialReference(4326))
                print "Using transformaton object: %s" % self.tf
            else:
                self.base_srid = 4326
            if len(args) > 3:
                self.readonly = bool(args[3])
                print "Setting readonly flag to %s" % self.readonly
            else:
                self.readonly = False
        except:
            print "Arguments:  Input_File_Name.[dbf|csv], Data_Owner_User_Id, (Base_SRID optional)"
            print "Options:    --verbose"
            return

        self.err_writer = csv.writer(open(err_file, "wb"))

        if self.file_name.endswith(".csv"):
            rows = self.get_csv_rows(in_file)
        if self.file_name.endswith(".dbf"):
            rows = self.get_dbf_rows(in_file)

        self.data_owner = User.objects.get(pk=self.user_id)
        self.updater = User.objects.get(pk=1)

        self.import_event = ImportEvent(file_name=self.file_name)
        self.import_event.save()

        print "Importing %d records" % len(rows)
        for i, row in enumerate(rows):
            self.handle_row(row)

            j = i + 1
            if j % 50 == 0:
                print "Loaded %d..." % j
            self.log_verbose("item %d" % i)

        print "Finished data load. "

        print "Calculating new species tree counts... "
        for s in Species.objects.all():
            s.save()
        print "Done."

    def log_verbose(self, msg):
        if self.verbose:
            print msg

    def log_error(self, msg, row):
        print "ERROR: %s" % msg
        columns = [row[s] for s in self.headers]
        self.write_headers_if_needed()
        self.err_writer.writerow(columns)

    def check_coords(self, row):
        try:
            x = float(row.get("POINT_X", 0))
            y = float(row.get("POINT_Y", 0))
        except:
            self.log_error("  Invalid coords, might not be numbers", row)
            return (False, 0, 0)

        ok = x and y
        if not ok:
            self.log_error("  Invalid coords", row)
        self.log_verbose("  Passed coordinate check")
        return (ok, x, y)

    def check_species(self, row):
        # locate the species and instanciate the tree instance
        if not row.get("SCIENTIFIC") and not row.get("GENUS"):
            self.log_verbose("  No species information")
            return (True, None)

        if row.get("SCIENTIFIC"):
            name = str(row["SCIENTIFIC"]).strip()
            species = Species.objects.filter(scientific_name__iexact=name)
            self.log_verbose("  Looking for species: %s" % name)
        else:
            genus = str(row["GENUS"]).strip()
            species = ""
            cultivar = ""
            gender = ""
            name = genus
            if row.get("SPECIES"):
                species = str(row["SPECIES"]).strip()
                name = name + " " + species
            if row.get("CULTIVAR"):
                cultivar = str(row["CULTIVAR"]).strip()
                name = name + " " + cultivar
            if row.get("GENDER"):
                gender = str(row["GENDER"]).strip()
                name = name + " " + gender
            species = Species.objects.filter(scientific_name__iexact=name)
            self.log_verbose("  Looking for species: %s %s %s %s" % (genus, species, cultivar, gender))

        if species:  # species match found
            self.log_verbose("  Found species %r" % species[0])
            return (True, species)

        # species data but no match, check it manually
        self.log_error("ERROR:  Unknown species %r" % name, row)
        return (False, None)

    def check_tree_info(self, row):
        tree_info = False
        fields = [
            "STEWARD",
            "SPONSOR",
            "DATEPLANTED",
            "DIAMETER",
            "HEIGHT",
            "CANOPYHEIGHT",
            "CONDITION",
            "CANOPYCONDITION",
            "PROJECT_1",
            "PROJECT_2",
            "PROJECT_3",
            "OWNER",
        ]

        for f in fields:
            # field exists and there's something in it
            if row.get(f) and str(row[f]).strip():
                tree_info = True
                self.log_verbose("  Found tree data in field %s, creating a tree" % f)
                break

        return tree_info

    def check_proximity(self, plot, tree, species, row):
        # check for nearby plots
        collisions = plot.validate_proximity(True, 0)

        # if there are no collisions, then proceed as planned
        if not collisions:
            self.log_verbose("  No collisions found")
            return (True, plot, tree)
        self.log_verbose("  Initial proximity test count: %d" % collisions.count())

        # exclude collisions from the same file we're working in
        collisions = collisions.exclude(import_event=self.import_event)
        if collisions:
            self.log_verbose("  All collisions are from this import file")
            return (True, plot, tree)
        self.log_verbose("  Secondary proximity test count: %d" % collisions.count())

        # if we have multiple collitions, check for same species or unknown species
        # and try to associate with one of them otherwise abort
        if collisions.count() > 1:
            # get existing trees for the plots that we collided with
            tree_ids = []
            for c in collisions:
                if c.current_tree():
                    tree_ids.append(c.current_tree().id)

            trees = Tree.objects.filter(id__in=tree_ids)

            # Precedence: single same species, single unknown
            # return false for all others and log
            if species:
                same = trees.filter(species=species[0])
                if same.count() == 1 and same[0].species == species[0]:
                    self.log_verbose("  Using single nearby plot with tree of same species")
                    return (True, c, same[0])

            unk = trees.filter(species=None)

            if unk.count() == 1:
                self.log_verbose("  Using single nearby plot with tree of unknown species")
                return (True, c, unk[0])

            self.log_error("ERROR:  Proximity test failed (near %d plots)" % collisions.count(), row)
            return (False, None, None)

        # one nearby match found, use it as base
        plot = collisions[0]
        plot_tree = plot.current_tree()
        self.log_verbose("  Found one tree nearby")

        # if the nearby plot doesn't have a tree, don't bother doing species matching
        if not plot_tree:
            self.log_verbose("  No tree found for plot, using %d as base plot record" % plot.id)
            return (True, plot, tree)

        # if neither have a species, then we're done and we need to use
        # the tree we collided with.
        if not plot_tree.species and not species:
            self.log_verbose("  No species info for either record, using %d as base tree record" % plot_tree.id)
            return (True, plot, plot_tree)

        # if only the new one has a species, update the tree we collided
        # with and then return it
        if not plot_tree.species:
            # we need to update the collision tree with the new species
            plot_tree.set_species(species[0].id, False)  # save later
            self.log_verbose("  Species match, using update file species: %s" % species[0])
            return (True, plot, plot_tree)

        # if only the collision tree has a species, we're done.
        if not species or species.count() == 0:
            self.log_verbose("  No species info for import record, using %d as base record" % plot_tree.id)
            return (True, plot, plot_tree)

        # in this case, both had a species. we should check to see if
        # the species are the same.
        if plot_tree.species != species[0]:
            # now that we have a new species, we want to update the
            # collision tree's species and delete all the old status
            # information.
            self.log_verbose("  Species do not match, using update file species: %s" % species[0])
            plot_tree.set_species(species[0].id, False)
            plot_tree.dbh = None
            plot_tree.height = None
            plot_tree.canopy_height = None
            plot_tree.condition = None
            plot_tree.canopy_condition = None
        return (True, plot, plot_tree)

    def handle_row(self, row):
        self.log_verbose(row)

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

        plot = Plot()

        try:
            if self.base_srid != 4326:
                geom = Point(x, y, srid=self.base_srid)
                geom.transform(self.tf)
                self.log_verbose(geom)
                plot.geometry = geom
            else:
                plot.geometry = Point(x, y, srid=4326)
        except:
            self.log_error("ERROR: Geometry failed to transform", row)
            return

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

        # check for tree info, should we create a tree or just a plot
        if species or self.check_tree_info(row):
            tree = Tree(plot=plot)
        else:
            tree = None

        if tree and species:
            tree.species = species[0]

        # check the proximity (try to match up with existing trees)
        # this may return a different plot/tree than created just above,
        # so don't set anything else on either until after this point
        ok, plot, tree = self.check_proximity(plot, tree, species, row)
        if not ok:
            return

        if row.get("ADDRESS") and not plot.address_street:
            plot.address_street = str(row["ADDRESS"]).title()
            plot.geocoded_address = str(row["ADDRESS"]).title()

        if not plot.geocoded_address:
            plot.geocoded_address = ""

        # FIXME: get this from the config?
        plot.address_state = "CA"
        plot.import_event = self.import_event
        plot.last_updated_by = self.updater
        plot.data_owner = self.data_owner
        plot.readonly = self.readonly

        if row.get("PLOTTYPE"):
            for k, v in choices["plot_types"]:
                if v == row["PLOTTYPE"]:
                    plot.type = k
                    break

        if row.get("PLOTLENGTH"):
            plot.length = row["PLOTLENGTH"]

        if row.get("PLOTWIDTH"):
            plot.width = row["PLOTWIDTH"]

        if row.get("ID"):
            plot.owner_orig_id = row["ID"]

        if row.get("ORIGID"):
            plot.owner_additional_properties = "ORIGID=" + str(row["ORIGID"])

        if row.get("OWNER_ADDITIONAL_PROPERTIES"):
            plot.owner_additional_properties = (
                str(plot.owner_additional_properties) + " " + str(row["OWNER_ADDITIONAL_PROPERTIES"])
            )

        if row.get("OWNER_ADDITIONAL_ID"):
            plot.owner_additional_id = str(row["OWNER_ADDITIONAL_ID"])

        if row.get("POWERLINE"):
            for k, v in choices["powerlines"]:
                if v == row["POWERLINE"]:
                    plot.powerline = k
                    break

        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":
            plot.sidewalk_damage = 2
        else:
            plot.sidewalk_damage = 1

        plot.quick_save()

        pnt = plot.geometry
        n = Neighborhood.objects.filter(geometry__contains=pnt)
        z = ZipCode.objects.filter(geometry__contains=pnt)

        plot.neighborhoods = ""
        plot.neighborhood.clear()
        plot.zipcode = None

        if n:
            for nhood in n:
                if nhood:
                    plot.neighborhoods = plot.neighborhoods + " " + nhood.id.__str__()
                    plot.neighborhood.add(nhood)

        if z:
            plot.zipcode = z[0]

        plot.quick_save()

        if tree:
            tree.plot = plot
            tree.readonly = self.readonly
            tree.import_event = self.import_event
            tree.last_updated_by = self.updater

            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_string = str(row["DATEPLANTED"])
                try:
                    date = datetime.strptime(date_string, "%m/%d/%Y")
                except:
                    pass
                try:
                    date = datetime.strptime(date_string, "%Y/%m/%d")
                except:
                    pass
                if not date:
                    raise ValueError("Date strings must be in mm/dd/yyyy or yyyy/mm/dd format")

                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["conditions"]:
                    if v == row["CONDITION"]:
                        tree.condition = k
                        break

            if row.get("CANOPYCONDITION"):
                for k, v in choices["canopy_conditions"]:
                    if v == row["CANOPYCONDITION"]:
                        tree.canopy_condition = k
                        break
            # FOR OTM INDIA
            # GIRTH_CM", "GIRTH_M", "HEIGHT_FT", "HEIGHT_M", "NEST", "BURROWS", "FLOWERS", "FRUITS", "NAILS", "POSTER", "WIRES", "TREE_GUARD", "NUISANCE", "NUISANCE_DESC", "HEALTH_OF_TREE", "FOUND_ON_GROUND", "GROUND_DESCRIPTION", "RISK_ON_TREE", "RISK_DESC", "RARE", "ENDANGERED", "VULNERABLE", "PEST_AFFECTED", "REFER_TO_DEPT", "SPECIAL_OTHER", "SPECIAL_OTHER_DESCRIPTION", "LATITUDE", "LONGITUDE", "CREATION_DATE", "DEVICE_ID", "TIME", "DATE"])

            if row.get("GIRTH_M"):
                tree.girth_m = float(row["GIRTH_M"])

            if row.get("HEIGHT_M"):
                tree.height_m = float(row["HEIGHT_M"])

            if row.get("NEST"):
                tree.nest = str(row["NEST"])

            if row.get("BURROWS"):
                tree.burrows = str(row["BURROWS"])

            if row.get("FLOWERS"):
                tree.flowers = str(row["FLOWERS"])

            if row.get("FRUITS"):
                tree.fruits = str(row["FRUITS"])

            if row.get("NAILS"):
                tree.nails = str(row["NAILS"])

            if row.get("POSTER"):
                tree.poster = str(row["POSTER"])

            if row.get("WIRES"):
                tree.wires = str(row["WIRES"])

            if row.get("TREE_GUARD"):
                tree.tree_guard = str(row["TREE_GUARD"])

            if row.get("NUISANCE"):
                tree.other_nuisance = bool(row["NUISANCE"])

            if row.get("NUISANCE_DESC"):
                tree.other_nuisance_desc = str(row["NUISANCE_DESC"])

            if row.get("HEALTH_OF_TREE"):
                tree.health_of_tree = str(row["HEALTH_OF_TREE"])

            if row.get("FOUND_ON_GROUND"):
                tree.found_on_ground = str(row["FOUND_ON_GROUND"])

            if row.get("GROUND_DESCRIPTION"):
                tree.ground_description = str(row["GROUND_DESCRIPTION"])

            if row.get("RISK_ON_TREE"):
                tree.risk_on_tree = str(row["RISK_ON_TREE"])

            if row.get("RISK_DESC"):
                tree.risk_desc = str(row["RISK_DESC"])

            if row.get("PEST_AFFECTED"):
                tree.pests = str(row["PEST_AFFECTED"])

            if row.get("REFER_TO_DEPT"):
                tree.refer_to_dept = str(row["REFER_TO_DEPT"])

            if row.get("SPECIAL_OTHER"):
                tree.special_other = str(row["SPECIAL_OTHER"])

            if row.get("SPECIAL_OTHER_DESCRIPTION"):
                tree.special_other_description = str(row["SPECIAL_OTHER_DESCRIPTION"])

            if row.get("LATITUDE"):
                tree.latitude = str(row["LATITUDE"])

            if row.get("LONGITUDE"):
                tree.longitude = str(row["LONGITUDE"])

            if row.get("PRABHAG_ID"):
                tree.prabhag_id = str(row["PRABHAG_ID"])

            if row.get("CLUSTER_ID"):
                tree.cluster_id = str(row["CLUSTER_ID"])

            # if row.get('ID'):
            #    tree.id = str(row['ID'])

            # import pdb; pdb.set_trace()

            f = open("trees_in_otm_obj.log", "w")
            f.write("b4 save")
            f.write(str(tree.__dict__))
            tree.quick_save()
            f.write("after save \n")
            f.write(str(tree.__dict__))
            f.close()

            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()
예제 #27
0
def setupTreemapEnv():
    settings.GEOSERVER_GEO_LAYER = ""
    settings.GEOSERVER_GEO_STYLE = ""
    settings.GEOSERVER_URL = ""

    def local_render_to_response(*args, **kwargs):
        from django.template import loader, RequestContext
        from django.http import HttpResponse

        httpresponse_kwargs = {"mimetype": kwargs.pop("mimetype", None)}
        hr = HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)

        if hasattr(args[1], "dicts"):
            hr.request_context = args[1].dicts

        return hr

    django.shortcuts.render_to_response = local_render_to_response

    r1 = ReputationAction(name="edit verified", description="blah")
    r2 = ReputationAction(name="edit tree", description="blah")
    r3 = ReputationAction(name="Administrative Action", description="blah")
    r4 = ReputationAction(name="add tree", description="blah")
    r5 = ReputationAction(name="edit plot", description="blah")
    r6 = ReputationAction(name="add plot", description="blah")
    r7 = ReputationAction(name="add stewardship", description="blah")
    r8 = ReputationAction(name="remove stewardship", description="blah")

    for r in [r1, r2, r3, r4, r5, r6, r7, r8]:
        r.save()

    bv = BenefitValues(
        co2=0.02,
        pm10=9.41,
        area="InlandValleys",
        electricity=0.1166,
        voc=4.69,
        ozone=5.0032,
        natural_gas=1.25278,
        nox=12.79,
        stormwater=0.0078,
        sox=3.72,
        bvoc=4.96,
    )

    bv.save()

    dbh = "[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]"
    dbh2 = "[2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]"

    rsrc1 = Resource(meta_species="BDM OTHER", region="NoEastXXX")
    rsrc2 = Resource(meta_species="BDL OTHER", region="NoEastXXX")

    rsrc1.save()
    rsrc2.save()

    u = User.objects.filter(username="******")

    if u:
        u = u[0]
    else:
        u = User.objects.create_user("jim", "*****@*****.**", "jim")
        u.is_staff = True
        u.is_superuser = True
        u.save()
        up = UserProfile(user=u)
        up.save()
        u.reputation = Reputation(user=u)
        u.reputation.save()

    amy_filter_result = User.objects.filter(username="******")
    if not amy_filter_result:
        amy = User.objects.create_user("amy", "*****@*****.**", "amy")
    else:
        amy = amy_filter_result[0]
        amy.is_staff = False
        amy.is_superuser = False
        amy.save()
        amy_profile = UserProfile(user=amy)
        amy_profile.save()
        amy.reputation = Reputation(user=amy)
        amy.reputation.save()

    olivia_filter_result = User.objects.filter(username="******")
    if not amy_filter_result:
        olivia = User.objects.create_user("olivia", "*****@*****.**", "olivia")
    else:
        olivia = olivia_filter_result[0]
        olivia.is_staff = False
        olivia.is_superuser = False
        olivia.save()
        olivia_profile = UserProfile(user=olivia)
        olivia_profile.save()
        olivia.reputation = Reputation(user=olivia)
        olivia.reputation.save()

    n1geom = MultiPolygon(Polygon(((0, 0), (100, 0), (100, 100), (0, 100), (0, 0))))
    n2geom = MultiPolygon(Polygon(((0, 101), (101, 101), (101, 200), (0, 200), (0, 101))))

    n1 = Neighborhood(name="n1", region_id=2, city="c1", state="PA", county="PAC", geometry=n1geom)
    n2 = Neighborhood(name="n2", region_id=2, city="c2", state="NY", county="NYC", geometry=n2geom)

    n1.save()
    n2.save()

    z1geom = MultiPolygon(Polygon(((0, 0), (100, 0), (100, 100), (0, 100), (0, 0))))
    z2geom = MultiPolygon(Polygon(((0, 100), (100, 100), (100, 200), (0, 200), (0, 100))))

    z1 = ZipCode(zip="19107", geometry=z1geom)
    z2 = ZipCode(zip="10001", geometry=z2geom)

    z1.save()
    z2.save()

    exgeom1 = MultiPolygon(Polygon(((0, 0), (25, 0), (25, 25), (0, 25), (0, 0))))
    ex1 = ExclusionMask(geometry=exgeom1, type="building")

    ex1.save()

    agn1 = AggregateNeighborhood(
        annual_stormwater_management=0.0,
        annual_electricity_conserved=0.0,
        annual_energy_conserved=0.0,
        annual_natural_gas_conserved=0.0,
        annual_air_quality_improvement=0.0,
        annual_co2_sequestered=0.0,
        annual_co2_avoided=0.0,
        annual_co2_reduced=0.0,
        total_co2_stored=0.0,
        annual_ozone=0.0,
        annual_nox=0.0,
        annual_pm10=0.0,
        annual_sox=0.0,
        annual_voc=0.0,
        annual_bvoc=0.0,
        total_trees=0,
        total_plots=0,
        location=n1,
    )

    agn2 = AggregateNeighborhood(
        annual_stormwater_management=0.0,
        annual_electricity_conserved=0.0,
        annual_energy_conserved=0.0,
        annual_natural_gas_conserved=0.0,
        annual_air_quality_improvement=0.0,
        annual_co2_sequestered=0.0,
        annual_co2_avoided=0.0,
        annual_co2_reduced=0.0,
        total_co2_stored=0.0,
        annual_ozone=0.0,
        annual_nox=0.0,
        annual_pm10=0.0,
        annual_sox=0.0,
        annual_voc=0.0,
        annual_bvoc=0.0,
        total_trees=0,
        total_plots=0,
        location=n2,
    )

    agn1.save()
    agn2.save()

    s1 = Species(symbol="s1", genus="testus1", species="specieius1", cultivar_name="", family="", alternate_symbol="a1")
    s2 = Species(symbol="s2", genus="testus2", species="specieius2", cultivar_name="", family="", alternate_symbol="a2")
    s3 = Species(symbol="s3", genus="testus2", species="specieius3", cultivar_name="", family="", alternate_symbol="a3")

    s1.native_status = "True"
    s1.fall_conspicuous = True
    s1.flower_conspicuous = True
    s1.palatable_human = True

    s2.native_status = "True"
    s2.fall_conspicuous = False
    s2.flower_conspicuous = True
    s2.palatable_human = False
    s2.wildlife_value = True

    s3.wildlife_value = True

    s1.save()
    s2.save()
    s3.save()

    s1.resource.add(rsrc1)
    s2.resource.add(rsrc2)
    s3.resource.add(rsrc2)

    ie = ImportEvent(file_name="site_add")
    ie.save()