Beispiel #1
0
    def test_file_to_geojson(
        self,
        mock_listdir,
        mock_get_meta,
        mock_exists,
        mock_read_json_file,
        mock_rmtree,
        mock_convert_vector,
        mock_polygonize,
        mock_unzip_file,
    ):
        expected_geojson = {"type": "FeatureCollection", "other_stuff": {}}
        stage_dir = "/var/lib/stage"
        file_name_stem = "test_file"
        input_path = f"{stage_dir}/{file_name_stem}.geojson"
        expected_output_path = f"{stage_dir}/out_{file_name_stem}.geojson"
        mock_convert_vector.return_value = expected_output_path
        mock_exists.return_value = True
        mock_read_json_file.return_value = expected_geojson

        # Test raster
        mock_get_meta.return_value = {"driver": "GeoJSON", "is_raster": True}
        self.assertEqual(expected_geojson, file_to_geojson(input_path))
        mock_polygonize.assert_called_once_with(input_path,
                                                expected_output_path)

        # Test vector
        mock_get_meta.return_value = {"driver": "GeoJSON", "is_raster": False}
        self.assertEqual(expected_geojson, file_to_geojson(input_path))
        mock_convert_vector.assert_called_once_with(input_path,
                                                    expected_output_path,
                                                    driver="geojson")
        mock_convert_vector.reset_mock()
        mock_rmtree(stage_dir)

        # Test shapefile
        input_path = f"{stage_dir}/{file_name_stem}.zip"
        mock_unzip_file.return_value = True
        shapefile = "test.shp"
        mock_listdir.return_value = [shapefile]
        expected_input_path = f"{stage_dir}/{shapefile}"
        self.assertEqual(expected_geojson, file_to_geojson(input_path))
        mock_convert_vector.assert_called_once_with(expected_input_path,
                                                    expected_output_path,
                                                    driver="geojson")

        with self.assertRaises(Exception):
            # Test bad file
            mock_exists.return_value = False
            file_to_geojson(input_path)
Beispiel #2
0
    def update_geom(self):
        from eventkit_cloud.tasks.helpers import download_data
        from eventkit_cloud.ui.helpers import file_to_geojson

        geometry = None
        if self.config != self.__config:
            orig_extent_url = load_provider_config(
                self.__config).get("extent_url")
            config = load_provider_config(self.config)
            extent_url = config.get("extent_url")
            if extent_url and extent_url != orig_extent_url:
                random_uuid = uuid.uuid4()
                session = get_or_update_session(**config)
                if not extent_url:
                    return
                output_file = download_data(task_uid=str(random_uuid),
                                            input_url=extent_url,
                                            session=session)
                geojson = file_to_geojson(output_file)
                geojson_geometry = geojson.get("geometry") or geojson.get(
                    "features", [{}])[0].get("geometry")
                geometry = GEOSGeometry(json.dumps(geojson_geometry),
                                        srid=4326)
        elif (self.url != self.__url) or (self.layer != self.__layer):
            try:
                client = self.get_service_client()
                geometry = client.download_geometry()
            except AttributeError as e:
                # TODO: This fails in tests.  How to handle failure to update geometry?
                logger.info(e, exc_info=True)
        if geometry:
            self.the_geom = convert_polygon(geometry)
Beispiel #3
0
def convert_to_geojson(request):
    file = request.FILES.get('file', None)
    if not file:
        return HttpResponse('No file supplied in the POST request', status=400)
    try:
        geojson = file_to_geojson(file)
        return HttpResponse(json.dumps(geojson), content_type="application/json", status=200)
    except Exception as e:
        logger.error(e)
        return HttpResponse(str(e), status=400)
Beispiel #4
0
def convert_to_geojson(request):
    file = request.FILES.get('file', None)
    if not file:
        return HttpResponse('No file supplied in the POST request', status=400)
    try:
        geojson = file_to_geojson(file)
        return HttpResponse(json.dumps(geojson), content_type="application/json", status=200)
    except Exception as e:
        logger.error(e)
        return HttpResponse(str(e), status=400)
Beispiel #5
0
def convert_to_geojson(request):
    in_memory_file = request.FILES.get("file", None)
    if not in_memory_file:
        return HttpResponse("No file supplied in the POST request", status=400)
    try:
        output_file = write_uploaded_file(in_memory_file)
        geojson = file_to_geojson(output_file)
        return HttpResponse(json.dumps(geojson),
                            content_type="application/json",
                            status=200)
    except Exception as e:
        logger.error(e)
        return HttpResponse(str(e), status=400)
