Example #1
0
    def testVirtualTables(self):
        ds = self.cat.create_datastore("gsconfig_import_test2")
        ds.connection_parameters.update(**DBPARAMS)
        self.cat.save(ds)
        ds = self.cat.get_store("gsconfig_import_test2")
        self.cat.add_data_to_store(
            ds, "import2", {
                'shp': 'test/data/states.shp',
                'shx': 'test/data/states.shx',
                'dbf': 'test/data/states.dbf',
                'prj': 'test/data/states.prj'
            })
        store = self.cat.get_store("gsconfig_import_test2")
        geom = JDBCVirtualTableGeometry('the_geom', 'MultiPolygon', '4326')
        ft_name = 'my_jdbc_vt_test'
        epsg_code = 'EPSG:4326'
        sql = "select * from import2 where 'STATE_NAME' = 'Illinois'"
        keyColumn = None
        parameters = None

        jdbc_vt = JDBCVirtualTable(ft_name, sql, 'false', geom, keyColumn,
                                   parameters)
        self.cat.publish_featuretype(ft_name,
                                     store,
                                     epsg_code,
                                     jdbc_virtual_table=jdbc_vt)
Example #2
0
def createLayer():
    cat = Catalog('http://localhost:8081/geoserver/rest/', 'admin',
                  'geoserver')
    store = cat.get_store('test')
    geom = JDBCVirtualTableGeometry('newgeom', 'LineString', '4326')
    ft_name = 'my_jdbc_vt_test'
    epsg_code = 'EPSG:4326'
    sql = 'select ST_MakeLine(wkb_geometry ORDER BY waypoint) As newgeom, assetid, runtime from waypoints group by assetid,runtime'
    keyColumn = None
    parameters = None

    jdbc_vt = JDBCVirtualTable(ft_name, sql, 'false', geom, keyColumn,
                               parameters)
    ft = cat.publish_featuretype(ft_name,
                                 store,
                                 epsg_code,
                                 jdbc_virtual_table=jdbc_vt)
def _create_feature(sync_job, task_metadata, task_status, gs, stage=None):
    """
    This is not a critical task. 
    """
    crs = sync_job.get("crs", None)
    if not crs and "datasource" not in sync_job:
        # try and fetch the layer's CRS from PostGIS
        if "spatial_column" in sync_job:
            getcrs_cmd = [
                "psql", "-w", "-h", settings.GEOSERVER_PGSQL_HOST, "-p",
                settings.GEOSERVER_PGSQL_PORT, "-d",
                settings.GEOSERVER_PGSQL_DATABASE, "-U",
                settings.GEOSERVER_PGSQL_USERNAME, "-A", "-t", "-c",
                "SELECT srid FROM public.geometry_columns WHERE f_table_schema='{0}' AND f_table_name='{1}' AND f_geometry_column='{2}';"
                .format(sync_job["schema"], sync_job["name"],
                        sync_job["spatial_column"])
            ]
        else:
            getcrs_cmd = [
                "psql", "-w", "-h", settings.GEOSERVER_PGSQL_HOST, "-p",
                settings.GEOSERVER_PGSQL_PORT, "-d",
                settings.GEOSERVER_PGSQL_DATABASE, "-U",
                settings.GEOSERVER_PGSQL_USERNAME, "-A", "-t", "-c",
                "SELECT public.ST_SRID(wkb_geometry) FROM {}.{} LIMIT 1;".
                format(sync_job["schema"], sync_job["name"])
            ]
        getcrs = subprocess.Popen(getcrs_cmd,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE,
                                  env=settings.env)
        getcrs_output = getcrs.communicate()
        if not getcrs_output[0]:
            crs = settings.GEOSERVER_DEFAULT_CRS
            message = 'No CRS found for {}.{}, using default of {}'.format(
                sync_job["schema"], sync_job["name"], crs)
            task_status.set_message("message", message, stage=stage)
            logger.info(message)
        else:
            srid = getcrs_output[0].decode('utf-8').strip()
            if len(srid) == 6 and srid.startswith('90'):
                crs = settings.GEOSERVER_DEFAULT_CRS
                message = 'Layer {}.{} has the non-standard SRID {}! Check the Borg Collector definition for this input and force a standard CRS if necessary. For now, the layer will be published with default CRS {}'.format(
                    sync_job["schema"], sync_job["name"], srid, crs)
                logger.warn(message)
                task_status.set_message("message", message, stage=stage)
            else:
                crs = 'EPSG:{}'.format(srid)
                message = 'Found CRS for {}.{}: {}'.format(
                    sync_job["schema"], sync_job["name"], crs)
                logger.info(message)
                task_status.set_message("message", message, stage=stage)

    bbox = None
    if (sync_job.get('override_bbox', False)):
        bbox = sync_job["bbox"]
        bbox = (repr(bbox[0]), repr(bbox[2]), repr(bbox[1]), repr(bbox[3]),
                crs)

    if sync_job.get('viewsql'):
        gs.publish_featuretype(
            sync_job['name'],
            get_datastore(gs, sync_job),
            crs,
            keywords=(sync_job.get('keywords', None) or []) +
            (sync_job.get('applications', None) or []),
            title=sync_job.get('title', None),
            abstract=sync_job.get('abstract', None),
            jdbc_virtual_table=JDBCVirtualTable(
                sync_job['name'], sync_job.get('viewsql'), 'false',
                JDBCVirtualTableGeometry(sync_job["spatial_column"],
                                         sync_job["spatial_type"], crs[5:])),
            nativeBoundingBox=bbox,
            latLonBoundingBox=bbox)
    else:
        gs.publish_featuretype(
            sync_job['name'],
            get_datastore(gs, sync_job),
            crs,
            keywords=(sync_job.get('keywords', None) or []) +
            (sync_job.get('applications', None) or []),
            title=sync_job.get('title', None),
            abstract=sync_job.get('abstract', None),
            nativeName=sync_job.get('table', None),
            nativeBoundingBox=bbox,
            latLonBoundingBox=bbox)

    name = task_feature_name(sync_job)
    l_gs = gs.get_layer(name)
    if not l_gs:
        raise Exception("Layer({0}) not registering.".format(name))