Example #1
0
def write_back(layer=None, ajax=True):
    user = current_user

    connection = psycopg2.connect(
        database=current_app.config.get('TEMP_PG_DB'),
        host=current_app.config.get('TEMP_PG_HOST'),
        user=current_app.config.get('TEMP_PG_USER'),
        password=current_app.config.get('TEMP_PG_PASSWORD'),
        sslmode='allow',
    )

    couch = CouchDBBox(current_app.config.get('COUCH_DB_URL'), '%s_%s' % (SystemConfig.AREA_BOX_NAME, user.id))
    schema = couch.layer_schema(layer)
    extend_schema_for_couchdb(schema)
    tablename = 'tmp%s%s' % (user.id, layer)
    tmp_db = TempPGDB(connection=connection, tablename=tablename, schema=schema)
    couch.store_features(layer, tmp_db.load_features(), delete_missing=tmp_db.imported_feature_ids())
    connection.close()

    # write changes back to PostGIS to prevent multiple inserts
    create_wfs(user, [layer])

    signals.features_updated.send(user)

    if ajax:
        return Response(response='success', status=200, headers=None, mimetype='application/json', content_type=None)
Example #2
0
def write_back(layer=None, ajax=True):
    user = current_user

    connection = psycopg2.connect(
        database=current_app.config.get('TEMP_PG_DB'),
        host=current_app.config.get('TEMP_PG_HOST'),
        user=current_app.config.get('TEMP_PG_USER'),
        password=current_app.config.get('TEMP_PG_PASSWORD'),
        sslmode='allow',
    )

    couch = CouchDBBox(current_app.config.get('COUCH_DB_URL'), '%s_%s' % (SystemConfig.AREA_BOX_NAME, user.id))
    schema = couch.layer_schema(layer)
    extend_schema_for_couchdb(schema)
    tablename = 'tmp%s%s' % (user.id, layer)
    tmp_db = TempPGDB(connection=connection, tablename=tablename, schema=schema)
    couch.store_features(layer, tmp_db.load_features(), delete_missing=tmp_db.imported_feature_ids())
    connection.close()

    signals.features_updated.send(user, layer=layer)

    if ajax:
        return Response(response='success', status=200, headers=None, mimetype='application/json', content_type=None)
Example #3
0
def create_wfs(user=None, layers=None):
    connection = psycopg2.connect(
        database=current_app.config.get('TEMP_PG_DB'),
        host=current_app.config.get('TEMP_PG_HOST'),
        user=current_app.config.get('TEMP_PG_USER'),
        password=current_app.config.get('TEMP_PG_PASSWORD'),
        sslmode='allow',
    )
    couch = CouchDBBox(current_app.config.get('COUCH_DB_URL'), '%s_%s' % (SystemConfig.AREA_BOX_NAME, user.id))

    wfs_layer_token = uuid.uuid4().hex

    wfs = []
    tinyows_layers = []
    titles = dict(couch.get_layer_names())
    for id, layer in enumerate(layers):
        title = titles[layer] if layer in titles else layer
        wfs_layer = {
            'id': id,
            'name': title,
            'layer': layer,
            'url': url_for('.tinyows_wfs', token=wfs_layer_token, _external=True) + '?',
            'srs': 'EPSG:3857',
            'geometry_field': 'geometry',
            'wfs_version': '1.1.0',
            'feature_ns': current_app.config.get('TINYOWS_NS_URI'),
            'typename': current_app.config.get('TINYOWS_NS_PREFIX'),
            'writable': False,
            'display_in_layerswitcher': True,
        }
        if layer == current_app.config.get('EXTERNAL_WFS_LAYER'):
            wfs_layer['name'] = current_app.config.get('EXTERNAL_WFS_NAME')
            wfs_layer['url'] = current_app.config.get('EXTERNAL_WFS_URL')
            wfs_layer['layer'] = current_app.config.get('EXTERNAL_WFS_LAYER')
            wfs_layer['srs'] = current_app.config.get('EXTERNAL_WFS_SRS')
            wfs_layer['geometry_field'] = current_app.config.get('EXTERNAL_WFS_GEOMETRY')
            wfs_layer['feature_ns'] = current_app.config.get('EXTERNAL_WFS_NS_URI')
            wfs_layer['typename'] = current_app.config.get('EXTERNAL_WFS_NS_PREFIX')
            wfs_layer['max_features'] = current_app.config.get('EXTERNAL_WFS_MAX_FEATURES')
            wfs_layer['display_in_layerswitcher'] = False
        else:
            schema = couch.layer_schema(layer)
            if not schema or 'properties' not in schema:
                raise MissingSchemaError('no schema found for layer %s' % layer)
            extend_schema_for_couchdb(schema)
            # tinyows layername must not contain underscores
            tablename = 'tmp%s%s' % (user.id, layer)
            tmp_db = TempPGDB(connection=connection, tablename=tablename, schema=schema)
            tmp_db.create_table()
            tmp_db.insert_features(couch.iter_layer_features(layer))
            # TODO remember created table in new model, store wfs_layer_token
            # and remove old tinyows configs on update
            wfs_layer['layer'] = tablename
            wfs_layer['writable'] = current_app.config.get('USER_READONLY_LAYER') != layer
            tinyows_layers.append({
                'name': tablename,
                'title': wfs_layer['name'],
                'writable': '1' if wfs_layer['writable'] else '0',
            })
        wfs.append(wfs_layer)
    connection.commit()
    connection.close()

    ensure_dir(current_app.config.get('TINYOWS_TMP_CONFIG_DIR'))

    tinyows_config = os.path.join(
        current_app.config.get('TINYOWS_TMP_CONFIG_DIR'),
        wfs_layer_token + '.xml')

    tinyows.build_config(current_app, tinyows_layers, wfs_layer_token, tinyows_config)

    return wfs, wfs_layer_token
