Exemplo n.º 1
0
def test_place():
    expected_bbox = [-105.3014509, 39.9643513, -105.1780988, 40.094409]
    expected_bbox_map = [
        -11740727.544603072, -11701591.786121061, 4852834.0517692715,
        4891969.810251278
    ]
    expected_zoom = 10
    loc = ctx.Place(SEARCH, zoom_adjust=ADJUST)
    assert loc.im.shape == (256, 256, 3)
    loc  # Make sure repr works

    # Check auto picks are correct
    assert loc.search == SEARCH
    assert_array_almost_equal([loc.w, loc.s, loc.e, loc.n], expected_bbox)
    assert_array_almost_equal(loc.bbox_map, expected_bbox_map)
    assert loc.zoom == expected_zoom

    loc = ctx.Place(SEARCH, path="./test2.tif", zoom_adjust=ADJUST)
    assert os.path.exists("./test2.tif")

    # .plot() method
    ax = loc.plot()
    assert_array_almost_equal(loc.bbox_map, ax.images[0].get_extent())

    f, ax = matplotlib.pyplot.subplots(1)
    ax = loc.plot(ax=ax)
    assert_array_almost_equal(loc.bbox_map, ax.images[0].get_extent())
Exemplo n.º 2
0
def test_place():
    expected_bbox = [-105.430545, 39.8549856, -105.110545, 40.1749856]
    expected_bbox_map = [
        -11740727.544603072,
        -11662456.027639052,
        4774562.53480525,
        4931105.568733288,
    ]
    expected_zoom = 9
    loc = ctx.Place(SEARCH, zoom_adjust=ADJUST)
    assert loc.im.shape == (512, 256, 4)
    loc  # Make sure repr works

    # Check auto picks are correct
    assert loc.search == SEARCH
    assert_array_almost_equal([loc.w, loc.s, loc.e, loc.n], expected_bbox)
    assert_array_almost_equal(loc.bbox_map, expected_bbox_map)
    assert loc.zoom == expected_zoom

    loc = ctx.Place(SEARCH, path="./test2.tif", zoom_adjust=ADJUST)
    assert os.path.exists("./test2.tif")

    # .plot() method
    ax = loc.plot()
    assert_array_almost_equal(loc.bbox_map, ax.images[0].get_extent())

    f, ax = matplotlib.pyplot.subplots(1)
    ax = loc.plot(ax=ax)
    assert_array_almost_equal(loc.bbox_map, ax.images[0].get_extent())
  def loadShapefiles(self):

    self._loc = ctx.Place(self._basemap, zoom_adjust=0)  # zoom_adjust modifies the auto-zoom
    # Print some metadata
    self._xs = {}
    
    # Longitude w,e Latitude n,s
    for attr in ["w", "s", "e", "n", "place", "zoom", "n_tiles"]:
      self._xs[attr] = getattr(self._loc, attr)
      print("{}: {}".format(attr, self._xs[attr]))

    self._xs["centroid"] = LineString(
        (
          (self._xs["w"], self._xs["s"]),
          (self._xs["e"], self._xs["n"])
        )
      ).centroid

    self._xs["bbox"] = Polygon.from_bounds(
          self._xs["w"], self._xs["s"],
          self._xs["e"], self._xs["n"]
        )

    self._xs["dx"] = 111.32; #One degree in longitude is this in KM 
    self._xs["dy"] = 40075 * math.cos( self._xs["centroid"].y ) / 360 
    _log.info("Arc amplitude at this latitude %f, %f"%(self._xs["dx"], self._xs["dy"]))

    path = os.getcwd()
    _log.info("Loading geo data from path:"+path)
    roads_1 = gpd.read_file(os.path.join(path, "shapefiles","1","roads-line.shp"))
    roads_2 = gpd.read_file(os.path.join(path, "shapefiles","2","roads-line.shp"))
    blocks = gpd.read_file(os.path.join(path, "shapefiles","quartieriBarca1.shp"))
    self._roads = [roads_1, roads_2]
    self._blocks= blocks
Exemplo n.º 4
0
def test_plot_map():
    search = 'boulder'
    loc = ctx.Place(search, zoom_adjust=-3)
    ax = ctx.plot_map(loc)
    assert ax.get_title() == loc.place

    ax = ctx.plot_map(loc.im, loc.bbox)
    assert_array_almost_equal(loc.bbox, ax.images[0].get_extent())
Exemplo n.º 5
0
def test_place():
    search = 'boulder'
    adjust = -3  # To save download size / time
    expected_bbox = [-105.3014509, 39.9643513, -105.1780988, 40.094409]
    expected_bbox_map = [
        -11740727.544603072, -11701591.786121061, 4852834.0517692715,
        4891969.810251278
    ]
    expected_zoom = 10
    loc = ctx.Place(search, zoom_adjust=adjust)
    loc  # Make sure repr works

    # Check auto picks are correct
    assert loc.search == search
    assert_array_almost_equal([loc.w, loc.s, loc.e, loc.n], expected_bbox)
    assert_array_almost_equal(loc.bbox_map, expected_bbox_map)
    assert loc.zoom == expected_zoom

    loc = ctx.Place(search, path="./test2.tif", zoom_adjust=adjust)
    assert os.path.exists("./test2.tif")
Exemplo n.º 6
0
def test_plot_map():
    # Place as a search
    loc = ctx.Place(SEARCH, zoom_adjust=ADJUST)
    w, e, s, n = loc.bbox_map
    ax = ctx.plot_map(loc)

    assert ax.get_title() == loc.place
    ax = ctx.plot_map(loc.im, loc.bbox)
    assert_array_almost_equal(loc.bbox, ax.images[0].get_extent())

    # Place as an image
    img, ext = ctx.bounds2img(w, s, e, n, zoom=10)
    ax = ctx.plot_map(img, ext)
    assert_array_almost_equal(ext, ax.images[0].get_extent())
Exemplo n.º 7
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
    f, ax = matplotlib.pyplot.subplots(1)
    ax.set_xlim(x1, x2)
    ax.set_ylim(y1, y2)
    ax = ctx.add_basemap(ax, zoom=10)

    ax_extent = (-11740727.544603072, -11662456.027639052, 4852834.0517692715,
                 4891969.810251278)
    assert_array_almost_equal(ax_extent, ax.images[0].get_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)
Exemplo n.º 8
0
cases we want to go from a location to a map of that location as quickly
as possible. There are two main ways to do this with Contextily.

Searching for places with text
==============================

The simplest approach is to search for a location with text. You can do
this with the ``Place`` class. This will return an object that contains
metadata about the place, such as its bounding box. It will also contain an
image of the place.
"""
import numpy as np
import matplotlib.pyplot as plt
import contextily as ctx

loc = ctx.Place("boulder", zoom_adjust=0)  # zoom_adjust modifies the auto-zoom

# Print some metadata
for attr in ["w", "s", "e", "n", "place", "zoom", "n_tiles"]:
    print("{}: {}".format(attr, getattr(loc, attr)))

# Show the map
im1 = loc.im

fig, axs = plt.subplots(1, 3, figsize=(15, 5))
ctx.plot_map(loc, ax=axs[0])

###############################################################################
# The zoom level will be chosen for you by default, though you can specify
# this manually as well:
Exemplo n.º 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,
    ]
Exemplo n.º 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)
Exemplo n.º 11
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)