Пример #1
0
    def setUp(self):
        self.user = User.objects.create_user("featuretest", "*****@*****.**", password="******")
        self.prop1 = ForestProperty(user=self.user, name="My Property", geometry_final=p1)
        self.prop1.save()

        d = os.path.dirname(__file__)
        self.shp_path = os.path.abspath(os.path.join(d, "..", "fixtures", "testdata", "test_stands.shp"))
        s = StandImporter(self.user)
        s.import_ogr(self.shp_path, forest_property=self.prop1)
Пример #2
0
    def test_importer_preimpute(self):
        self.assertEqual(len(Stand.objects.all()), 0)

        self._populate_fvsaggregate()

        s = StandImporter(self.user)
        s.import_ogr(self.condid_shp_path, new_property_name="Locked Property", pre_impute=True)

        new_property = ForestProperty.objects.get(name="Locked Property")
        self.assertEqual(len(new_property.feature_set(feature_classes=[Stand])), 37)
Пример #3
0
    def test_importer_preimpute(self):
        self.assertEqual(len(Stand.objects.all()), 0)

        self._populate_fvsaggregate()

        s = StandImporter(self.user)
        s.import_ogr(self.condid_shp_path, new_property_name="Locked Property", pre_impute=True)

        new_property = ForestProperty.objects.get(name="Locked Property")
        self.assertEqual(len(new_property.feature_set(feature_classes=[Stand])), 37)
Пример #4
0
    def setUp(self):
        self.user = User.objects.create_user(
            'featuretest', '*****@*****.**', password='******')
        self.prop1 = ForestProperty(user=self.user, name="My Property", geometry_final=p1)
        self.prop1.save()

        d = os.path.dirname(__file__)
        self.shp_path = os.path.abspath(os.path.join(d, '..', 'fixtures', 
            'testdata', 'test_stands.shp'))
        s = StandImporter(self.user)
        s.import_ogr(self.shp_path, forest_property=self.prop1) 
Пример #5
0
    def setUp(self):
        self.user = User.objects.create_user(
            'featuretest', '*****@*****.**', password='******')
        self.prop1 = ForestProperty(user=self.user, name="My Property", geometry_final=p1)
        self.prop1.save()

        d = os.path.dirname(__file__)
        self.shp_path = os.path.abspath(os.path.join(d, '..', 'fixtures',
            'testdata', 'test_stands.shp'))
        s = StandImporter(self.user)
        s.import_ogr(self.shp_path, forest_property=self.prop1)
Пример #6
0
    def test_importer_py(self):
        self.assertEqual(len(Stand.objects.all()), 0)
        self.assertEqual(len(self.prop1.feature_set()), 0)

        s = StandImporter(self.user)
        s.import_ogr(self.shp_path, forest_property=self.prop1)

        self.assertEqual(len(Stand.objects.all()), 37)
        # from the default 'name' field this time
        self.assertEqual(len(Stand.objects.filter(name="001A")), 0)
        self.assertEqual(len(Stand.objects.filter(name="277")), 1)
        self.assertEqual(len(self.prop1.feature_set()), 37)
Пример #7
0
    def test_importer_py(self):
        self.assertEqual(len(Stand.objects.all()), 0)
        self.assertEqual(len(self.prop1.feature_set()), 0)

        s = StandImporter(self.user)
        s.import_ogr(self.shp_path, forest_property=self.prop1)

        self.assertEqual(len(Stand.objects.all()), 37)
        # from the default 'name' field this time
        self.assertEqual(len(Stand.objects.filter(name='001A')), 0)
        self.assertEqual(len(Stand.objects.filter(name='277')), 1)
        self.assertEqual(len(self.prop1.feature_set()), 37)
Пример #8
0
    def handle(self, *args, **options):

        try:
            shp_path = args[0]
            assert os.path.exists(shp_path)
        except (AssertionError, IndexError):
            raise CommandError(
                "Specify shapefile and name field\ne.g. python manage.py import_demo_stands my_stands.shp NAMEFIELD \"My Property\""
            )

        ds = DataSource(shp_path)
        layer = ds[0]

        if layer.srs.srid != settings.GEOMETRY_DB_SRID:
            print "WARNING: Shapefile must be in Web Mercator. If it's not, the results will not be good."

        try:
            namefld = args[1]
        except (IndexError):
            raise CommandError(
                "Specify shapefile and name field\ne.g. python manage.py import_demo_stands my_stands.shp NAMEFIELD \"My Property\" \nChoice are %s"
                % (', '.join(layer.fields), ))

        if namefld not in layer.fields:
            raise Exception(
                namefld +
                " is not a valid field name for this dataset \nChoices are: \n %s"
                % (', '.join(layer.fields), ))

        try:
            property_name = args[2]
        except IndexError:
            property_name = layer.name

        try:
            user = User.objects.get(username='******')
        except User.DoesNotExist:
            user = User.objects.create_user('demo',
                                            '*****@*****.**',
                                            password='******')

        print "Importing stands..."
        field_mapping = {'name': namefld}
        s = StandImporter(user)
        s.import_ogr(shp_path,
                     field_mapping,
                     new_property_name=property_name,
                     pre_impute=True)

        prop = ForestProperty.objects.filter(
            name=property_name).latest('date_modified')
        print "%d stands imported from %s" % (len(
            prop.feature_set()), shp_path)
