Example #1
0
def test_ST_Buffer():
    data = ["POLYGON((0 0,1 0,1 1,0 0))"]
    data = GeoSeries(data)
    rst = data.buffer(1.2).to_wkt()
    expect = "POLYGON ((-0.848528137423857 0.848528137423857,0.151471862576143 1.84852813742386,0.19704327236937 1.89177379057287,0.244815530740195 1.93257515374836,0.294657697249032 1.97082039324994,0.346433157981967 2.00640468153451,0.4 2.03923048454133,0.455211400312543 2.06920782902604,0.511916028309039 2.09625454917112,0.569958460545639 2.12029651179664,0.629179606750062 2.14126781955418,0.689417145876974 2.15911099154688,0.750505971018688 2.17377712088057,0.812278641951722 2.18522600871417,0.874565844078815 2.19342627444193,0.937196852508467 2.19835544170549,1.0 2.2,1.06280314749153 2.19835544170549,1.12543415592118 2.19342627444193,1.18772135804828 2.18522600871417,1.24949402898131 2.17377712088057,1.31058285412302 2.15911099154688,1.37082039324994 2.14126781955418,1.43004153945436 2.12029651179664,1.48808397169096 2.09625454917112,1.54478859968746 2.06920782902604,1.6 2.03923048454133,1.65356684201803 2.00640468153451,1.70534230275097 1.97082039324994,1.75518446925981 1.93257515374836,1.80295672763063 1.89177379057287,1.84852813742386 1.84852813742386,1.89177379057287 1.80295672763063,1.93257515374837 1.7551844692598,1.97082039324994 1.70534230275097,2.00640468153451 1.65356684201803,2.03923048454133 1.6,2.06920782902604 1.54478859968746,2.09625454917112 1.48808397169096,2.12029651179664 1.43004153945436,2.14126781955418 1.37082039324994,2.15911099154688 1.31058285412302,2.17377712088057 1.24949402898131,2.18522600871417 1.18772135804828,2.19342627444193 1.12543415592118,2.19835544170549 1.06280314749153,2.2 1.0,2.2 0.0,2.19835544170549 -0.062803147491532,2.19342627444193 -0.125434155921184,2.18522600871417 -0.187721358048277,2.17377712088057 -0.249494028981311,2.15911099154688 -0.310582854123025,2.14126781955418 -0.370820393249937,2.12029651179664 -0.43004153945436,2.09625454917112 -0.48808397169096,2.06920782902604 -0.544788599687456,2.03923048454133 -0.6,2.00640468153451 -0.653566842018033,1.97082039324994 -0.705342302750968,1.93257515374836 -0.755184469259805,1.89177379057287 -0.80295672763063,1.84852813742386 -0.848528137423857,1.80295672763063 -0.891773790572873,1.75518446925981 -0.932575153748365,1.70534230275097 -0.970820393249937,1.65356684201803 -1.00640468153451,1.6 -1.03923048454133,1.54478859968746 -1.06920782902604,1.48808397169096 -1.09625454917112,1.43004153945436 -1.12029651179664,1.37082039324994 -1.14126781955418,1.31058285412302 -1.15911099154688,1.24949402898131 -1.17377712088057,1.18772135804828 -1.18522600871417,1.12543415592118 -1.19342627444193,1.06280314749153 -1.19835544170549,1.0 -1.2,0.0 -1.2,-0.062803147491532 -1.19835544170549,-0.125434155921184 -1.19342627444193,-0.187721358048276 -1.18522600871417,-0.24949402898131 -1.17377712088057,-0.310582854123024 -1.15911099154688,-0.370820393249936 -1.14126781955418,-0.430041539454359 -1.12029651179664,-0.488083971690959 -1.09625454917112,-0.544788599687455 -1.06920782902604,-0.6 -1.03923048454133,-0.653566842018031 -1.00640468153451,-0.705342302750966 -0.970820393249938,-0.755184469259804 -0.932575153748366,-0.802956727630628 -0.891773790572875,-0.848528137423855 -0.848528137423859,-0.891773790572871 -0.802956727630632,-0.932575153748363 -0.755184469259807,-0.970820393249935 -0.70534230275097,-1.00640468153451 -0.653566842018035,-1.03923048454132 -0.6,-1.06920782902604 -0.544788599687459,-1.09625454917112 -0.488083971690964,-1.12029651179664 -0.430041539454364,-1.14126781955418 -0.370820393249941,-1.15911099154688 -0.310582854123029,-1.17377712088057 -0.249494028981315,-1.18522600871416 -0.187721358048281,-1.19342627444193 -0.125434155921189,-1.19835544170549 -0.062803147491537,-1.2 -0.0,-1.19835544170549 0.062803147491527,-1.19342627444193 0.125434155921179,-1.18522600871417 0.187721358048272,-1.17377712088057 0.249494028981306,-1.15911099154688 0.310582854123019,-1.14126781955419 0.370820393249931,-1.12029651179664 0.430041539454355,-1.09625454917112 0.488083971690954,-1.06920782902604 0.54478859968745,-1.03923048454133 0.6,-1.00640468153451 0.653566842018027,-0.970820393249941 0.705342302750962,-0.93257515374837 0.755184469259799,-0.891773790572878 0.802956727630624,-0.848528137423857 0.848528137423857))"

    assert rst[0] == expect
