Esempio n. 1
0
def data():
    """Length-100 array for this type.
        * data[0] and data[1] should both be non missing
        * data[0] and data[1] should not gbe equal
        """
    return PointArray([[0, 1], [1, 2], [3, 4], None, [-1, -2]] * 20,
                      dtype='float64')
Esempio n. 2
0
def data_for_grouping():
    """Data for factorization, grouping, and unique tests.
    Expected to be like [B, B, NA, NA, A, A, B, C]
    Where A < B < C and NA is missing
    """
    return PointArray([[1, 0], [1, 0], None, None, [0, 0], [0, 0], [1, 0],
                       [2, 0]])
def test_construct_pointarray_interleaved():
    src_array = np.array([0, 1, 2, 3, 4, 5, 6, 7], dtype=np.float32)
    points = PointArray(src_array)

    np.testing.assert_array_equal(points.x, src_array[0::2])
    np.testing.assert_array_equal(points.y, src_array[1::2])
    np.testing.assert_array_equal(points.isna(), np.isnan(src_array[0::2]))
    np.testing.assert_array_equal(points.flat_values, src_array)
def test_construct_pointarray_tuple():
    src_array = np.array([[0, 1], [2, 3], [4, 5], [6, 7]], dtype=np.float32)
    points = PointArray((src_array[:, 0], src_array[:, 1]))

    np.testing.assert_array_equal(points.x, src_array[:, 0])
    np.testing.assert_array_equal(points.y, src_array[:, 1])
    np.testing.assert_array_equal(points.isna(), np.isnan(src_array[:, 0]))
    np.testing.assert_array_equal(points.flat_values, src_array.flatten())
Esempio n. 5
0
def test_construct_pointarray_2d_with_None():
    src_array = np.array([None, [2, 3], [4, 5], None], dtype=object)
    expected_xs = np.array([np.nan, 2, 4, np.nan], dtype=np.float64)
    expected_ys = np.array([np.nan, 3, 5, np.nan], dtype=np.float64)

    points = PointArray(src_array)

    np.testing.assert_array_equal(points.x, expected_xs)
    np.testing.assert_array_equal(points.y, expected_ys)
    np.testing.assert_array_equal(points.isna(), np.isnan(expected_xs))
    np.testing.assert_array_equal(points.flat_values[2:6],
                                  np.array([2, 3, 4, 5], dtype=np.int64))
Esempio n. 6
0
def spatial_select_columnar(xvals, yvals, geometry):
    try:
        from spatialpandas.geometry import Polygon, PointArray
        points = PointArray((xvals.astype('float'), yvals.astype('float')))
        poly = Polygon([np.concatenate([geometry, geometry[:1]]).flatten()])
        return points.intersects(poly)
    except Exception:
        pass
    try:
        from shapely.geometry import Point, Polygon
        points = (Point(x, y) for x, y in zip(xvals, yvals))
        poly = Polygon(geometry)
        return np.array([poly.contains(p) for p in points])
    except ImportError:
        raise ImportError("Lasso selection on tabular data requires "
                          "either spatialpandas or shapely to be available.")
Esempio n. 7
0
def spatial_select_columnar(xvals, yvals, geometry):
    if 'cudf' in sys.modules:
        import cudf
        if isinstance(xvals, cudf.Series):
            xvals = xvals.values.astype('float')
            yvals = yvals.values.astype('float')
            try:
                import cuspatial
                result = cuspatial.point_in_polygon(
                    xvals,
                    yvals,
                    cudf.Series([0], index=["selection"]),
                    [0],
                    geometry[:, 0],
                    geometry[:, 1],
                )
                return result.values
            except Exception:
                xvals = np.asarray(xvals)
                yvals = np.asarray(yvals)
    x0, x1 = geometry[:, 0].min(), geometry[:, 0].max()
    y0, y1 = geometry[:, 1].min(), geometry[:, 1].max()
    mask = (xvals >= x0) & (xvals <= x1) & (yvals >= y0) & (yvals <= y1)
    masked_xvals = xvals[mask]
    masked_yvals = yvals[mask]
    try:
        from spatialpandas.geometry import Polygon, PointArray
        points = PointArray(
            (masked_xvals.astype('float'), masked_yvals.astype('float')))
        poly = Polygon([np.concatenate([geometry, geometry[:1]]).flatten()])
        geom_mask = points.intersects(poly)
    except Exception:
        pass
    try:
        from shapely.geometry import Point, Polygon
        points = (Point(x, y) for x, y in zip(masked_xvals, masked_yvals))
        poly = Polygon(geometry)
        geom_mask = np.array([poly.contains(p) for p in points])
    except ImportError:
        raise ImportError("Lasso selection on tabular data requires "
                          "either spatialpandas or shapely to be available.")
    mask[np.where(mask)[0]] = geom_mask
    return mask
Esempio n. 8
0
def data_missing_for_sorting():
    """Length-3 array with a known sort order.
    This should be three items [B, NA, A] with
    A < B and NA missing.
    """
    return PointArray([[1, 0], None, [0, 0]])
Esempio n. 9
0
def data_for_sorting():
    """Length-3 array with a known sort order.
    This should be three items [B, C, A] with
    A < B < C
    """
    return PointArray([[1, 0], [2, 0], [0, 0]])
Esempio n. 10
0
def data_missing():
    """Length-2 array with [NA, Valid]"""
    return PointArray([None, [-1, 0]], dtype='int64')
Esempio n. 11
0
def test_equality():
    a = PointArray([[0, 1], [1, 2], None, [-1, -2], [7, 3]], dtype='float64')
    assert all(a == a)
    assert all(a[1:-1] == a[[1, 2, 3]])
    assert not any(a[1:-1] == a[[2, 3, 1]])
Esempio n. 12
0
def test_point_array():
    points = PointArray(unit_square_cw)
    np.testing.assert_equal(points.length, [0.0, 0.0, 0.0, 0.0, 0.0])
    np.testing.assert_equal(points.area, [0.0, 0.0, 0.0, 0.0, 0.0])
    assert points.total_bounds == (1, 1, 2, 2)
Esempio n. 13
0
def geohashes_to_geoseries(s: pd.Series) -> GeoSeries:
    """Create spatialpandas GeoSeries from geohashes."""
    s = pd.Series(s)
    return GeoSeries(PointArray(s.apply(geohash_decode_xy)), index=s.index)