Ejemplo n.º 1
0
Archivo: io.py Proyecto: wsf1990/pygeos
def from_shapely(geometry, **kwargs):
    """Creates geometries from shapely Geometry objects.

    This function requires the GEOS version of PyGEOS and shapely to be equal.

    Parameters
    ----------
    geometry : shapely Geometry object or array_like

    Examples
    --------
    >>> from shapely.geometry import Point   # doctest: +SKIP
    >>> from_shapely(Point(1, 2))   # doctest: +SKIP
    <pygeos.Geometry POINT (1 2)>
    """
    if shapely_geos_version is None:
        raise ImportError("This function requires shapely")

    # shapely has something like: "3.6.2-CAPI-1.10.2 4d2925d6"
    # pygeos has something like: "3.6.2-CAPI-1.10.2"
    if not shapely_geos_version.startswith(geos_capi_version_string):
        raise ImportError(
            "The shapely GEOS version ({}) is incompatible with the GEOS "
            "version PyGEOS was compiled with ({})".format(
                shapely_geos_version, geos_capi_version_string))

    if isinstance(geometry, ShapelyGeometry):
        # this so that the __array_interface__ of the shapely geometry is not
        # used, converting the Geometry to its coordinates
        arr = np.empty(1, dtype=object)
        arr[0] = geometry
        arr.shape = ()
    else:
        arr = np.asarray(geometry, dtype=object)
    return lib.from_shapely(arr, **kwargs)
Ejemplo n.º 2
0
def set_use_pygeos(val=None):
    """
    Set the global configuration on whether to use PyGEOS or not.

    The default is use PyGEOS if it is installed. This can be overridden
    with an environment variable USE_PYGEOS (this is only checked at
    first import, cannot be changed during interactive session).

    Alternatively, pass a value here to force a True/False value.
    """
    global USE_PYGEOS
    global PYGEOS_SHAPELY_COMPAT

    if val is not None:
        USE_PYGEOS = bool(val)
    else:
        if USE_PYGEOS is None:

            USE_PYGEOS = HAS_PYGEOS

            env_use_pygeos = os.getenv("USE_PYGEOS", None)
            if env_use_pygeos is not None:
                USE_PYGEOS = bool(int(env_use_pygeos))

    # validate the pygeos version
    if USE_PYGEOS:
        try:
            import pygeos  # noqa

            # validate the pygeos version
            if not str(pygeos.__version__) >= LooseVersion("0.8"):
                raise ImportError(
                    "PyGEOS >= 0.8 is required, version {0} is installed".format(
                        pygeos.__version__
                    )
                )

            # Check whether Shapely and PyGEOS use the same GEOS version.
            # Based on PyGEOS from_shapely implementation.

            from shapely.geos import geos_version_string as shapely_geos_version
            from pygeos import geos_capi_version_string

            # shapely has something like: "3.6.2-CAPI-1.10.2 4d2925d6"
            # pygeos has something like: "3.6.2-CAPI-1.10.2"
            if not shapely_geos_version.startswith(geos_capi_version_string):
                warnings.warn(
                    "The Shapely GEOS version ({}) is incompatible with the GEOS "
                    "version PyGEOS was compiled with ({}). Conversions between both "
                    "will be slow.".format(
                        shapely_geos_version, geos_capi_version_string
                    )
                )
                PYGEOS_SHAPELY_COMPAT = False
            else:
                PYGEOS_SHAPELY_COMPAT = True

        except ImportError:
            raise ImportError(INSTALL_PYGEOS_ERROR)
Ejemplo n.º 3
0
def check_shapely_version():
    """
    This function will try to import shapely and extracts some necessary classes and functions from the package.
    It also looks if Shapely and PyGEOS use the same GEOS version, as this means the conversion can be faster.

    This function sets a few global variables:

    - ShapelyGeometry:
        shapely.geometry.base.BaseGeometry
    - ShapelyPreparedGeometry:
        shapely.prepared.PreparedGeometry
    - shapely_lgeos:
        shapely.geos.lgeos
    - shapely_geom_factory:
        shapely.geometry.base.geom_factory
    - shapely_wkb_loads:
        shapely.wkb.loads
    - shapely_compatible:
        ``None`` if shapely is not installed,
        ``True`` if shapely and PyGEOS use the same GEOS version,
        ``False`` otherwise
    - _shapely_checked:
        Mostly internal variable to mark that we already tried to import shapely
    """
    global ShapelyGeometry
    global ShapelyPreparedGeometry
    global shapely_lgeos
    global shapely_geom_factory
    global shapely_wkb_loads
    global shapely_compatible
    global _shapely_checked

    if not _shapely_checked:
        try:
            from shapely.geometry.base import BaseGeometry as ShapelyGeometry
            from shapely.geometry.base import geom_factory as shapely_geom_factory
            from shapely.geos import geos_version_string
            from shapely.geos import lgeos as shapely_lgeos
            from shapely.prepared import PreparedGeometry as ShapelyPreparedGeometry
            from shapely.wkb import loads as shapely_wkb_loads

            # shapely has something like: "3.6.2-CAPI-1.10.2 4d2925d6"
            # pygeos has something like: "3.6.2-CAPI-1.10.2"
            shapely_compatible = True
            if not geos_version_string.startswith(geos_capi_version_string):
                shapely_compatible = False
                warnings.warn(
                    "The shapely GEOS version ({}) is incompatible "
                    "with the PyGEOS GEOS version ({}). "
                    "Conversions between both will be slow".format(
                        geos_version_string, geos_capi_version_string
                    )
                )
        except ImportError:
            pass

        _shapely_checked = True
Ejemplo n.º 4
0
from shapely.geos import geos_version_string as shapely_geos_version
from pygeos import geos_capi_version_string
import warnings

# shapely has something like: "3.6.2-CAPI-1.10.2 4d2925d6"
# pygeos has something like: "3.6.2-CAPI-1.10.2"
if not shapely_geos_version.startswith(geos_capi_version_string):
    warnings.warn(
        "The Shapely GEOS version ({}) is incompatible with the GEOS "
        "version PyGEOS was compiled with ({}). The tool will work "
        "but it runs a bit slower.".format(shapely_geos_version,
                                           geos_capi_version_string))
    PYGEOS_SHAPELY_COMPAT = False
else:
    PYGEOS_SHAPELY_COMPAT = True