示例#1
0
def test_bounds2raster():
    w, s, e, n = (-106.6495132446289, 25.845197677612305, -93.50721740722656,
                  36.49387741088867)
    _ = ctx.bounds2raster(w, s, e, n, 'test.tif', zoom=4, ll=True)
    rtr = rio.open('test.tif')
    img = np.array([band for band in rtr.read()]).transpose(1, 2, 0)
    solu = (-12528334.684053527, 2509580.5126589066, -10023646.141204873,
            5014269.05550756)
    for i, j in zip(rtr.bounds, solu):
        assert round(i - j, TOL) == 0
    assert img[100, 100, :].tolist() == [230, 229, 188]
    assert img[100, 200, :].tolist() == [156, 180, 131]
    assert img[200, 100, :].tolist() == [230, 225, 189]
    assert img.sum() == 36926856
    assert_array_almost_equal(img.mean(), 187.8197021484375)

    # multiple tiles for which result is not square
    w, s, e, n = (2.5135730322461427, 49.529483547557504, 6.15665815595878,
                  51.47502370869813)
    img, ext = ctx.bounds2raster(w, s, e, n, 'test2.tif', zoom=7, ll=True)
    rtr = rio.open('test2.tif')
    rimg = np.array([band for band in rtr.read()]).transpose(1, 2, 0)
    assert rimg.shape == img.shape
    assert rimg.sum() == img.sum()
    assert_array_almost_equal(rimg.mean(), img.mean())
    assert_array_almost_equal(
        ext, (0.0, 939258.2035682457, 6261721.35712164, 6887893.492833804))
    rtr_bounds = [
        -611.49622628141, 6262332.853347922, 938646.7073419644,
        6888504.989060086
    ]
    assert_array_almost_equal(list(rtr.bounds), rtr_bounds)
示例#2
0
def download_map_bg(self):
    if self.config.bg_img is None:
        raise Exception("Variable Drawer.background_img_path is not defined")

    print(f"Downloading map's background image to {self.config.bg_img}")
    img, ext = cx.bounds2raster(
        self.config.map_xlim[0],
        self.config.map_ylim[0],
        self.config.map_xlim[1],
        self.config.map_ylim[1],
        self.config.bg_img,
        ll=True,
        # source=cx.providers.CartoDB.Positron,
        # source='https://a.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png'
        source='http://www.google.cn/maps/vt?lyrs=s@189&gl=cn&x={x}&y={y}&z={z}'
    )

    def rgb2gray(rgb):
        r, g, b = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2]
        gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
        return gray

    plt.close()
    plt.imshow(rgb2gray(img),
               extent=ext,
               cmap=plt.get_cmap('gray'),
               vmin=0,
               vmax=255)
    plt.show()
    exit()
示例#3
0
def test_ll2wdw():
    w, s, e, n = (-106.6495132446289, 25.845197677612305, -93.50721740722656,
                  36.49387741088867)
    hou = (-10676650.69219051, 3441477.046670125, -10576977.7804825,
           3523606.146650609)
    _ = ctx.bounds2raster(w, s, e, n, 'test.tif', zoom=4, ll=True)
    rtr = rio.open('test.tif')
    wdw = ctx.tile.bb2wdw(hou, rtr)
    assert wdw == ((152, 161), (189, 199))
示例#4
0
def test_bounds2raster():
    w, s, e, n = (-106.6495132446289, 25.845197677612305, -93.50721740722656,
                  36.49387741088867)
    _ = ctx.bounds2raster(w, s, e, n, 4, 'test.tif', ll=True)
    rtr = rio.open('test.tif')
    img = np.array([band for band in rtr.read()]).transpose(1, 2, 0)
    solu = (-12528334.684053527, 2509580.5126589066, -10023646.141204873,
            5014269.05550756)
    for i, j in zip(rtr.bounds, solu):
        assert round(i - j, TOL) == 0
    assert img[100, 100, :].tolist() == [230, 229, 188]
    assert img[100, 200, :].tolist() == [156, 180, 131]
    assert img[200, 100, :].tolist() == [230, 225, 189]

    # multiple tiles for which result is not square
    w, s, e, n = (2.5135730322461427, 49.529483547557504, 6.15665815595878,
                  51.47502370869813)
    raster, _ = ctx.bounds2raster(w, s, e, n, 7, 'test.tif', ll=True)
    rtr = rio.open('test.tif')
    img = np.array([band for band in rtr.read()]).transpose(1, 2, 0)
    assert raster.shape == img.shape
