def import_all_layers(uploaded_data, owner=None): """ Imports all layers of *uploaded_data*. *uploaded_data* is a saved UploadedData instance. *return* Number of layers imported. """ from osgeo_importer.tasks import import_object from osgeo_importer.inspectors import GDALInspector logger.info('Importing all layers for UploadedData({})'.format(uploaded_data.id)) if owner is None: User = get_user_model() owner = User.objects.get(username='******') import_results = [] for uploaded_file in uploaded_data.uploadfile_set.all(): msg = 'Importing file "{}" from UploadedData({})'.format(uploaded_file.name, uploaded_data.id) logger.info(msg) gi = GDALInspector(uploaded_file.file.path) all_layer_details = gi.describe_fields() for (layer_details, upload_layer) in zip(all_layer_details, uploaded_file.uploadlayer_set.all()): configuration_options = layer_details.copy() configuration_options.update({ 'layer_owner': owner.username, 'layer_type': upload_layer.layer_type, 'upload_layer_id': upload_layer.id, 'layer_name': upload_layer.layer_name }) msg = 'Kicking off a celery task to import layer: {}'.format(upload_layer.layer_name) logger.info(msg) import_result = import_object.delay( upload_layer.upload_file.id, configuration_options=configuration_options ) import_results.append(import_result) logger.info('All layer import tasks started') return len(import_results)
def test_gdal_file_type(self): """ Tests the describe fields functionality. """ files = [ (os.path.join(os.path.dirname(__file__), '..', 'importer-test-files', 'US_Shootings.csv'), 'CSV'), (os.path.join(os.path.dirname(__file__), '..', 'importer-test-files', 'point_with_date.geojson'), 'GeoJSON'), (os.path.join(os.path.dirname(__file__), '..', 'importer-test-files', 'mojstrovka.gpx'), 'GPX'), (os.path.join(os.path.dirname(__file__), '..', 'importer-test-files', 'us_states.kml'), 'KML'), (os.path.join(os.path.dirname(__file__), '..', 'importer-test-files', 'boxes_with_year_field.shp'), 'ESRI Shapefile'), (os.path.join(os.path.dirname(__file__), '..', 'importer-test-files', 'boxes_with_date_iso_date.zip'), 'ESRI Shapefile'), ] from .models import NoDataSourceFound try: for path, file_type in files: with GDALInspector(path) as f: self.assertEqual(f.file_type(), file_type) except NoDataSourceFound as e: print 'No data source found in: {0}'.format(path) raise e
def test_describe_fields(self): """Tests the describe fields functionality. """ path = test_file('US_Shootings.csv') with GDALInspector(path) as inspector: layers = inspector.describe_fields() self.assertTrue(layers[0]['layer_name'], 'us_shootings') self.assertEqual([n['name'] for n in layers[0]['fields']], ['Date', 'Shooter', 'Killed', 'Wounded', 'Location', 'City', 'Longitude', 'Latitude']) self.assertEqual(layers[0]['feature_count'], 203)
def test_describe_fields(self): """ Tests the describe fields functionality. """ f = os.path.join(os.path.dirname(__file__), '..', 'importer-test-files', 'US_Shootings.csv') with GDALInspector(f) as f: layers = f.describe_fields() self.assertTrue(layers[0]['layer_name'], 'us_shootings') self.assertEqual([n['name'] for n in layers[0]['fields']], [ 'Date', 'Shooter', 'Killed', 'Wounded', 'Location', 'City', 'Longitude', 'Latitude' ]) self.assertEqual(layers[0]['feature_count'], 203)
def test_gdal_file_type(self): """Tests the describe fields functionality. """ filenames = { 'US_Shootings.csv': {'CSV'}, 'point_with_date.geojson': {'GeoJSON'}, 'mojstrovka.gpx': {'GPX'}, 'us_states.kml': {'LIBKML', 'KML'}, 'boxes_with_year_field.shp': {'ESRI Shapefile'}, 'boxes_with_date_iso_date.zip': {'ESRI Shapefile'} } from osgeo_importer.models import NoDataSourceFound try: for filename, file_type in sorted(filenames.items()): path = test_file(filename) with GDALInspector(path) as inspector: self.assertIn(inspector.file_type(), file_type) except NoDataSourceFound: logging.exception('No data source found in: {0}'.format(path)) raise