Beispiel #6
0
    def update_geom(self):
        from eventkit_cloud.tasks.helpers import download_data
        from eventkit_cloud.ui.helpers import file_to_geojson

        if self.config != self.__config:
            orig_extent_url = load_provider_config(self.__config).get("extent_url")
            config = load_provider_config(self.config)
            extent_url = config.get("extent_url")
            if extent_url and extent_url != orig_extent_url:
                random_uuid = uuid.uuid4()
                session = get_or_update_session(**config)
                if not extent_url:
                    return
                output_file = download_data(task_uid=str(random_uuid), input_url=extent_url, session=session)
                geojson = file_to_geojson(output_file)
                geometry = geojson.get("geometry") or geojson.get("features", [{}])[0].get("geometry")
                if geometry:
                    self.the_geom = convert_polygon(GEOSGeometry(json.dumps(geometry), srid=4326))
    def test_file_to_geojson(self, uid, makedir, write, unzip, listdirs, meta, subproc, exists, reader, rm, split):
        geojson = {'type': 'FeatureCollection', 'other_stuff': {}}
        file = Mock()
        file.name = 'test_file.geojson'
        split.return_value = 'test_file', '.geojson'
        uid.return_value = 12345
        makedir.return_value = True
        write.return_value = True
        unzip.return_value = False
        listdirs.return_value = []
        meta.return_value = {'driver': 'GeoJSON', 'is_raster': False}
        proc = Mock()
        proc.wait = Mock()
        subproc.Popen.return_value = proc
        exists.return_value = True
        reader.return_value = geojson
        with self.settings(
            EXPORT_STAGING_ROOT='/var/lib/stage'
        ):
            # It should run through the entire process for a geojson file and return it
            dir = '/var/lib/stage/12345'
            expected_file_name, expected_file_ext = os.path.splitext(file.name)
            expected_in_path = os.path.join(dir, 'in_{0}{1}'.format(expected_file_name, expected_file_ext))
            expected_out_path = os.path.join(dir, 'out_{0}.geojson'.format(expected_file_name))
            ret = file_to_geojson(file)
            self.assertEqual(ret, geojson)
            makedir.assert_called_once_with(dir)
            write.assert_called_once_with(file, expected_in_path)
            meta.assert_called_once_with(expected_in_path)
            expected_cmd = "ogr2ogr -f geojson {0} {1}".format(expected_out_path, expected_in_path)
            subproc.Popen.assert_called_once_with(expected_cmd, shell=True, executable='/bin/bash')
            rm.assert_called_once_with(dir)


            # It should run through the entire process for a zip shp and return a geojson
            file.name = 'something.zip'
            split.return_value = 'something', '.zip'
            unzip.return_value = True
            expected_file_name, expected_file_ext = os.path.splitext(file.name)
            expected_in_path = os.path.join(dir, 'in_{0}{1}'.format(expected_file_name, expected_file_ext))
            expected_out_path = os.path.join(dir, 'out_{0}.geojson'.format(expected_file_name))
            listdirs.return_value = ['something.shp']
            updated_in_path = os.path.join(dir, 'something.shp')
            updated_cmd = 'ogr2ogr -f geojson {0} {1}'.format(expected_out_path, updated_in_path)
            ret = file_to_geojson(file)
            self.assertEqual(ret, geojson)
            unzip.assert_called_once_with(expected_in_path, dir)
            listdirs.assert_called_with(dir)
            meta.assert_called_with(updated_in_path)
            subproc.Popen.called_with(updated_cmd, shell=True, executable='/bin/bash')

            # It should raise an exception if there is no file extension
            file.name = 'something'
            split.return_value = 'something', ''
            self.assertRaises(Exception, file_to_geojson, file)

            # It should raise an exception if zip does not contain shp
            file.name = 'thing.zip'
            split.return_value = 'thing', '.zip'
            unzip.return_value = True
            listdirs.return_value = ['thing.dbf', 'thing.prj']
            self.assertRaises(Exception, file_to_geojson, file)

            # It should raise an exception if no driver can be found
            file.name = 'test.geojson'
            split.return_value = 'test', '.geojson'
            meta.return_value = {'driver': None, 'is_raster': None}
            self.assertRaises(Exception, file_to_geojson, file)

            # It should raise an exception if input file is not vector
            meta.return_value = {'driver': 'GTiff', 'is_raster': True}
            self.assertRaises(Exception, file_to_geojson, file)

            # It should raise an exception if the subprocess throws one
            meta.return_value = {'driver': 'GeoJSON', 'is_raster': False}
            subproc.Popen.side_effect = Exception('doh!')
            self.assertRaises(Exception, file_to_geojson, file)

            # It should raise and exception if output file does not exist
            subproc.Popen.side_effect = None
            exists.return_value = False
            self.assertRaises(Exception, file_to_geojson, file)
    def test_file_to_geojson(self, uid, makedir, write, unzip, listdirs, meta,
                             subproc, exists, reader, rm, split):
        geojson = {'type': 'FeatureCollection', 'other_stuff': {}}
        file = Mock()
        file.name = 'test_file.geojson'
        split.return_value = 'test_file', '.geojson'
        uid.return_value = 12345
        makedir.return_value = True
        write.return_value = True
        unzip.return_value = False
        listdirs.return_value = []
        meta.return_value = {'driver': 'GeoJSON', 'is_raster': False}
        proc = Mock()
        proc.wait = Mock()
        subproc.Popen.return_value = proc
        exists.return_value = True
        reader.return_value = geojson
        with self.settings(EXPORT_STAGING_ROOT='/var/lib/stage'):
            # It should run through the entire process for a geojson file and return it
            dir = '/var/lib/stage/12345'
            expected_file_name, expected_file_ext = os.path.splitext(file.name)
            expected_in_path = os.path.join(
                dir, 'in_{0}{1}'.format(expected_file_name, expected_file_ext))
            expected_out_path = os.path.join(
                dir, 'out_{0}.geojson'.format(expected_file_name))
            ret = file_to_geojson(file)
            self.assertEqual(ret, geojson)
            makedir.assert_called_once_with(dir)
            write.assert_called_once_with(file, expected_in_path)
            meta.assert_called_once_with(expected_in_path)
            expected_cmd = "ogr2ogr -f geojson {0} {1}".format(
                expected_out_path, expected_in_path)
            subproc.Popen.assert_called_once_with(expected_cmd,
                                                  shell=True,
                                                  executable='/bin/bash')
            rm.assert_called_once_with(dir)

            # It should run through the entire process for a zip shp and return a geojson
            file.name = 'something.zip'
            split.return_value = 'something', '.zip'
            unzip.return_value = True
            expected_file_name, expected_file_ext = os.path.splitext(file.name)
            expected_in_path = os.path.join(
                dir, 'in_{0}{1}'.format(expected_file_name, expected_file_ext))
            expected_out_path = os.path.join(
                dir, 'out_{0}.geojson'.format(expected_file_name))
            listdirs.return_value = ['something.shp']
            updated_in_path = os.path.join(dir, 'something.shp')
            updated_cmd = 'ogr2ogr -f geojson {0} {1}'.format(
                expected_out_path, updated_in_path)
            ret = file_to_geojson(file)
            self.assertEqual(ret, geojson)
            unzip.assert_called_once_with(expected_in_path, dir)
            listdirs.assert_called_with(dir)
            meta.assert_called_with(updated_in_path)
            subproc.Popen.called_with(updated_cmd,
                                      shell=True,
                                      executable='/bin/bash')

            # It should raise an exception if there is no file extension
            file.name = 'something'
            split.return_value = 'something', ''
            self.assertRaises(Exception, file_to_geojson, file)

            # It should raise an exception if zip does not contain shp
            file.name = 'thing.zip'
            split.return_value = 'thing', '.zip'
            unzip.return_value = True
            listdirs.return_value = ['thing.dbf', 'thing.prj']
            self.assertRaises(Exception, file_to_geojson, file)

            # It should raise an exception if no driver can be found
            file.name = 'test.geojson'
            split.return_value = 'test', '.geojson'
            meta.return_value = {'driver': None, 'is_raster': None}
            self.assertRaises(Exception, file_to_geojson, file)

            # It should raise an exception if input file is not vector
            meta.return_value = {'driver': 'GTiff', 'is_raster': True}
            self.assertRaises(Exception, file_to_geojson, file)

            # It should raise an exception if the subprocess throws one
            meta.return_value = {'driver': 'GeoJSON', 'is_raster': False}
            subproc.Popen.side_effect = Exception('doh!')
            self.assertRaises(Exception, file_to_geojson, file)

            # It should raise and exception if output file does not exist
            subproc.Popen.side_effect = None
            exists.return_value = False
            self.assertRaises(Exception, file_to_geojson, file)