Example #2
0
def test_ST_Contains():
    p11 = "POLYGON((0 0,1 0,1 1,0 1,0 0))"
    p12 = "POLYGON((8 0,9 0,9 1,8 1,8 0))"
    p13 = "POINT(2 2)"
    p14 = "POINT(200 2)"
    data1 = GeoSeries([p11, p12, p13, p14])

    p21 = "POLYGON((0 0,0 8,8 8,8 0,0 0))"
    p22 = "POLYGON((0 0,0 8,8 8,8 0,0 0))"
    p23 = "POLYGON((0 0,0 8,8 8,8 0,0 0))"
    p24 = "POLYGON((0 0,0 8,8 8,8 0,0 0))"
    data2 = GeoSeries([p21, p22, p23, p24])
    rst = data2.contains(data1)
    assert len(rst) == 4
    assert rst[0] == 1
    assert rst[1] == 0
    assert rst[2] == 1
    assert rst[3] == 0

    rst = data2.contains(data2[0])
    assert len(rst) == 4
    assert rst[0] == 1
    assert rst[1] == 1
    assert rst[2] == 1
    assert rst[3] == 1
Example #3
0
    def test_from_empty(self):
        s = GeoSeries([], dtype="GeoDtype")
        assert_is_geoseries(s)
        assert len(s) == 0

        s = GeoSeries()
        assert_is_geoseries(s)
        assert len(s) == 0
Example #4
0
def test_ST_Transform():
    data = ["POINT (10 10)"]
    data = GeoSeries(data, crs="EPSG:4326")
    rst = data.to_crs("EPSG:3857").to_wkt()

    wkt = rst[0]
    rst_point = ogr.CreateGeometryFromWkt(str(wkt))
    assert abs(rst_point.GetX() - 1113194.90793274 < 0.01)
    assert abs(rst_point.GetY() - 1118889.97485796 < 0.01)
Example #5
0
def test_to_geopandas():
    p1 = "POLYGON ((0 0,4 0,4 4,0 4,0 0))"
    p2 = None
    data = GeoSeries([p1, p2], crs="EPSG:4326")
    rst = data.to_geopandas()

    import shapely
    assert rst[0] == shapely.geometry.Polygon(((0, 0), (4, 0), (4, 4), (0, 4), (0, 0)))
    assert rst[1] is None
    assert rst.crs.to_string() == "EPSG:4326"
Example #6
0
    def test_equals(self):
        s1 = GeoSeries([make_point(1, 1), None])
        s2 = GeoSeries([make_point(1, 1), None])
        assert s1.equals(s2)

        s1 = GeoSeries()
        s2 = GeoSeries()
        assert s1.equals(s2)
Example #7
0
def test_ST_HausdorffDistance():
    import math
    data1 = ["POLYGON((0 0 ,0 1, 1 1, 1 0, 0 0))", "POINT(0 0)"]
    data2 = ["POLYGON((0 0 ,0 2, 1 1, 1 0, 0 0))", "POINT(0 1)"]
    data1 = GeoSeries(data1)
    data2 = GeoSeries(data2)
    rst = data1.hausdorff_distance(data2)
    assert len(rst) == 2
    assert rst[0] == 1
    assert rst[1] == 1

    rst = data1.hausdorff_distance(data1[1])
    assert len(rst) == 2
    assert math.isclose(rst[0], math.sqrt(2), rel_tol=1e-5)
    assert rst[1] == 0
Example #8
0
def test_ST_Centroid():
    data = ["POLYGON((0 0,1 0,1 1,0 1,0 0))", "POLYGON((0 0,0 8,8 8,8 0,0 0))"]
    data = GeoSeries(data)
    rst = data.centroid.to_wkt()

    assert rst[0] == "POINT (0.5 0.5)"
    assert rst[1] == "POINT (4 4)"
