예제 #1
0
import numpy as np
import pygeos

point_polygon_testdata = (
    pygeos.points(np.arange(6), np.arange(6)),
    pygeos.box(2, 2, 4, 4),
)
point = pygeos.points(2, 3)
line_string = pygeos.linestrings([(0, 0), (1, 0), (1, 1)])
linear_ring = pygeos.linearrings([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])
polygon = pygeos.polygons([(0, 0), (2, 0), (2, 2), (0, 2), (0, 0)])
multi_point = pygeos.multipoints([(0, 0), (1, 2)])
multi_line_string = pygeos.multilinestrings([[(0, 0), (1, 2)]])
multi_polygon = pygeos.multipolygons([
    [(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)],
    [(2.1, 2.1), (2.2, 2.1), (2.2, 2.2), (2.1, 2.2), (2.1, 2.1)],
])
geometry_collection = pygeos.geometrycollections(
    [pygeos.points(51, -1),
     pygeos.linestrings([(52, -1), (49, 2)])])
point_z = pygeos.points(1.0, 1.0, 1.0)
polygon_with_hole = pygeos.Geometry(
    "POLYGON((0 0, 0 10, 10 10, 10 0, 0 0), (2 2, 2 4, 4 4, 4 2, 2 2))")

all_types = (
    point,
    line_string,
    linear_ring,
    polygon,
    multi_point,
    multi_line_string,
예제 #2
0
@pytest.mark.parametrize(
    "geometry,expected",
    [
        # points do not intersect
        (pygeos.points(0.5, 0.5), []),
        # points intersect
        (pygeos.points(1, 1), [1]),
        # box contains points
        (box(0, 0, 1, 1), [0, 1]),
        # box contains points
        (box(5, 5, 15, 15), [5, 6, 7, 8, 9]),
        # envelope of buffer contains points
        (pygeos.buffer(pygeos.points(3, 3), 1), [2, 3, 4]),
        # envelope of points contains points
        (pygeos.multipoints([[5, 7], [7, 5]]), [5, 6, 7]),
    ],
)
def test_query_points(tree, geometry, expected):
    assert_array_equal(tree.query(geometry), expected)


@pytest.mark.parametrize(
    "geometry,expected",
    [
        # point intersects first line
        (pygeos.points(0, 0), [0]),
        (pygeos.points(0.5, 0.5), [0]),
        # point within envelope of first line
        (pygeos.points(0, 0.5), [0]),
        # point at shared vertex between 2 lines
예제 #3
0
def test_multipoints():
    actual = pygeos.multipoints([point], indices=[0])
    assert_geometries_equal(actual, pygeos.multipoints([point]))
예제 #4
0
def test_create_collection_only_none():
    actual = pygeos.multipoints(np.array([None], dtype=object))
    assert actual.to_wkt() == "MULTIPOINT EMPTY"
예제 #5
0
def test_create_collection_skips_none():
    actual = pygeos.multipoints([point, None, None, point])
    assert actual.to_wkt() == "MULTIPOINT (2 3, 2 3)"
예제 #6
0
def test_repr_multipoint_with_point_empty():
    # Test if segfault is prevented
    geom = pygeos.multipoints([point, empty_point])
    assert repr(geom) == "<pygeos.Geometry Exception in WKT writer>"
예제 #7
0
def create_network_OD_points(country_network):
    """Create a list of OD points for the specified country 

    Args:
        country ([type]): [description]

    Returns:
        [type]: [description]
    """    
    
    # set paths to data

    #data_path = Path(r'/scistor/ivm/data_catalogue/open_street_map/')
    data_path = Path(r'C:/data/')

    world_pop = data_path.joinpath('worldpop','ppp_2018_1km_Aggregated.tif')

    if data_path.joinpath('network_OD_points','{}.csv'.format(country_network)).is_file():
        return None

        #print("{} already finished!".format(country_network))           
    try: 
        #get country ID
        print('{} started!'.format(country_network))
        
        # load data
        nodes = feather.read_dataframe(data_path.joinpath("percolation_networks","{}-nodes.feather".format(country_network)))
        nodes.geometry = pygeos.from_wkb(nodes.geometry)

        #create dataframe of country row
        geometry = pygeos.convex_hull(pygeos.multipoints(nodes.geometry.values))
        df = pd.DataFrame([geometry],columns=['geometry'])

        #specify height of cell in the grid and create grid of bbox
        
        def create_final_od_grid(df,height_div):
            height = numpy.sqrt(pygeos.area(df.geometry)/height_div).values[0]
            grid = pd.DataFrame(create_grid(create_bbox(df),height),columns=['geometry'])
            
            #clip grid of bbox to grid of the actual spatial exterior of the country
            clip_grid = pygeos.intersection(grid,df.geometry)
            clip_grid = clip_grid.loc[~pygeos.is_empty(clip_grid.geometry)]

            # turn to shapely geometries again for zonal stats
            clip_grid.geometry = pygeos.to_wkb(clip_grid.geometry)
            clip_grid.geometry = clip_grid.geometry.apply(loads)
            clip_grid = gpd.GeoDataFrame(clip_grid)

            # get total population per grid cell

            if height < 0.01:
                clip_grid['tot_pop'] = clip_grid.geometry.apply(lambda x: point_query(x,world_pop))
                clip_grid['tot_pop'] = clip_grid['tot_pop'].apply(lambda x: np.sum(x))    
            else:
                clip_grid['tot_pop'] = clip_grid.geometry.apply(lambda x: zonal_stats(x,world_pop,stats="sum"))
                clip_grid['tot_pop'] = clip_grid['tot_pop'].apply(lambda x: x[0]['sum'])               

            # remove cells in the grid that have no population data
            clip_grid = clip_grid.loc[~pd.isna(clip_grid.tot_pop)]
            
            if len(clip_grid) > 100:
                clip_grid = clip_grid.loc[clip_grid.tot_pop > 100]
            clip_grid.reset_index(inplace=True,drop=True)
            clip_grid.geometry = clip_grid.geometry.centroid
            clip_grid['GID_0'] = country_network[:3]
            clip_grid['grid_height'] = height
            #print(len(clip_grid),height)
            return clip_grid
        
        length_clip = 0
        height_div = 200
        save_lengths = []
        while length_clip < 150:
            clip_grid = create_final_od_grid(df,height_div)
            length_clip = len(clip_grid)
            save_lengths.append(length_clip)
            height_div += 50

            if (len(save_lengths) == 6) & (numpy.mean(save_lengths[3:]) < 150):
                break
                
        print('{} finished with {} points!'.format(country_network,len(clip_grid)))
        
        clip_grid.to_csv(data_path.joinpath('network_OD_points','{}.csv'.format(country_network)))
    except:
        None
예제 #8
0
    assert pygeos.to_wkb(actual, hex=True, byte_order=1) == wkb
    assert pygeos.to_wkb(actual, hex=True, include_srid=True,
                         byte_order=1) == ewkb

    point = pygeos.points(1, 1)
    point_with_srid = pygeos.set_srid(point, np.int32(4326))
    result = pygeos.to_wkb(point_with_srid, include_srid=True, byte_order=1)
    assert np.frombuffer(result[5:9], "<u4").item() == 4326


@pytest.mark.skipif(pygeos.geos_version >= (3, 8, 0),
                    reason="Pre GEOS 3.8.0 has 3D empty points")
@pytest.mark.parametrize("geom,dims,expected", [
    (empty_point, 2, POINT_NAN_WKB),
    (empty_point, 3, POINTZ_NAN_WKB),
    (pygeos.multipoints([empty_point]), 2, MULTIPOINT_NAN_WKB),
    (pygeos.multipoints([empty_point]), 3, MULTIPOINTZ_NAN_WKB),
    (pygeos.geometrycollections([empty_point]), 2, GEOMETRYCOLLECTION_NAN_WKB),
    (pygeos.geometrycollections([empty_point
                                 ]), 3, GEOMETRYCOLLECTIONZ_NAN_WKB),
    (pygeos.geometrycollections([pygeos.multipoints([empty_point])
                                 ]), 2, NESTED_COLLECTION_NAN_WKB),
    (pygeos.geometrycollections([pygeos.multipoints([empty_point])
                                 ]), 3, NESTED_COLLECTIONZ_NAN_WKB),
])
def test_to_wkb_point_empty_pre_geos38(geom, dims, expected):
    actual = pygeos.to_wkb(geom, output_dimension=dims, byte_order=1)
    # Use numpy.isnan; there are many byte representations for NaN
    assert actual[:-dims * 8] == expected[:-dims * 8]
    assert np.isnan(struct.unpack("<{}d".format(dims),
                                  actual[-dims * 8:])).all()
예제 #9
0
def test_to_wkt_multipoint_with_point_empty_errors():
    # Test if segfault is prevented
    geom = pygeos.multipoints([empty_point, point])
    with pytest.raises(ValueError):
        pygeos.to_wkt(geom)
예제 #10
0
def test_create_collection_only_nan():
    actual = pygeos.multipoints(np.array([np.nan], dtype=object))
    assert actual.to_wkt() == "GEOMETRYCOLLECTION EMPTY"
예제 #11
0
def test_create_collection_skips_nan():
    actual = pygeos.multipoints([point, np.nan, np.nan, point])
    assert actual.to_wkt() == "MULTIPOINT (2 2, 2 2)"
예제 #12
0
    tree = pygeos.STRtree(arr)
    # Dereference geometries
    arr[:] = None
    # Still it does not lead to a segfault
    tree.query(point)


def test_query_no_geom(tree):
    with pytest.raises(TypeError):
        tree.query("I am not a geometry")


def test_query_none(tree):
    assert tree.query(None).size == 0


def test_query_empty(tree):
    assert tree.query(empty).size == 0


@pytest.mark.parametrize(
    "envelope,expected",
    [
        (pygeos.points(1, 1), [1]),
        (box(0, 0, 1, 1), [0, 1]),
        (box(5, 5, 15, 15), [5, 6, 7, 8, 9]),
        (pygeos.multipoints([[5, 7], [7, 5]]), [5, 6, 7]),  # query by envelope
    ])
def test_query(tree, envelope, expected):
    assert_array_equal(tree.query(envelope), expected)
예제 #13
0
import numpy as np
import pygeos

point_polygon_testdata = (
    pygeos.points(np.arange(6), np.arange(6)),
    pygeos.box(2, 2, 4, 4),
)
point = pygeos.points(2, 2)
line_string = pygeos.linestrings([[0, 0], [1, 0], [1, 1]])
linear_ring = pygeos.linearrings(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
polygon = pygeos.polygons(
    ((0.0, 0.0), (0.0, 2.0), (2.0, 2.0), (2.0, 0.0), (0.0, 0.0)))
multi_point = pygeos.multipoints([[0.0, 0.0], [1.0, 2.0]])
multi_line_string = pygeos.multilinestrings([[[0.0, 0.0], [1.0, 2.0]]])
multi_polygon = pygeos.multipolygons([
    ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
    ((0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.2, 0.1)),
])
geometry_collection = pygeos.geometrycollections(
    [pygeos.points(51, -1),
     pygeos.linestrings([(52, -1), (49, 2)])])
point_z = pygeos.points(1.0, 1.0, 1.0)

all_types = (
    point,
    line_string,
    linear_ring,
    polygon,
    multi_point,
    multi_line_string,
    multi_polygon,
예제 #14
0
def create_voronoi(
        points: Sequence[pygeos.Geometry]) -> Sequence[pygeos.Geometry]:
    mp = pygeos.multipoints(points)
    polys = pygeos.get_parts(pygeos.voronoi_polygons(mp))
    convex_hull = pygeos.buffer(pygeos.convex_hull(mp), 2)
    return pygeos.intersection(convex_hull, polys)
예제 #15
0
def test_multipoints():
    actual = pygeos.multipoints(
        np.array([point], dtype=object), indices=np.zeros(1, dtype=np.intp)
    )
    assert_geometries_equal(actual, pygeos.multipoints([point]))
예제 #16
0
    assert pygeos.to_wkt(actual, trim=True) == "POINT (0 0)"

    assert pygeos.to_wkb(actual, hex=True) == wkb
    assert pygeos.to_wkb(actual, hex=True, include_srid=True) == ewkb

    point = pygeos.points(1, 1)
    point_with_srid = pygeos.set_srid(point, np.int32(4326))
    result = pygeos.to_wkb(point_with_srid, include_srid=True)
    assert np.frombuffer(result[5:9], "<u4").item() == 4326


@pytest.mark.skipif(pygeos.geos_version >= (3, 8, 0), reason="Pre GEOS 3.8.0 has 3D empty points")
@pytest.mark.parametrize("geom,dims,expected", [
    (empty_point, 2, POINT_NAN_WKB),
    (empty_point, 3, POINTZ_NAN_WKB),
    (pygeos.multipoints([empty_point]), 2, MULTIPOINT_NAN_WKB),
    (pygeos.multipoints([empty_point]), 3, MULTIPOINTZ_NAN_WKB),
    (pygeos.geometrycollections([empty_point]), 2, GEOMETRYCOLLECTION_NAN_WKB),
    (pygeos.geometrycollections([empty_point]), 3, GEOMETRYCOLLECTIONZ_NAN_WKB),
    (pygeos.geometrycollections([pygeos.multipoints([empty_point])]), 2, NESTED_COLLECTION_NAN_WKB),
    (pygeos.geometrycollections([pygeos.multipoints([empty_point])]), 3, NESTED_COLLECTIONZ_NAN_WKB),
])
def test_to_wkb_point_empty_pre_geos38(geom,dims,expected):
    actual = pygeos.to_wkb(geom, output_dimension=dims)
    # Use numpy.isnan; there are many byte representations for NaN
    assert actual[:-dims * 8] == expected[:-dims * 8]
    assert np.isnan(struct.unpack("<{}d".format(dims), actual[-dims * 8:])).all()


@pytest.mark.skipif(pygeos.geos_version < (3, 8, 0), reason="Post GEOS 3.8.0 has 2D empty points")
@pytest.mark.parametrize("geom,dims,expected", [