Example #4
0
def create_wfs(user=None, editable_layers=None):
    connection = psycopg2.connect(
        database=current_app.config.get('TEMP_PG_DB'),
        host=current_app.config.get('TEMP_PG_HOST'),
        user=current_app.config.get('TEMP_PG_USER'),
        password=current_app.config.get('TEMP_PG_PASSWORD'),
        sslmode='allow',
    )

    couch = CouchDBBox(current_app.config.get('COUCH_DB_URL'), '%s_%s' % (SystemConfig.AREA_BOX_NAME, user.id))
    wfs_layer_token = uuid.uuid4().hex

    wfs = []

    # create layer to edit
    tinyows_layers = []
    titles = dict(couch.get_layer_names())

    for layer in editable_layers:
        wfs_layer = {
            'id': id,
            'name': titles[layer],
            'layer': layer,
            'url': url_for('.tinyows_wfs', token=wfs_layer_token, _external=True) + '?',
            'srs': 'EPSG:3857',
            'geometry_field': 'geometry',
            'wfs_version': '1.1.0',
            'feature_ns': current_app.config.get('TINYOWS_NS_URI'),
            'typename': current_app.config.get('TINYOWS_NS_PREFIX'),
            'writable': False,
            'display_in_layerswitcher': True,
        }
        schema = couch.layer_schema(layer)
        if not schema or 'properties' not in schema:
            raise MissingSchemaError('no schema found for layer %s' % layer)
        extend_schema_for_couchdb(schema)
        # tinyows layername must not contain underscores
        tablename = 'tmp%s%s' % (user.id, layer)
        tmp_db = TempPGDB(connection=connection, tablename=tablename, schema=schema)
        tmp_db.create_table()
        tmp_db.insert_features(couch.iter_layer_features(layer))
        # TODO remember created table in new model, store wfs_layer_token
        # and remove old tinyows configs on update
        wfs_layer['layer'] = tablename
        wfs_layer['writable'] = current_app.config.get('USER_READONLY_LAYER') != layer
        tinyows_layers.append({
            'name': tablename,
            'title': wfs_layer['name'],
            'writable': '1' if wfs_layer['writable'] else '0',
        })
        wfs.append(wfs_layer)

    connection.commit()
    connection.close()
    ensure_dir(current_app.config.get('TINYOWS_TMP_CONFIG_DIR'))

    tinyows_config = os.path.join(
        current_app.config.get('TINYOWS_TMP_CONFIG_DIR'),
        wfs_layer_token + '.xml')

    tinyows.build_config(current_app, tinyows_layers, wfs_layer_token, tinyows_config)

    # wfs_layers for search
    wfs_search =  db.session.query(WFS).all()
    for layer in wfs_search:
        wfs.append({
            'id': layer.id,
            'name': layer.name,
            'layer': layer.layer,
            'url': layer.url,
            'srs': layer.srs,
            'geometry_field': layer.geometry,
            'wfs_version': '1.1.0',
            'feature_ns': layer.ns_uri,
            'typename': layer.ns_prefix,
            'writable': False,
            'search_property': layer.search_property,
            'display_in_layerswitcher': False,
        })

    return wfs, wfs_layer_token