Example #9
0
def test_ST_Length():
    data = ["LINESTRING(0 0,0 1)", "LINESTRING(1 1,1 4)"]
    data = GeoSeries(data)
    rst = data.length

    assert rst[0] == 1.0
    assert rst[1] == 3.0
Example #10
0
 def test_form_scalar_with_index(self):
     index = [1, 2, 3, 4, 5]
     s = GeoSeries('Point (1 2)', index=index)
     assert_is_geoseries(s)
     assert len(s) == 5
     for i in index:
         assert s[i] == s[index[0]]
Example #11
0
def test_ST_Area():
    data = ["POLYGON((0 0,1 0,1 1,0 1,0 0))", "POLYGON((0 0,0 8,8 8,8 0,0 0))"]
    data = GeoSeries(data)
    rst = data.area

    assert rst[0] == 1.0
    assert rst[1] == 64.0
Example #12
0
 def test_from_with_na_data(self):
     s = GeoSeries(['Point (1 2)', None, np.nan])
     assert_is_geoseries(s)
     assert len(s) == 3
     assert s.hasnans
     assert s[1] is None
     assert s[2] is None
Example #13
0
 def test_form_series_with_index(self):
     index = [1, 2, 3, 4, 5]
     s = pd.Series(make_point(1, 1), index=index)
     geo_s = GeoSeries(s)
     assert_is_geoseries(geo_s)
     assert len(geo_s) == 5
     for i in index:
         assert geo_s[i] == geo_s[index[0]]
Example #14
0
    def test_missing_values(self):
        s = GeoSeries([make_point(1, 2), None])
        assert s[1] is None
        assert s.isna().tolist() == [False, True]
        assert s.notna().tolist() == [True, False]
        assert not s.dropna().isna().any()

        s1 = s.fillna(make_point(1, 1))
        s1 = s1.to_wkt()
        assert s1[0] == "POINT (1 2)"
        assert s1[1] == "POINT (1 1)"

        # fillna with method
        s1 = s.fillna(method='ffill')
        assert s1[0] == s1[1]

        s1 = s[::-1].fillna(method="bfill")
        assert s1[0] == s1[1]

        # set item with na value
        s[0] = np.nan
        assert s[0] is None

        s[0] = pd.NA
        assert s[0] is None
Example #15
0
 def test_from_geopandas(self):
     import geopandas as gpd
     from shapely.geometry import Point
     gpd_s = gpd.GeoSeries([Point(1, 1), Point(1, 2), None], index=[1, 2, 3], crs='EPSG:4326')
     s = GeoSeries.from_geopandas(gpd_s)
     assert_is_geoseries(s)
     assert s.crs == "EPSG:4326"
     assert s.to_wkt().to_list() == [make_point(1, 1), make_point(1, 2), None]
     assert s.index.to_list() == [1, 2, 3]
Example #16
0
    def test_operator(self):
        # __eq__
        s = GeoSeries([make_point(0, 0), make_point(1, 2), None])
        r = s == s[0]
        assert r.tolist() == [True, False, False]

        # __ne__
        r = s != s[0]
        assert r.tolist() == [False, True, True]
Example #17
0
def test_geoseries_type_by_df_box_col_values():
    from pandas import DataFrame, Series
    series = GeoSeries(["POINT (0 0)", None, "POINT (0 1)", "POINT (2 0)"])
    df = DataFrame({'s': series})
    assert isinstance(df['s'], type(series))

    series = Series([1, None, 2, 3])
    df = DataFrame({'s': series})
    assert isinstance(df['s'], type(series))
Example #18
0
class TestCRS:
    def setup_method(self):
        self.crs = "epsg:3854"
        self.s = GeoSeries([make_point(x, x) for x in range(5)], crs=self.crs)

    def test_constrctor(self):
        assert self.crs.upper() == self.s.crs

    def test_series_method(self):
        assert self.s.head().crs == self.crs.upper()
        assert self.s[:4].crs == self.crs.upper()
        assert self.s.take([1, 3, 4]).crs == self.crs.upper()
        assert self.s[[True, False, True, True, True]].crs == self.crs.upper()

    # test methods in GeoSeries will produce GeoSeries as result
    def test_geom_method(self):
        assert self.s.buffer(0.2).crs == self.crs.upper()
        assert self.s.intersection(self.s).crs == self.crs.upper()
        assert self.s.centroid.crs == self.crs.upper()