Пример #9
0
    def test_importer_py_fieldmap(self):
        self.assertEqual(len(Stand.objects.all()), 0)
        self.assertEqual(len(self.prop1.feature_set()), 0)

        s = StandImporter(self.user)
        field_mapping = {"name": "STAND_TEXT"}
        s.import_ogr(self.shp_path, field_mapping, forest_property=self.prop1)

        self.assertEqual(len(Stand.objects.all()), 37)
        # from the 'STAND_TEXT' field this time
        self.assertEqual(len(Stand.objects.filter(name="001A")), 1)
        self.assertEqual(len(Stand.objects.filter(name="277")), 0)
        self.assertEqual(len(self.prop1.feature_set()), 37)
Пример #10
0
    def test_importer_py_fieldmap(self):
        self.assertEqual(len(Stand.objects.all()), 0)
        self.assertEqual(len(self.prop1.feature_set()), 0)

        s = StandImporter(self.user)
        field_mapping = {'name': 'STAND_TEXT'}
        s.import_ogr(self.shp_path, field_mapping, forest_property=self.prop1)

        self.assertEqual(len(Stand.objects.all()), 37)
        # from the 'STAND_TEXT' field this time
        self.assertEqual(len(Stand.objects.filter(name='001A')), 1)
        self.assertEqual(len(Stand.objects.filter(name='277')), 0)
        self.assertEqual(len(self.prop1.feature_set()), 37)
Пример #11
0
    def test_importer_py_newproperty(self):
        self.assertEqual(len(Stand.objects.all()), 0)
        self.assertEqual(len(self.prop1.feature_set()), 0)

        s = StandImporter(self.user)
        s.import_ogr(self.shp_path, new_property_name="Another Property")

        self.assertEqual(len(Stand.objects.all()), 37)
        # from the default 'name' field this time
        self.assertEqual(len(Stand.objects.filter(name='001A')), 0)
        self.assertEqual(len(Stand.objects.filter(name='277')), 1)
        self.assertEqual(len(self.prop1.feature_set()), 0)
        new_stand = ForestProperty.objects.get(name="Another Property")
        self.assertEqual(len(new_stand.feature_set()), 37)
Пример #12
0
    def test_importer_py_newproperty(self):
        self.assertEqual(len(Stand.objects.all()), 0)
        self.assertEqual(len(self.prop1.feature_set()), 0)

        s = StandImporter(self.user)
        s.import_ogr(self.shp_path, new_property_name="Another Property")

        self.assertEqual(len(Stand.objects.all()), 37)
        # from the default 'name' field this time
        self.assertEqual(len(Stand.objects.filter(name="001A")), 0)
        self.assertEqual(len(Stand.objects.filter(name="277")), 1)
        self.assertEqual(len(self.prop1.feature_set()), 0)
        new_stand = ForestProperty.objects.get(name="Another Property")
        self.assertEqual(len(new_stand.feature_set()), 37)
Пример #13
0
    def test_importer_condid(self):
        self.assertEqual(len(Stand.objects.all()), 0)

        self._populate_fvsaggregate()
        s = StandImporter(self.user)
        s.import_ogr(self.condid_shp_path, new_property_name="Locked Property")

        new_property = ForestProperty.objects.get(name="Locked Property")
        self.assertEqual(len(new_property.feature_set(feature_classes=[Stand])), 37)
        self.assertEqual(len([x for x in new_property.feature_set(feature_classes=[Stand]) if x.is_locked]), 37)
        self.assertEqual(len([x for x in new_property.feature_set(feature_classes=[Stand]) if x.cond_id is None]), 0)
        self.assertEqual(
            len([x for x in new_property.feature_set(feature_classes=[Stand]) if x.cond_id != x.locked_cond_id]), 0
        )
        self.assertTrue(new_property.is_locked)

        self.assertEqual(len([x for x in new_property.feature_set(feature_classes=[Stand]) if x.strata is None]), 0)
        self.assertEqual(len(Strata.objects.all()), 4)