示例#5
0
 def download_bg(self, save_path):
     print(f"Downloading map's background image to {save_path}")
     img, ext = cx.bounds2raster(
         self.xlim[0],
         self.ylim[0],
         self.xlim[1],
         self.ylim[1],
         save_path,
         ll=True,
         # source=cx.providers.CartoDB.Positron,
         # source='https://a.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png'
         source=
         'http://www.google.cn/maps/vt?lyrs=s@189&gl=cn&x={x}&y={y}&z={z}')
示例#6
0
def test_warp_img_transform():
    w, s, e, n = ext = (
        -106.6495132446289,
        25.845197677612305,
        -93.50721740722656,
        36.49387741088867,
    )
    _ = ctx.bounds2raster(w, s, e, n, "test.tif", zoom=4, ll=True)
    rtr = rio.open("test.tif")
    img = np.array([band for band in rtr.read()])
    wimg, wext = ctx.warp_img_transform(img, rtr.transform, rtr.crs,
                                        {"init": "epsg:4326"})
    assert wimg[:, 100, 100].tolist() == [228, 221, 184]
    assert wimg[:, 100, 200].tolist() == [213, 219, 177]
    assert wimg[:, 200, 100].tolist() == [133, 130, 109]
示例#7
0
def write_district_osm_tile(district_gdf: gpd.geodataframe.GeoDataFrame,
                            filename: str) -> bool:
    """Writes the map tile corresponding to a district geo dataframe to a geotiff.

    Args:
        district_gdf (gpd.geodataframe.GeoDataFrame): District for which map tile needs to be extracted
        filename (str): geotiff file name to which the map tile is written to.

    Returns:
        bool: Returns True if the map tile is not empty. 
    """
    (w, s, e, n) = district_extents(district_gdf)
    img, ex = ctx.bounds2raster(w,
                                s,
                                e,
                                n,
                                ll=True,
                                path=os.environ.get("OSM_DIR") + filename,
                                source=ctx.providers.CartoDB.Positron)
    if img.size != 0:
        return True
    else:
        return False
示例#8
0
# 多个行政区域多个多边形的情形,total_bounds对了,用total_bounds
gdzh["geometry"].bounds
gdzh["geometry"].total_bounds

we, se, ee, ne = gdzh["geometry"].total_bounds
# 计算视野需要的zoom参数 -6? 不对
zoom = ctx.tile._calculate_zoom(we, se, ee, ne)
# 计算要下载的瓦片数量,zoom=10:15,11:40, zoom = -6时只需1块 tile
_ = ctx.howmany(we, se, ee, ne, 11, ll=False)
# 下载瓦片图并画图,不接受负值的zoom,ll=True, 火星坐标等,ll=False, 墨卡托坐标,前面已转换成该坐标
# zoom=11是最合适的,就是上面ctx.add_basemap(ax,source=gaodeurl)自动选定的zoom
img, ext = ctx.bounds2img(we, se, ee, ne, 11, ll=False, source=gaodeurl)
fig, ax = plt.subplots(1, figsize=(16, 16))
plt.imshow(img, extent=ext)
# 另存成tif图像文件,程序挂起,?
_ = ctx.bounds2raster(we, se, ee, ne, 'zh.tif', ll=False)