Example #19
0
def test_ST_GeomFromGeoJSON():
    from pandas import Series
    j0 = "{\"type\":\"Point\",\"coordinates\":[1,2]}"
    j1 = "{\"type\":\"LineString\",\"coordinates\":[[1,2],[4,5],[7,8]]}"
    j2 = "{\"type\":\"Polygon\",\"coordinates\":[[[0,0],[0,1],[1,1],[1,0],[0,0]]]}"
    data = Series([j0, j1, j2])
    str_ptr = GeoSeries.geom_from_geojson(data).to_wkt()
    assert str_ptr[0] == "POINT (1 2)"
    assert str_ptr[1] == "LINESTRING (1 2,4 5,7 8)"
    assert str_ptr[2] == "POLYGON ((0 0,0 1,1 1,1 0,0 0))"
Example #20
0
def test_ST_PolygonFromEnvelope():
    from pandas import Series
    x_min = Series([0.0])
    x_max = Series([1.0])
    y_min = Series([0.0])
    y_max = Series([1.0])

    rst = GeoSeries.polygon_from_envelope(x_min, y_min, x_max, y_max).to_wkt()

    assert rst[0] == "POLYGON ((0 0,0 1,1 1,1 0,0 0))"
Example #21
0
def test_ST_AsGeoJSON():
    from pandas import Series
    j0 = "{\"type\":\"Point\",\"coordinates\":[1,2]}"
    j1 = "{\"type\":\"LineString\",\"coordinates\":[[1,2],[4,5],[7,8]]}"
    j2 = "{\"type\":\"Polygon\",\"coordinates\":[[[0,0],[0,1],[1,1],[1,0],[0,0]]]}"
    data = Series([j0, j1, j2])
    str_ptr = GeoSeries.geom_from_geojson(data).as_geojson()
    assert str_ptr[0] == '{ "type": "Point", "coordinates": [ 1.0, 2.0 ] }'
    assert str_ptr[
        1] == '{ "type": "LineString", "coordinates": [ [ 1.0, 2.0 ], [ 4.0, 5.0 ], [ 7.0, 8.0 ] ] }'
    assert str_ptr[
        2] == '{ "type": "Polygon", "coordinates": [ [ [ 0.0, 0.0 ], [ 0.0, 1.0 ], [ 1.0, 1.0 ], [ 1.0, 0.0 ], [ 0.0, 0.0 ] ] ] }'
Example #22
0
def test_geo_method_with_missing_value():
    s1 = GeoSeries([make_point(1, 1), None])
    s2 = GeoSeries([None, make_point(1, 1)])
    s3 = GeoSeries([make_point(1, 1), None])

    assert s1.geom_equals(s3).all()
    assert not s1.geom_equals(s2).any()
Example #23
0
def test_ST_Within():
    p11 = "POLYGON((0 0,1 0,1 1,0 1,0 0))"
    p12 = "POLYGON((8 0,9 0,9 1,8 1,8 0))"
    p13 = "LINESTRING(2 2,3 2)"
    p14 = "POINT(10 2)"
    data1 = GeoSeries([p11, p12, p13, p14])

    p21 = "POLYGON((0 0,0 8,8 8,8 0,0 0))"
    p22 = "POLYGON((0 0,0 8,8 8,8 0,0 0))"
    p23 = "POLYGON((0 0,0 8,8 8,8 0,0 0))"
    p24 = "POLYGON((0 0,0 8,8 8,8 0,0 0))"
    data2 = GeoSeries([p21, p22, p23, p24])

    rst = data2.within(data1)
    assert len(rst) == 4
    assert rst[0] == 0
    assert rst[1] == 0
    assert rst[2] == 0
    assert rst[3] == 0

    rst = data1.within(data2[0])
    assert len(rst) == 4
    assert rst[0] == 1
    assert rst[1] == 0
    assert rst[2] == 1
    assert rst[3] == 0
Example #24
0
def test_ST_Point():
    from pandas import Series
    data1 = [1.3, 2.5]
    data2 = [3.8, 4.9]
    string_ptr = GeoSeries.point(data1, data2).to_wkt()
    assert len(string_ptr) == 2
    assert string_ptr[0] == "POINT (1.3 3.8)"
    assert string_ptr[1] == "POINT (2.5 4.9)"

    string_ptr = GeoSeries.point(Series([1, 2], dtype='double'), 5).to_wkt()
    assert len(string_ptr) == 2
    assert string_ptr[0] == "POINT (1 5)"
    assert string_ptr[1] == "POINT (2 5)"

    string_ptr = GeoSeries.point(5, Series([1, 2], dtype='double')).to_wkt()
    assert len(string_ptr) == 2
    assert string_ptr[0] == "POINT (5 1)"
    assert string_ptr[1] == "POINT (5 2)"

    string_ptr = GeoSeries.point(5.0, 1.0).to_wkt()
    assert len(string_ptr) == 1
    assert string_ptr[0] == "POINT (5 1)"