Пример #14
0
    def test_importer_condid(self):
        self.assertEqual(len(Stand.objects.all()), 0)

        self._populate_fvsaggregate()
        s = StandImporter(self.user)
        s.import_ogr(self.condid_shp_path, new_property_name="Locked Property")

        new_property = ForestProperty.objects.get(name="Locked Property")
        self.assertEqual(len(new_property.feature_set(feature_classes=[Stand])), 37)
        self.assertEqual(len([x for x in new_property.feature_set(feature_classes=[Stand])
                              if x.is_locked]), 37)
        self.assertEqual(len([x for x in new_property.feature_set(feature_classes=[Stand])
                              if x.cond_id is None]), 0)
        self.assertEqual(len([x for x in new_property.feature_set(feature_classes=[Stand])
                              if x.cond_id != x.locked_cond_id]), 0)
        self.assertTrue(new_property.is_locked)

        self.assertEqual(len([x for x in new_property.feature_set(feature_classes=[Stand])
                              if x.strata is None]), 0)
        self.assertEqual(len(Strata.objects.all()), 4)
    def handle(self, *args, **options):

        try: 
            shp_path = args[0]
            assert os.path.exists(shp_path)
        except (AssertionError, IndexError):
            raise CommandError("Specify shapefile and name field\ne.g. python manage.py import_demo_stands my_stands.shp NAMEFIELD \"My Property\"")

        ds = DataSource(shp_path)
        layer = ds[0]
        
        if layer.srs.srid != settings.GEOMETRY_DB_SRID:
            print "WARNING: Shapefile must be in Web Mercator. If it's not, the results will not be good."

        try: 
            namefld = args[1]
        except (IndexError):
            raise CommandError("Specify shapefile and name field\ne.g. python manage.py import_demo_stands my_stands.shp NAMEFIELD \"My Property\" \nChoice are %s" % (', '.join(layer.fields),))

        if namefld not in layer.fields:
            raise Exception(namefld + " is not a valid field name for this dataset \nChoices are: \n %s" % (', '.join(layer.fields),))

        try:
            property_name = args[2]
        except IndexError:
            property_name = layer.name

        try:
            user = User.objects.get(username='******')
        except User.DoesNotExist:
            user = User.objects.create_user('demo', '*****@*****.**', password='******')

        print "Importing stands..."
        field_mapping = {'name': namefld}
        s = StandImporter(user)
        s.import_ogr(shp_path, field_mapping, new_property_name=property_name, pre_impute=True) 

        prop = ForestProperty.objects.filter(name=property_name).latest('date_modified')
        print "%d stands imported from %s" % (len(prop.feature_set()), shp_path)
Пример #16
0
 def test_importer_badcondid(self):
     self.assertEqual(len(Stand.objects.all()), 0)
     s = StandImporter(self.user)
     with self.assertRaises(Exception):
         # haven't populated the fvsaggregate table so upload is invalid
         s.import_ogr(self.condid_shp_path, new_property_name="Locked Property")
Пример #17
0
 def test_importer_badcondid(self):
     self.assertEqual(len(Stand.objects.all()), 0)
     s = StandImporter(self.user)
     with self.assertRaises(Exception):
         # haven't populated the fvsaggregate table so upload is invalid
         s.import_ogr(self.condid_shp_path, new_property_name="Locked Property")
