Example #1
0
    def process(self):
        log.debug('Start vector export process. Task %d' % self.task_id)
        try:
            with self.task() as task:
                mapping = mappings[task.mapping_name].copy()
                output_file = self.app_state.user_data_path('export', task.project.title, task.mapping_name + '.shp', make_dirs=True)

                couch = CouchDB('http://%s:%s' % ('127.0.0.1', self.app_state.config.get('couchdb', 'port')), task.db_name)
                write_json_to_shape(couch.load_records(), mapping, output_file)
            self.task_done()
        except ConvertError, e:
            self.task_failed(e)
Example #2
0
    def process(self):
        log.debug('Start vector export process. Task %d' % self.task_id)
        try:
            with self.task() as task:
                couch = VectorCouchDB('http://%s:%s' % ('127.0.0.1', self.app_state.config.get('couchdb', 'port')), task.db_name, task.title)

                if task.type_ == 'geojson':
                    # use geojson if is in task - otherwise load from database
                    if not task.geojson:
                        data = json.dumps(create_feature_collection(couch.load_features()))
                    else:
                        data = task.geojson

                    # check metadata for jsonSchema and restrict feature properties to
                    # schema properties of schema don't allow additional properties
                    metadata = couch.metadata()
                    if metadata and 'appOptions' in metadata and 'jsonSchema' in metadata['appOptions'] and 'additionalProperties' in metadata['appOptions']['jsonSchema']['schema'] and metadata['appOptions']['jsonSchema']['schema']['additionalProperties'] == False:
                        schema_properties = metadata['appOptions']['jsonSchema']['schema']['properties'].keys()
                        data = json.loads(data)
                        for feature in data['features']:
                            export_properties = {}
                            for key in feature['properties'].keys():
                                if key in schema_properties:
                                    export_properties[key] = feature['properties'][key]
                            feature['properties'] = export_properties
                        data = json.dumps(data)

                    if task.destination != 'file':
                        dest_couch = CouchFileBox('http://%s:%s' % ('127.0.0.1', self.app_state.config.get('couchdb', 'port')), task.destination)

                        file_obj = {'content-type': 'application/json' , 'file': data, 'filename': task.file_name + '.json' }
                        dest_couch.store_file(file_obj, overwrite=True)
                    else:
                        output_file = self.app_state.user_data_path('export', 'vector', task.file_name + '.json', make_dirs=True)
                        write_json_to_file(json.loads(data), output_file)

                elif task.type_ == 'shp':
                    if task.destination != 'file':
                        dest_couch = CouchFileBox('http://%s:%s' % ('127.0.0.1', self.app_state.config.get('couchdb', 'port')), task.destination)
                        temp_dir = mkdtemp()

                        output_file = path.join(temp_dir, task.file_name + '.shp')
                        fields = fields_from_properties(couch.load_features())
                        mapping = Mapping(None, None, 'Polygon', other_srs=task.srs, fields=fields)

                        # create shape
                        write_json_to_shape(couch.load_features(), mapping, output_file)
                        zip_file = zip_shapefiles(output_file)
                        dest_couch.store_file({'content-type': 'application/zip', 'file': zip_file.getvalue(), 'filename': task.file_name + '.zip' }, overwrite=True)

                        rmtree(temp_dir)
                    else:
                        output_file = self.app_state.user_data_path('export',  'vector', task.file_name+ '.shp', make_dirs=True)
                        # create fields for shp - use for mapping
                        fields = fields_from_properties(couch.load_features())
                        mapping = Mapping(None, None, 'Polygon', other_srs=task.srs, fields=fields)
                        # create shape
                        write_json_to_shape(couch.load_features(), mapping, output_file)
            self.task_done()
        except ConvertError, e:
            self.task_failed(e)
Example #3
0
    def process(self):
        log.debug('Start vector export process. Task %d' % self.task_id)
        try:
            with self.task() as task:
                couch = VectorCouchDB(
                    'http://%s:%s' %
                    ('127.0.0.1', self.app_state.config.get(
                        'couchdb', 'port')), task.db_name, task.title)

                if task.type_ == 'geojson':
                    # use geojson if is in task - otherwise load from database
                    if not task.geojson:
                        data = json.dumps(
                            create_feature_collection(couch.load_features()))
                    else:
                        data = task.geojson

                    # check metadata for jsonSchema and restrict feature properties to
                    # schema properties of schema don't allow additional properties
                    metadata = couch.metadata()
                    if metadata and 'appOptions' in metadata and 'jsonSchema' in metadata[
                            'appOptions'] and 'additionalProperties' in metadata[
                                'appOptions']['jsonSchema'][
                                    'schema'] and metadata['appOptions'][
                                        'jsonSchema']['schema'][
                                            'additionalProperties'] == False:
                        schema_properties = metadata['appOptions'][
                            'jsonSchema']['schema']['properties'].keys()
                        data = json.loads(data)
                        for feature in data['features']:
                            export_properties = {}
                            for key in feature['properties'].keys():
                                if key in schema_properties:
                                    export_properties[key] = feature[
                                        'properties'][key]
                            feature['properties'] = export_properties
                        data = json.dumps(data)

                    if task.destination != 'file':
                        dest_couch = CouchFileBox(
                            'http://%s:%s' %
                            ('127.0.0.1',
                             self.app_state.config.get('couchdb', 'port')),
                            task.destination)

                        file_obj = {
                            'content-type': 'application/json',
                            'file': data,
                            'filename': task.file_name + '.json'
                        }
                        dest_couch.store_file(file_obj, overwrite=True)
                    else:
                        output_file = self.app_state.user_data_path(
                            'export',
                            'vector',
                            task.file_name + '.json',
                            make_dirs=True)
                        write_json_to_file(json.loads(data), output_file)

                elif task.type_ == 'shp':
                    if task.destination != 'file':
                        dest_couch = CouchFileBox(
                            'http://%s:%s' %
                            ('127.0.0.1',
                             self.app_state.config.get('couchdb', 'port')),
                            task.destination)
                        temp_dir = mkdtemp()

                        output_file = path.join(temp_dir,
                                                task.file_name + '.shp')
                        fields = fields_from_properties(couch.load_features())
                        mapping = Mapping(None,
                                          None,
                                          'Polygon',
                                          other_srs=task.srs,
                                          fields=fields)

                        # create shape
                        write_json_to_shape(couch.load_features(), mapping,
                                            output_file)
                        zip_file = zip_shapefiles(output_file)
                        dest_couch.store_file(
                            {
                                'content-type': 'application/zip',
                                'file': zip_file.getvalue(),
                                'filename': task.file_name + '.zip'
                            },
                            overwrite=True)

                        rmtree(temp_dir)
                    else:
                        output_file = self.app_state.user_data_path(
                            'export',
                            'vector',
                            task.file_name + '.shp',
                            make_dirs=True)
                        # create fields for shp - use for mapping
                        fields = fields_from_properties(couch.load_features())
                        mapping = Mapping(None,
                                          None,
                                          'Polygon',
                                          other_srs=task.srs,
                                          fields=fields)
                        # create shape
                        write_json_to_shape(couch.load_features(), mapping,
                                            output_file)
            self.task_done()
        except ConvertError, e:
            self.task_failed(e)