def create_feature(sync_job, task_metadata, task_status):
    """
    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
        getcrs_cmd = [
            "psql",
            "-w",
            "-h",
            GEOSERVER_PGSQL_HOST,
            "-p",
            GEOSERVER_PGSQL_PORT,
            "-d",
            GEOSERVER_PGSQL_DATABASE,
            "-U",
            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=env)
        getcrs_output = getcrs.communicate()
        if not getcrs_output[0]:
            crs = 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)
            logger.info(message)
        else:
            srid = getcrs_output[0].decode("utf-8").strip()
            crs = "EPSG:{}".format(srid)
            if len(srid) == 6 and srid.startswith("90"):
                crs = 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)
            else:
                message = "Found CRS for {}.{}: {}".format(sync_job["schema"], sync_job["name"], crs)
                logger.info(message)
                task_status.set_message("message", message)
    gs.publish_featuretype(
        sync_job["name"],
        get_datastore(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),
    )
    name = task_feature_name(sync_job)
    l_gs = gs.get_layer(name)
    if not l_gs:
        raise Exception("Layer({0}) not registering.".format(name))
Пример #2
0
def delete_feature(sync_job, task_metadata, task_status):
    l_gs = gs.get_layer("{}:{}".format(sync_job["workspace"], sync_job["name"]))
    if l_gs:
        gs.delete(l_gs)
Пример #3
0
def get_feature(sync_job):
    l_gs = gs.get_layer(task_feature_name(sync_job))
    if not l_gs:
        raise Exception("Feature ({0}) does not exist".format(task_feature_name(sync_job)))

    return l_gs
def delete_feature(sync_job, task_metadata, task_status):
    feature_name = "{}:{}".format(sync_job["workspace"], sync_job["name"])
    l_gs = gs.get_layer(feature_name)
    styles = {}
    feature_exist = False
    # try to find associated feature's private styles
    if l_gs:
        # delete alternate styles
        feature_exist = True
        for s_gs in l_gs.styles or {}:
            if s_gs.name.startswith(sync_job["name"]):
                # the alternate style is only used by this feature, save it for delete.
                styles[s_gs.name] = s_gs

        # try to delete default style
        if l_gs.default_style and l_gs.default_style.name.startswith(sync_job["name"]):
            # has default style and default style is only used by this feature, save it for delete it.
            styles[l_gs.default_style.name] = l_gs.default_style

    # try to find feature's private styles but failed to attach to the feature
    for name, style in sync_job["styles"].iteritems():
        style_name = geoserver_style_name(sync_job, name)
        if style_name in styles:
            continue
        s_gs = gs.get_style(name=style_name, workspace=sync_job["workspace"])
        if s_gs:
            styles[style_name] = s_gs

    # delete the feature
    if l_gs:
        # delete layer
        gs.delete(l_gs)
        # delete feature
        url = (
            GEOSERVER_REST_URL
            + "workspaces/"
            + sync_job["workspace"]
            + "/datastores/"
            + store_name(sync_job)
            + "/featuretypes/"
            + sync_job["name"]
            + ".xml"
        )
        gs.delete(Feature(l_gs, url))

    # delete the styles
    for style in styles.itervalues():
        gs.delete(style)

    if feature_exist:
        if styles:
            task_status.set_message(
                "message",
                os.linesep.join(
                    [
                        "Succeed to delete feature ({})".format(feature_name),
                        "Succeed to delete private styles ({}).".format(
                            ", ".join([name for name in styles.iterkeys()])
                        ),
                    ]
                ),
            )
        else:
            task_status.set_message("message", "Succeed to delete feature ({}).".format(feature_name))
    else:
        if styles:
            task_status.set_message(
                "message",
                os.linesep.join(
                    [
                        "Feature ({}) doesn't exist.".format(feature_name),
                        "Succeed to delete private styles ({}).".format(
                            ", ".join([name for name in styles.iterkeys()])
                        ),
                    ]
                ),
            )
        else:
            task_status.set_message("message", "Feature ({}) doesn't exist.".format(feature_name))