Example #1
0
 def _create_strata(self):
     stand_list = {
         'property': self.prop1.uid,
         'classes': [
             ('Douglas-fir', 2, 4, 145),
         ]
     }
     strata = Strata(user=self.user, name="My Strata", search_age=30.0, search_tpa=120.0, stand_list = stand_list)
     strata.save()
     return strata
Example #2
0
 def _create_strata(self):
     stand_list = {
         'property': self.prop1.uid,
         'classes': [
             ('Douglas-fir', 2, 4, 145),
         ]
     }
     strata = Strata(user=self.user, name="My Strata", search_age=30.0, search_tpa=120.0, stand_list = stand_list)
     strata.save()
     return strata
Example #3
0
    def test_bad_stand_list(self):
        stand_list = [ ('Douglas-fir', 2, 4, 145), ]
        strata = Strata(user=self.user, name="My Strata", search_age=30.0, search_tpa=120.0, stand_list=stand_list)
        with self.assertRaises(ValidationError):
            strata.save()

        stand_list = {
            'property': self.prop1.uid,
            'classes': [
                ('Douglas-fir', "booo"),
            ]
        }
        strata = Strata(user=self.user, name="My Strata", search_age=30.0, search_tpa=120.0, stand_list=stand_list)
        with self.assertRaises(ValidationError):
            strata.save()
Example #4
0
    def test_bad_stand_list(self):
        stand_list = [("Douglas-fir", 2, 4, 145)]
        strata = Strata(user=self.user, name="My Strata", search_age=30.0, search_tpa=120.0, stand_list=stand_list)
        with self.assertRaises(ValidationError):
            strata.save()

        stand_list = {"property": self.prop1.uid, "classes": [("Douglas-fir", "booo")]}
        strata = Strata(user=self.user, name="My Strata", search_age=30.0, search_tpa=120.0, stand_list=stand_list)
        with self.assertRaises(ValidationError):
            strata.save()
Example #5
0
    def test_bad_stand_list(self):
        stand_list = [ ('Douglas-fir', 2, 4, 145), ] 
        strata = Strata(user=self.user, name="My Strata", search_age=30.0, search_tpa=120.0, stand_list=stand_list)
        with self.assertRaises(ValidationError):
            strata.save()

        stand_list = {
            'property': self.prop1.uid,
            'classes': [
                ('Douglas-fir', "booo"),
            ]
        }
        strata = Strata(user=self.user, name="My Strata", search_age=30.0, search_tpa=120.0, stand_list=stand_list)
        with self.assertRaises(ValidationError):
            strata.save()
Example #6
0
 def _create_strata(self):
     stand_list = {"property": self.prop1.uid, "classes": [("Douglas-fir", 2, 4, 145)]}
     strata = Strata(user=self.user, name="My Strata", search_age=30.0, search_tpa=120.0, stand_list=stand_list)
     strata.save()
     return strata
Example #7
0
    def import_ogr(self, shp_path, field_mapping=None, layer_num=0,
                   forest_property=None, new_property_name=None, pre_impute=False):
        ds = DataSource(shp_path)
        layer = ds[0]
        num_features = len(layer)
        field_mapping = self._validate_field_mapping(layer, field_mapping)

        if not forest_property and not new_property_name:
            raise Exception(
                "Must provide either existing forest_property OR new_property_name")

        if new_property_name:
            # Calculating property outline from stands
            stands = []
            for feature in layer:
                stands.append(wkt.loads(feature.geom.wkt))
            casc_poly = cascaded_union(stands)

            if casc_poly.type == 'MultiPolygon':
                polys = []
                for c in casc_poly:
                    # Identify small 'slivers' or areas of empty space b/t polygons that are unintentional
                    # If they're smaller than the threshold, remove them
                    interiors = [x for x in c.interiors if Polygon(
                        x).area > settings.SLIVER_THRESHOLD]
                    polys.append(Polygon(shell=c.exterior, holes=interiors))
            elif casc_poly.type == 'Polygon':
                # Identify small 'slivers' or areas of empty space b/t polygons that are unintentional
                # If they're smaller than the threshold, remove them
                interiors = [x for x in casc_poly.interiors if Polygon(
                    x).area > settings.SLIVER_THRESHOLD]
                polys = [Polygon(shell=casc_poly.exterior, holes=interiors)]

            casc = MultiPolygon(polys)

            # Creating Property
            self.forest_property = ForestProperty.objects.create(
                user=self.user, name=new_property_name, geometry_final=casc.wkt)
        else:
            self.forest_property = forest_property

        try:
            # special case for user-inventory situation
            # if there is a condid field, it is implicitly required.
            use_condid = False
            if 'condid' in layer.fields:
                use_condid = True
                variant = self.forest_property.variant
                valid_condids = FVSAggregate.valid_condids(variant)

            stands = []
            stratum = {}
            for feature in layer:
                stand = Stand(
                    user=self.user,
                    name=str(datetime_to_unix(datetime.datetime.now())),
                    geometry_orig=feature.geom.geos)

                for fname in self.optional_fields:
                    if fname in field_mapping.keys():
                        try:
                            stand.__dict__[fname] = feature.get(
                                field_mapping[fname])
                        except OGRIndexError:
                            pass

                # If user inventory case, check each feature which must contain integer condids
                # that refer to valid fvsaggregate records for that variant
                if use_condid:
                    condid = feature.get('condid')

                    if condid not in valid_condids:
                        raise Exception('Condition id {} is not valid for the {} variant (check fvsaggregate table)'.format(condid, variant))

                    if condid in stratum.keys():
                        # use cached
                        strata = stratum[condid]
                    else:
                        # create it
                        kwargs = condid_strata(condid, self.forest_property.uid)
                        strata = Strata(user=self.user, name=condid, **kwargs)
                        strata.save(skip_validation=True)  # no need for NN validation
                        self.forest_property.add(strata)
                        stratum[condid] = strata

                    stand.cond_id = condid
                    stand.locked_cond_id = condid
                    stand.strata = strata

                stand.full_clean()
                stands.append(stand)
                del stand

            for stand in stands:
                stand.save()
                self.forest_property.add(stand)
                if pre_impute:
                    # Technically locked stands won't need terrain variables
                    # ... but terrain info is nice to have anyways for all stands.
                    # Note the work is done asynchronously to ensure fast uploads
                    stand.preimpute()
        except:
            # Any failures? Rollback and re-raise...
            for stand in stands:
                stand.delete()
            if new_property_name:
                self.forest_property.delete()
            raise

        return True