Example #25
0
class TestType:
    def setup_method(self):
        self.s = GeoSeries([make_point(x, x) for x in range(5)])

    def test_head_tail(self):
        assert_is_geoseries(self.s.head())
        assert_is_geoseries(self.s.tail())

    def test_slice(self):
        assert_is_geoseries(self.s[2::5])
        assert_is_geoseries(self.s[::-1])

    def test_loc_iloc(self):
        assert_is_geoseries(self.s.loc[1:])
        assert_is_geoseries(self.s.iloc[:4])

    def test_take(self):
        assert_is_geoseries(self.s.take([0, 2, 4]))

    def test_geom_method(self):
        assert_is_geoseries(self.s.buffer(0.2))
        assert_is_geoseries(self.s.intersection(self.s))
        assert_is_geoseries(self.s.centroid)
Example #26
0
    def test_geom_with_index(self):
        index = ['a', 'b']

        # unary
        s = GeoSeries([make_point(1, 1), None], index=index)
        s1 = s.length
        assert s1.index.to_list() == index

        # binary with same index
        left = GeoSeries([make_point(1, 1), None], index=index)
        right = GeoSeries([make_point(1, 1), None], index=index)
        s1 = left.geom_equals(right)
        assert s1.index.to_list() == index

        # binary with different index will align index
        left = GeoSeries([make_point(1, 1), None], index=[1, 2])
        right = GeoSeries([make_point(1, 1), None], index=[3, 4])
        s1 = left.geom_equals(right)
        assert s1.index.to_list() == [1, 2, 3, 4]
        assert s1.to_list() == [False, True, False, True]
Example #27
0
def test_ST_Touches():
    data1 = GeoSeries(
        ["POLYGON ((1 1,1 2,2 2,2 1,1 1))", "POLYGON ((1 1,1 2,2 2,2 1,1 1))"])
    data2 = GeoSeries(
        ["POLYGON ((1 1,1 2,2 2,2 1,1 1))", "POLYGON ((2 1,3 1,3 2,2 2,2 1))"])
    rst = data1.touches(data2)
    assert len(rst) == 2
    assert rst[0] == 0
    assert rst[1] == 1

    rst = data2.touches(GeoSeries("POLYGON ((1 1,1 2,2 2,2 1,1 1))")[0])
    assert len(rst) == 2
    assert rst[0] == 0
    assert rst[1] == 1
Example #28
0
def test_ST_Equals():
    data1 = GeoSeries(
        ["POLYGON ((1 1,1 2,2 2,2 1,1 1))", "POLYGON ((1 1,1 2,2 2,2 1,1 1))"])
    data2 = GeoSeries(
        ["POLYGON ((1 1,1 2,2 2,2 1,1 1))", "POLYGON ((2 1,3 1,3 2,2 2,2 1))"])
    rst = data1.geom_equals(data2)
    assert len(rst) == 2
    assert rst[0] == 1
    assert rst[1] == 0

    rst = data2.geom_equals(GeoSeries("POLYGON ((1 1,1 2,2 2,2 1,1 1))")[0])
    assert len(rst) == 2
    assert rst[0] == 1
    assert rst[1] == 0
Example #29
0
def test_ST_Intersection():
    data1 = GeoSeries(["POLYGON ((1 1,1 2,2 2,2 1,1 1))", "POINT (0 1)"])
    data2 = GeoSeries(["POLYGON ((2 1,3 1,3 2,2 2,2 1))", "POINT (0 1)"])
    rst = data1.intersection(data2).to_wkt()
    assert len(rst) == 2
    assert rst[0] == "LINESTRING (2 2,2 1)"
    assert rst[1] == "POINT (0 1)"

    rst = data1.intersection(GeoSeries("POINT (0 1)")[0]).to_wkt()
    assert len(rst) == 2
    assert rst[0] == "GEOMETRYCOLLECTION EMPTY"
    assert rst[1] == "POINT (0 1)"
Example #30
0
def test_ST_Crosses():
    data1 = GeoSeries(
        ["POLYGON ((1 1,1 2,2 2,2 1,1 1))", "POLYGON ((1 1,1 2,2 2,2 1,1 1))"])
    data2 = GeoSeries(
        ["POLYGON ((1 1,1 2,2 2,2 1,1 1))", "POLYGON ((2 1,3 1,3 2,2 2,2 1))"])
    rst = data1.crosses(data2)
    assert len(rst) == 2
    assert rst[0] == 0
    assert rst[1] == 0

    rst = data2.crosses(data2[0])
    assert len(rst) == 2
    assert rst[0] == 0
    assert rst[1] == 0