# Orange Geo的离线地图,除美国外都是到省一级
gdf7 = gpd.read_file("D:/Anaconda3/Lib/site-packages/orangecontrib/geo/geojson/admin1-CHN.json",\
                     encoding='utf-8')
gdf7.plot(figsize=(16, 12))
plt.show()
# 美国的地图边界到郡一级
gdf8 = gpd.read_file("D:/Anaconda3/Lib/site-packages/orangecontrib/geo/geojson/admin2-USA.json",\
                     encoding='utf-8')
gdf8.plot(figsize=(128, 80))
plt.show()

# 手工建立映射表
provs3 = ["甘肃","青海","广西","贵州","重庆","北京","福建","安徽","广东","西藏","新疆","海南",\
          "宁夏","陕西","山西","湖北","湖南","四川","云南","河北","河南","辽宁","山东","天津",\
示例#9
0
def test_add_basemap():
    # Plot boulder bbox as in test_place
    x1, x2, y1, y2 = [
        -11740727.544603072,
        -11701591.786121061,
        4852834.0517692715,
        4891969.810251278,
    ]

    # Test web basemap
    fig, ax = matplotlib.pyplot.subplots(1)
    ax.set_xlim(x1, x2)
    ax.set_ylim(y1, y2)
    ctx.add_basemap(ax, zoom=10)

    # ensure add_basemap did not change the axis limits of ax
    ax_extent = (x1, x2, y1, y2)
    assert ax.axis() == ax_extent

    assert ax.images[0].get_array().sum() == 51551927
    assert ax.images[0].get_array().shape == (256, 256, 4)
    assert_array_almost_equal(ax.images[0].get_array()[:,:,:3].mean(), 177.20665995279947)
    assert_array_almost_equal(ax.images[0].get_array().mean(), 196.654995)

    # Test local source
    ## Windowed read
    subset = (
        -11730803.981631357,
        -11711668.223149346,
        4862910.488797557,
        4882046.247279563,
    )

    f, ax = matplotlib.pyplot.subplots(1)
    ax.set_xlim(subset[0], subset[1])
    ax.set_ylim(subset[2], subset[3])
    loc = ctx.Place(SEARCH, path="./test2.tif", zoom_adjust=ADJUST)
    ctx.add_basemap(ax, source="./test2.tif", reset_extent=True)

    assert_array_almost_equal(subset, ax.images[0].get_extent())
    assert ax.images[0].get_array().sum() == 3187219
    assert ax.images[0].get_array()[:,:,:3].sum() == 2175124
    assert ax.images[0].get_array().shape == (64, 64, 4)
    assert_array_almost_equal(ax.images[0].get_array()[:,:,:3].mean(), 177.01204427083334)
    assert_array_almost_equal(ax.images[0].get_array().mean(), 194.53240966796875)
    ## Full read
    f, ax = matplotlib.pyplot.subplots(1)
    ax.set_xlim(x1, x2)
    ax.set_ylim(y1, y2)
    loc = ctx.Place(SEARCH, path="./test2.tif", zoom_adjust=ADJUST)
    ctx.add_basemap(ax, source="./test2.tif", reset_extent=False)

    raster_extent = (
        -11740880.418659642,
        -11662608.901695622,
        4774715.408861821,
        4931258.442789858,
    )
    assert_array_almost_equal(raster_extent, ax.images[0].get_extent())
    assert ax.images[0].get_array()[:,:,:3].sum() == 76248416
    assert ax.images[0].get_array().sum() == 109671776
    assert ax.images[0].get_array().shape == (512, 256, 4)
    assert_array_almost_equal(ax.images[0].get_array()[:,:,:3].mean(), 193.90974934895834)
    assert_array_almost_equal(ax.images[0].get_array().mean(), 209.18231201171875)

    # Test with auto-zoom
    f, ax = matplotlib.pyplot.subplots(1)
    ax.set_xlim(x1, x2)
    ax.set_ylim(y1, y2)
    ctx.add_basemap(ax, zoom="auto")

    ax_extent = (
        -11740727.544603072,
        -11701591.786121061,
        4852834.051769271,
        4891969.810251278,
    )
    assert_array_almost_equal(ax_extent, ax.images[0].get_extent())
    assert ax.images[0].get_array()[:,:,:3].sum() == 563185119
    assert ax.images[0].get_array().sum() == 830571999
    assert ax.images[0].get_array().shape == (1024, 1024, 4)
    assert_array_almost_equal(ax.images[0].get_array()[:,:,:3].mean(), 179.03172779083252)
    assert_array_almost_equal(ax.images[0].get_array().mean(), 198.023796)

    # Test on-th-fly warping
    x1, x2 = -105.5, -105.00
    y1, y2 = 39.56, 40.13
    f, ax = matplotlib.pyplot.subplots(1)
    ax.set_xlim(x1, x2)
    ax.set_ylim(y1, y2)
    ctx.add_basemap(ax, crs={"init": "epsg:4326"}, attribution=None)
    assert ax.get_xlim() == (x1, x2)
    assert ax.get_ylim() == (y1, y2)
    assert ax.images[0].get_array()[:,:,:3].sum() == 724238693
    assert ax.images[0].get_array().shape == (1135, 1183, 4)
    assert_array_almost_equal(ax.images[0].get_array()[:,:,:3].mean(), 179.79593258881636)
    assert_array_almost_equal(ax.images[0].get_array().mean(), 198.596949)
    # Test local source warping
    _ = ctx.bounds2raster(x1, y1, x2, y2, "./test2.tif", ll=True)
    f, ax = matplotlib.pyplot.subplots(1)
    ax.set_xlim(x1, x2)
    ax.set_ylim(y1, y2)
    ctx.add_basemap(
        ax, source="./test2.tif", crs={"init": "epsg:4326"}, attribution=None
    )
    assert ax.get_xlim() == (x1, x2)
    assert ax.get_ylim() == (y1, y2)

    assert ax.images[0].get_array()[:,:,:3].sum() == 464536503
    assert ax.images[0].get_array().shape == (980, 862, 4)
    assert_array_almost_equal(ax.images[0].get_array()[:,:,:3].mean(), 183.301175)

    assert ax.images[0].get_array().sum() == 678981558
    assert_array_almost_equal(ax.images[0].get_array().mean(), 200.939189)

    x1, x2, y1, y2 = [
        -11740727.544603072,
        -11701591.786121061,
        4852834.0517692715,
        4891969.810251278,
    ]
示例#10
0
def test_add_basemap():
    # Plot boulder bbox as in test_place
    x1, x2, y1, y2 = [
        -11740727.544603072,
        -11701591.786121061,
        4852834.0517692715,
        4891969.810251278,
    ]

    # Test web basemap
    fig, ax = matplotlib.pyplot.subplots(1)
    ax.set_xlim(x1, x2)
    ax.set_ylim(y1, y2)
    ax = ctx.add_basemap(ax, zoom=10)

    # ensure add_basemap did not change the axis limits of ax
    ax_extent = (x1, x2, y1, y2)
    assert ax.axis() == ax_extent

    assert ax.images[0].get_array().sum() == 75853866
    assert ax.images[0].get_array().shape == (256, 512, 3)
    assert_array_almost_equal(ax.images[0].get_array().mean(),
                              192.90635681152344)

    # Test local source
    f, ax = matplotlib.pyplot.subplots(1)
    ax.set_xlim(x1, x2)
    ax.set_ylim(y1, y2)
    loc = ctx.Place(SEARCH, path="./test2.tif", zoom_adjust=ADJUST)
    ax = ctx.add_basemap(ax, url="./test2.tif")

    raster_extent = (
        -11740803.981631357,
        -11701668.223149346,
        4852910.488797557,
        4892046.247279563,
    )
    assert_array_almost_equal(raster_extent, ax.images[0].get_extent())
    assert ax.images[0].get_array().sum() == 34840247
    assert ax.images[0].get_array().shape == (256, 256, 3)
    assert_array_almost_equal(ax.images[0].get_array().mean(),
                              177.20665995279947)

    # Test with auto-zoom
    f, ax = matplotlib.pyplot.subplots(1)
    ax.set_xlim(x1, x2)
    ax.set_ylim(y1, y2)
    ax = ctx.add_basemap(ax, zoom="auto")

    ax_extent = (
        -11740727.544603072,
        -11691807.846500559,
        4852834.0517692715,
        4891969.810251278,
    )
    assert_array_almost_equal(ax_extent, ax.images[0].get_extent())
    assert ax.images[0].get_array().sum() == 723918764
    assert ax.images[0].get_array().shape == (1024, 1280, 3)
    assert_array_almost_equal(ax.images[0].get_array().mean(),
                              184.10206197102863)

    # Test on-th-fly warping
    x1, x2 = -105.5, -105.00
    y1, y2 = 39.56, 40.13
    f, ax = matplotlib.pyplot.subplots(1)
    ax.set_xlim(x1, x2)
    ax.set_ylim(y1, y2)
    ax = ctx.add_basemap(ax, crs={"init": "epsg:4326"}, attribution=None)
    assert ax.get_xlim() == (x1, x2)
    assert ax.get_ylim() == (y1, y2)
    assert ax.images[0].get_array().sum() == 724238693
    assert ax.images[0].get_array().shape == (1135, 1183, 3)
    assert_array_almost_equal(ax.images[0].get_array().mean(),
                              179.79593258881636)
    # Test local source warping
    _ = ctx.bounds2raster(x1, y1, x2, y2, "./test2.tif", ll=True)
    f, ax = matplotlib.pyplot.subplots(1)
    ax.set_xlim(x1, x2)
    ax.set_ylim(y1, y2)
    ax = ctx.add_basemap(ax,
                         url="./test2.tif",
                         crs={"init": "epsg:4326"},
                         attribution=None)
    assert ax.get_xlim() == (x1, x2)
    assert ax.get_ylim() == (y1, y2)
    assert ax.images[0].get_array().sum() == 724238693
    assert ax.images[0].get_array().shape == (1135, 1183, 3)
    assert_array_almost_equal(ax.images[0].get_array().mean(),
                              179.79593258881636)
df_well_tops[::5].apply(lambda x: 
                   ax.annotate(
                       s=x.wlbWellboreName, 
                       xy=x.geometry.centroid.coords[0], 
                       ha='left',
                       c='white'
                   ), axis=1);

# -

west, south, east, north = bbox = df_well_tops.total_bounds
img, ext = ctx.bounds2raster(west,
                             south,
                             east,
                             north,
                             "world_watercolor.tif",
                             source=ctx.providers.Stamen.Watercolor,
                             ll=True,
                             zoom=8
                            )




west, south, east, north = bbox = df_well_tops.total_bounds
img, ext = ctx.bounds2raster(west,
                             south,
                             east,
                             north,
                             "oceanesri.tif",
                             source=ctx.providers.Esri.OceanBasemap,
示例#12
0
def test_add_basemap():
    # Plot boulder bbox as in test_place
    x1, x2, y1, y2 = [
        -11740727.544603072,
        -11701591.786121061,
        4852834.0517692715,
        4891969.810251278,
    ]

    # Test web basemap
    fig, ax = matplotlib.pyplot.subplots(1)
    ax.set_xlim(x1, x2)
    ax.set_ylim(y1, y2)
    ctx.add_basemap(ax, zoom=10)

    # ensure add_basemap did not change the axis limits of ax
    ax_extent = (x1, x2, y1, y2)
    assert ax.axis() == ax_extent

    assert ax.images[0].get_array().sum() == 34840247
    assert ax.images[0].get_array().shape == (256, 256, 3)
    assert_array_almost_equal(ax.images[0].get_array().mean(), 177.20665995279947)

    # Test local source
    ## Windowed read
    subset = (
        -11730803.981631357,
        -11711668.223149346,
        4862910.488797557,
        4882046.247279563,
    )

    f, ax = matplotlib.pyplot.subplots(1)
    ax.set_xlim(subset[0], subset[1])
    ax.set_ylim(subset[2], subset[3])
    loc = ctx.Place(SEARCH, path="./test2.tif", zoom_adjust=ADJUST)
    ctx.add_basemap(ax, url="./test2.tif", reset_extent=True)

    raster_extent = (
        -11740803.981631357,
        -11701668.223149346,
        4852910.488797556,
        4892046.247279563,
    )
    assert_array_almost_equal(raster_extent, ax.images[0].get_extent())
    assert ax.images[0].get_array().sum() == 8440966
    assert ax.images[0].get_array().shape == (126, 126, 3)
    assert_array_almost_equal(ax.images[0].get_array().mean(), 177.22696733014195)
    ## Full read
    f, ax = matplotlib.pyplot.subplots(1)
    ax.set_xlim(x1, x2)
    ax.set_ylim(y1, y2)
    loc = ctx.Place(SEARCH, path="./test2.tif", zoom_adjust=ADJUST)
    ctx.add_basemap(ax, source="./test2.tif", reset_extent=False)

    raster_extent = (
        -11740803.981631357,
        -11701668.223149346,
        4852910.488797557,
        4892046.247279563,
    )
    assert_array_almost_equal(raster_extent, ax.images[0].get_extent())
    assert ax.images[0].get_array().sum() == 34840247
    assert ax.images[0].get_array().shape == (256, 256, 3)
    assert_array_almost_equal(ax.images[0].get_array().mean(), 177.20665995279947)

    # Test with auto-zoom
    f, ax = matplotlib.pyplot.subplots(1)
    ax.set_xlim(x1, x2)
    ax.set_ylim(y1, y2)
    ctx.add_basemap(ax, zoom="auto")

    ax_extent = (
        -11740727.544603072,
        -11701591.786121061,
        4852834.051769271,
        4891969.810251278,
    )
    assert_array_almost_equal(ax_extent, ax.images[0].get_extent())
    assert ax.images[0].get_array().sum() == 563185119
    assert ax.images[0].get_array().shape == (1024, 1024, 3)
    assert_array_almost_equal(ax.images[0].get_array().mean(), 179.03172779083252)

    # Test on-th-fly warping
    x1, x2 = -105.5, -105.00
    y1, y2 = 39.56, 40.13
    f, ax = matplotlib.pyplot.subplots(1)
    ax.set_xlim(x1, x2)
    ax.set_ylim(y1, y2)
    ctx.add_basemap(ax, crs={"init": "epsg:4326"}, attribution=None)
    assert ax.get_xlim() == (x1, x2)
    assert ax.get_ylim() == (y1, y2)
    assert ax.images[0].get_array().sum() == 724238693
    assert ax.images[0].get_array().shape == (1135, 1183, 3)
    assert_array_almost_equal(ax.images[0].get_array().mean(), 179.79593258881636)
    # Test local source warping
    _ = ctx.bounds2raster(x1, y1, x2, y2, "./test2.tif", ll=True)
    f, ax = matplotlib.pyplot.subplots(1)
    ax.set_xlim(x1, x2)
    ax.set_ylim(y1, y2)
    ctx.add_basemap(
        ax, source="./test2.tif", crs={"init": "epsg:4326"}, attribution=None
    )
    assert ax.get_xlim() == (x1, x2)
    assert ax.get_ylim() == (y1, y2)
    assert ax.images[0].get_array().sum() == 464751694
    assert ax.images[0].get_array().shape == (980, 862, 3)
    assert_array_almost_equal(ax.images[0].get_array().mean(), 183.38608756727749)