Пример #18
0
def upload_stands(request):
    '''
    Upload stands via OGR datasource
    '''
    from trees.forms import UploadStandsForm
    from trees.models import ForestProperty
    import tempfile
    import zipfile

    if not request.user.is_authenticated():
        return HttpResponse('You must be logged in.', status=401)

    if request.method == 'POST':
        form = UploadStandsForm(request.POST, request.FILES)

        if form.is_valid():
            # confirm property
            try:
                prop_pk = request.POST['property_pk']
            except KeyError:
                prop_pk = None

            try:
                new_prop_name = request.POST['new_property_name']
            except KeyError:
                new_prop_name = None

            if not prop_pk and not new_prop_name:
                return HttpResponse('You must provide either a new property name or existing property id.', status=401)

            # Save to disk
            f = request.FILES['ogrfile']
            tempdir = os.path.join(
                tempfile.gettempdir(), request.user.username)
            if not os.path.exists(tempdir):
                os.makedirs(tempdir)
            ogr_path = os.path.join(tempdir, f.name)
            dest = open(ogr_path, 'wb+')
            for chunk in f.chunks():
                dest.write(chunk)
            dest.close()

            # if zipfile, unzip and change ogr_path
            if ogr_path.endswith('zip'):
                shp_path = None
                zf = zipfile.ZipFile(ogr_path)
                for info in zf.infolist():
                    contents = zf.read(info.filename)
                    shp_part = os.path.join(
                        tempdir, info.filename.split(os.path.sep)[-1])
                    fout = open(shp_part, "wb")
                    fout.write(contents)
                    fout.close()
                    if shp_part.endswith(".shp"):
                        shp_path = shp_part
                ogr_path = shp_path

            assert os.path.exists(ogr_path)

            # Import
            from trees.utils import StandImporter
            fp = None
            try:
                s = StandImporter(request.user)

                if new_prop_name:
                    s.import_ogr(ogr_path,
                                 new_property_name=new_prop_name, pre_impute=True)
                else:
                    try:
                        fp = ForestProperty.objects.get(pk=prop_pk)
                    except ForestProperty.DoesNotExist:
                        return HttpResponse('<p class="label label-important">Could not find forest property %s</p>' % prop_pk, status=404)
                    s.import_ogr(ogr_path, forest_property=fp, pre_impute=True)
            except Exception as err:
                return HttpResponse('<p class="label label-important">Error importing stands:\n%s</p>' % (err,), status=500)

            if not fp:
                fp = ForestProperty.objects.filter(
                    name=new_prop_name).latest('date_modified')
            res = HttpResponse(json.dumps({'X-Madrona-Select': fp.uid}),
                               status=201, mimetype='application/json')
            return res
    else:
        form = UploadStandsForm()

    return HttpResponse('<p class="label label-important">Error importing stands: Invalid form submission</p>', status=400)
Пример #19
0
def upload_stands(request):
    '''
    Upload stands via OGR datasource
    '''
    from trees.forms import UploadStandsForm
    from trees.models import ForestProperty
    import tempfile
    import zipfile

    if not request.user.is_authenticated():
        return HttpResponse('You must be logged in.', status=401)

    if request.method == 'POST':
        form = UploadStandsForm(request.POST, request.FILES)

        if form.is_valid():
            # confirm property
            try:
                prop_pk = request.POST['property_pk']
            except KeyError:
                prop_pk = None

            try:
                new_prop_name = request.POST['new_property_name']
            except KeyError:
                new_prop_name = None

            if not prop_pk and not new_prop_name:
                return HttpResponse('You must provide either a new property name or existing property id.', status=401)

            # Save to disk
            f = request.FILES['ogrfile']
            tempdir = os.path.join(
                tempfile.gettempdir(), request.user.username)
            if not os.path.exists(tempdir):
                os.makedirs(tempdir)
            ogr_path = os.path.join(tempdir, f.name)
            dest = open(ogr_path, 'wb+')
            for chunk in f.chunks():
                dest.write(chunk)
            dest.close()

            # if zipfile, unzip and change ogr_path
            if ogr_path.endswith('zip'):
                shp_path = None
                zf = zipfile.ZipFile(ogr_path)
                for info in zf.infolist():
                    contents = zf.read(info.filename)
                    shp_part = os.path.join(
                        tempdir, info.filename.split(os.path.sep)[-1])
                    fout = open(shp_part, "wb")
                    fout.write(contents)
                    fout.close()
                    if shp_part.endswith(".shp"):
                        shp_path = shp_part
                ogr_path = shp_path

            assert os.path.exists(ogr_path)

            # Import
            from trees.utils import StandImporter
            fp = None
            try:
                s = StandImporter(request.user)

                if new_prop_name:
                    s.import_ogr(ogr_path,
                                 new_property_name=new_prop_name,
                                 pre_impute=True)
                else:
                    try:
                        fp = ForestProperty.objects.get(pk=prop_pk)
                    except ForestProperty.DoesNotExist:
                        return HttpResponse('<p class="label label-important">Could not find forest property %s</p>' % prop_pk, status=404)
                    s.import_ogr(ogr_path, forest_property=fp, pre_impute=True)
            except Exception as err:
                return HttpResponse('<p class="label label-important">Error importing stands:\n%s</p>' % (err,), status=500)

            if not fp:
                fp = ForestProperty.objects.filter(
                    name=new_prop_name).latest('date_modified')
            res = HttpResponse(json.dumps({'X-Madrona-Select': fp.uid}),
                               status=201, mimetype='application/json')
            return res
    else:
        form = UploadStandsForm()

    return HttpResponse('<p class="label label-important">Error importing stands: Invalid form submission</p>', status=400)