示例#1
0
 def test_distance(self):
     clu_path = get_path("clu/clu_public_a_il189.shp")
     clus, clusdf = vt.read_layer(clu_path)
     path = get_path("cb_2014_us_state_500k.zip")
     vl, vldf = vt.read_layer(path, index="STUSPS")
     s = clus[:5].distances(vl["RI"], proj="albers")/1.e3
     assert s.count() == 5
     assert s.min() > 1440 and s.max() < 1441
     d = vl.boundingboxes().distances(vl["MI"], proj='albers')/1.e3
     assert abs(d["CA"] - 1853.3812112445789) < 1e-6
     # Compute the haversine (great-circle) distance to MI
     MI = vl["MI"].Centroid().GetPoints()[0]
     d = (vl.to_wgs84().centroids(format="Series")
          .map(lambda x: haversine(x, MI))/1.e3)
     assert abs(d["WA"] - 2706.922595) < 1e-6
示例#2
0
 def test_distance(self):
     clu_path = get_path("clu/clu_public_a_il189.shp")
     clus, clusdf = vt.read_layer(clu_path)
     path = get_path("cb_2014_us_state_500k.zip")
     vl, vldf = vt.read_layer(path, index="STUSPS")
     s = clus[:5].distances(vl["RI"], proj="albers")/1.e3
     assert s.count() == 5
     assert s.min() > 1440 and s.max() < 1441
     d = vl.boundingboxes().distances(vl["MI"], proj='albers')/1.e3
     assert abs(d["CA"] - 1853.3812112445789) < 1e-6
     # Compute the haversine (great-circle) distance to MI
     MI = vl["MI"].Centroid().GetPoints()[0]
     d = (vl.to_wgs84().centroids(format="Series")
          .map(lambda x: haversine(x, MI))/1.e3)
     assert abs(d["WA"] - 2706.922595) < 1e-6
示例#3
0
def __test_read():
    path = ("http://www2.census.gov/geo/tiger/GENZ2014/shp/"
            "cb_2014_us_state_500k.zip")

    assert len(vt.read_layer(path)[0]) == 56

    path = get_path("cb_2014_us_state_500k.zip")
    assert len(vt.read_layer(path)[0]) == 56

    paths = ["http://eric.clst.org/wupl/Stuff/gz_2010_us_040_00_500k.json",
             get_path("gz_2010_us_040_00_500k.json")]

    for path in paths:
        assert len(vt.read_geojson(path)[0]) == 52

    geojson_str = open(paths[1]).read()
    assert len(vt.read_geojson(geojson_str)[0]) == 52
示例#4
0
 def test_ipredicates(self):
     path = get_path("cb_2014_us_state_500k.zip")
     vl, df = vt.read_layer(path, index="STUSPS")
     clu_path = get_path("clu/clu_public_a_il189.shp")
     clus, clusdf = vt.read_layer(clu_path)
     assert vl.iintersects(clus[0])[0] == "IL"
     assert len(vl.iwithin(clus[0])) == 0
     assert "IL" not in vl.idisjoint(clus[0])
     centroid = vl["CA"].Centroid()
     centroid.AssignSpatialReference(vl.proj)
     # California contains its centroid
     assert vl.icontains(centroid)[0] == "CA"
     # Clus are within IL
     assert len(clus.head().iwithin(vl["IL"])) > 0
     # No states within clus
     assert len(vl.iwithin(clus.head()[0])) == 0
     assert len(vl[["CA"]].boundingboxes().iwithin(vl["CA"])) == 0
示例#5
0
 def test_ipredicates(self):
     path = get_path("cb_2014_us_state_500k.zip")
     vl, df = vt.read_layer(path, index="STUSPS")
     clu_path = get_path("clu/clu_public_a_il189.shp")
     clus, clusdf = vt.read_layer(clu_path)
     assert vl.iintersects(clus[0])[0] == "IL"
     assert len(vl.iwithin(clus[0])) == 0
     assert "IL" not in vl.idisjoint(clus[0])
     centroid = vl["CA"].Centroid()
     centroid.AssignSpatialReference(vl.proj)
     # California contains its centroid
     assert vl.icontains(centroid)[0] == "CA"
     # Clus are within IL
     assert len(clus.head().iwithin(vl["IL"])) > 0
     # No states within clus
     assert len(vl.iwithin(clus.head()[0])) == 0
     assert len(vl[["CA"]].boundingboxes().iwithin(vl["CA"])) == 0
示例#6
0
def __test_read():
    path = ("http://www2.census.gov/geo/tiger/GENZ2014/shp/"
            "cb_2014_us_state_500k.zip")

    assert len(vt.read_layer(path)[0]) == 56

    path = get_path("cb_2014_us_state_500k.zip")
    assert len(vt.read_layer(path)[0]) == 56

    paths = ["http://eric.clst.org/wupl/Stuff/gz_2010_us_040_00_500k.json",
             get_path("gz_2010_us_040_00_500k.json")]

    for path in paths:
        assert len(vt.read_geojson(path)[0]) == 52

    geojson_str = open(paths[1]).read()
    assert len(vt.read_geojson(geojson_str)[0]) == 52
示例#7
0
 def setup_class(cls):
     cls.base_dir = os.path.abspath(os.path.dirname(__file__))
     path = os.path.join(cls.base_dir, "data/vector")
     path = os.path.join(path, "bay_area_counties.geojson")
     cls.counties, cls.counties_df = vt.read_geojson(path, index="NAME")
     clu_path = "data/vector/clu/clu_public_a_il189.shp"
     cls.clus, _ = vt.read_layer(os.path.join(cls.base_dir, clu_path))
     il_path = os.path.join(cls.base_dir, "data/vector/il.geojson")
     cls.il, cls.il_df = vt.read_geojson(il_path, index="name")
示例#8
0
 def setup_class(cls):
     cls.base_dir = os.path.abspath(os.path.dirname(__file__))
     path = os.path.join(cls.base_dir, "data/vector")
     path = os.path.join(path, "bay_area_counties.geojson")
     cls.counties, cls.counties_df = vt.read_geojson(path, index="NAME")
     clu_path = "data/vector/clu/clu_public_a_il189.shp"
     cls.clus, _ = vt.read_layer(os.path.join(cls.base_dir, clu_path))
     il_path = os.path.join(cls.base_dir, "data/vector/il.geojson")
     cls.il, cls.il_df = vt.read_geojson(il_path, index="name")
示例#9
0
def test_vector_shp_io_gs():
    filepath = 'data/vector/clu/clu_public_a_co095.shp'
    gs_path = 'gs://%s/%s' % (gs_bucket_name, filepath)

    # write a vector shape file to gs
    co, co_df = vt.read_layer(os.path.join(base, filepath))
    co.to_shapefile(gs_path, df=co_df)

    # read the vector shape file from gs
    #co, co_df = vt.read_layer(gs_path)
    #assert isinstance(co, vt.VectorLayer)

    # clean up
    delete_gs_key(filepath)
示例#10
0
    def setup_class(cls):
        os.chdir(base)
        # Let's add some CLUs for Washington County, IL
        cls.vl, _ = read_layer(get_path("vector/clu/clu_public_a_il189.shp"),
                               index="uniqueid")

        # Figure out which raster files we'll need.
        cls.dataset = read_catalog(get_path("../catalog/cdl_2014.json"))

        # Create a RasterBand for the raster. Raster data is stored
        # and read from there.
        cls.rb = RasterBand(get_path("raster/95000_45000.tif"))

        # Convert CLUs to pixels.
        cls.px_shps = cls.dataset.to_pixels(cls.vl)
示例#11
0
    def setup_class(cls):

        base = os.path.abspath(os.path.dirname(__file__))
        f = lambda x: os.path.join(base, x)
        # Let's add some CLUs for Washington County, IL

        vl, _ = read_layer(f("data/vector/clu/clu_public_a_il189.shp"),
                           index="uniqueid")

        # For speed, let's just use one shape.
        one_shape_id = "3850036893056913813r"
        cls.vl_one_shape = vl[[one_shape_id]]

        # Create a RasterBand for the raster. Raster data is stored and read from there.
        cls.rb = RasterBand(f("data/raster/95000_45000.tif"))
示例#12
0
    def setup_class(cls):
        os.chdir(base)

        # Let's add some CLUs for Washington County, IL
        cls.vl, _ = read_layer(get_path("vector/clu/clu_public_a_il189.shp"),
                               index="uniqueid")
        cls.one_shape_id = "3850036893056913813r"
        cls.vl_one_shape = cls.vl[[cls.one_shape_id]]
        cls.areas = cls.vl.areas()
        cls.areas = cls.areas / cls.areas.sum()

        # Read expected output computed via single-tile computation
        # from a file, so we can compare against that.
        cls.df_expected = pd.read_csv(get_path("expected.csv"), index_col=0)
        cls.df_expected.rename(columns=lambda x: int(x), inplace=True)
        cls.df_expected.applymap(lambda x: int(x))

        # For single shape output, we only want to compare our data against that shape.
        cls.df_expected_one_shape = cls.df_expected.loc[cls.one_shape_id]
示例#13
0
    def setup_class(cls):
        os.chdir(base)

        # Let's add some CLUs for Washington County, IL
        cls.vl, _ = read_layer(get_path("vector/clu/clu_public_a_il189.shp"),
                               index="uniqueid")
        cls.one_shape_id = "3850036893056913813r"
        cls.vl_one_shape = cls.vl[[cls.one_shape_id]]
        cls.areas = cls.vl.areas()
        cls.areas = cls.areas/cls.areas.sum()

        # Read expected output computed via single-tile computation
        # from a file, so we can compare against that.
        cls.df_expected = pd.read_csv(get_path("expected.csv"),
                                      index_col=0)
        cls.df_expected.rename(columns=lambda x: int(x), inplace=True)
        cls.df_expected.applymap(lambda x: int(x))

        # For single shape output, we only want to compare our data against that shape.
        cls.df_expected_one_shape = cls.df_expected.loc[cls.one_shape_id]
示例#14
0
    def test_co_ne_border(self):
        p = get_path("vector/clu/clu_public_a_co095.shp")
        vl, _ = read_layer(p)
        p = get_path("vector/co_ne_border.geojson")
        co_ne_border, df = read_geojson(p)
        vl = vl.within(co_ne_border.bbox())

        rd = read_catalog(get_path("../catalog/co_soil.json"))
        for r in rd.query(vl):
            r
            #compute_stats(r.values, r.weights)

        rd = read_catalog(get_path("../catalog/co_soil_bad.json"))
        failed = False
        try:
            for r in rd.query(vl):
                r
                #compute_stats(r.values, r.weights)
        except IndexError:
            failed = True

        assert failed
示例#15
0
    def test_co_ne_border(self):
        p = get_path("vector/clu/clu_public_a_co095.shp")
        vl, _ = read_layer(p)
        p = get_path("vector/co_ne_border.geojson")
        co_ne_border, df = read_geojson(p)
        vl = vl.within(co_ne_border.bbox())

        rd = read_catalog(get_path("../catalog/co_soil.json"))
        for r in rd.query(vl):
            r
            #compute_stats(r.values, r.weights)

        rd = read_catalog(get_path("../catalog/co_soil_bad.json"))
        failed = False
        try:
            for r in rd.query(vl):
                r
                #compute_stats(r.values, r.weights)
        except IndexError:
            failed = True

        assert failed
示例#16
0
  * Neither the name of the nor the names of its contributors may be used to endorse or promote products 
    derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

import os
from nose.tools import assert_equals
from pyspatial.vector import read_layer
from pyspatial.visualize import get_latlngs

base = os.path.abspath(os.path.dirname(__file__))
get_path = lambda x: os.path.join(base, "data/vector", x)

vl, vldf = read_layer(get_path("cb_2014_us_state_20m.zip"), index="STUSPS")


def test_getlatlngs():
    shp = vl.to_shapely("CA")
    exp = [{'lat': 37.242214717335116, 'lng': -119.61111973321412}]
    assert_equals(get_latlngs(vl["CA"]), exp)
    assert_equals(get_latlngs(shp), exp)
    assert_equals([get_latlngs(vl)[0]], exp)
示例#17
0
 def test_read(self):
     states, df = vt.read_layer(get_path("cb_2014_us_state_500k.zip"))
     assert isinstance(states, vt.VectorLayer)
     assert len(states) == 56
     co, co_df = vt.read_layer(get_path("clu/clu_public_a_co095.shp"))
     assert isinstance(co, vt.VectorLayer)
示例#18
0
 def test_to_json(self):
     with open(get_path("RI.json")) as inf:
         exp = inf.read()
     act, _ = vt.read_layer(get_path("cb_2014_us_state_20m.zip"),
                            index="STUSPS")
     assert exp == act[["RI"]].to_json()
示例#19
0
 def test_read_index(self):
     path = get_path("cb_2014_us_state_500k.zip")
     vl, df = vt.read_layer(path, index="STUSPS")
     assert all([a == b for a, b in zip(vl.index, df.index)])
     vl, df = vt.read_layer(path, index=xrange(5, 61))
     assert_raises(ValueError, vt.read_layer, path, 0, xrange(5, 56))
示例#20
0
 def test_to_json(self):
     with open(get_path("RI.json")) as inf:
         exp = inf.read()
     act, _ = vt.read_layer(get_path("cb_2014_us_state_20m.zip"),
                            index="STUSPS")
     assert exp == act[["RI"]].to_json()
示例#21
0
 def test_read_index(self):
     path = get_path("cb_2014_us_state_500k.zip")
     vl, df = vt.read_layer(path, index="STUSPS")
     assert all([a == b for a, b in zip(vl.index, df.index)])
     vl, df = vt.read_layer(path, index=xrange(5, 61))
     assert_raises(ValueError, vt.read_layer, path, 0, xrange(5, 56))
示例#22
0
 def test_read(self):
     states, df = vt.read_layer(get_path("cb_2014_us_state_500k.zip"))
     assert isinstance(states, vt.VectorLayer)
     assert len(states) == 56
     co, co_df = vt.read_layer(get_path("clu/clu_public_a_co095.shp"))
     assert isinstance(co, vt.VectorLayer)
示例#23
0
            if len(tiles) == 0:
                raise ValueError("%s is empty" % args.tile_path)

            tile = os.path.join(args.tile_path, tiles[0])
            ds = gdal.OpenShared(tile)
            if ds is None:
                raise ValueError("Unable to open file: %s" % tile)

            xsize = ds.RasterXSize
            ysize = ds.RasterYSize
            if xsize != ysize:
                raise ValueError("tiles must have same X and Y size")

            catalog["GridSize"] = xsize
            catalog["Path"] = args.tile_path
        else:
            raise ValueError("tiles path does not exist: %s" % args.tile_path)

    if args.index_path is not None:
        if args.index_path.endswith("json"):
            index, index_df = read_geojson(args.index_path)
        else:
            index = read_layer(args.index_path)
        catalog["Index"] = index.transform(projection_from_string()).to_dict()

    if args.dest is not None:
        with open(args.dest, "w+b") as outf:
            outf.write(json.dumps(catalog, indent=4, sort_keys=True))
    else:
        print json.dumps(catalog, indent=4, sort_keys=True)
示例#24
0
    derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

import os
from nose.tools import assert_equals
from pyspatial.vector import read_layer
from pyspatial.visualize import get_latlngs

base = os.path.abspath(os.path.dirname(__file__))
get_path = lambda x: os.path.join(base, "data/vector", x)

vl, vldf = read_layer(get_path("cb_2014_us_state_20m.zip"),
                      index="STUSPS")


def test_getlatlngs():
    shp = vl.to_shapely("CA")
    exp = [{'lat': 37.242214717335116, 'lng': -119.61111973321412}]
    assert_equals(get_latlngs(vl["CA"]), exp)
    assert_equals(get_latlngs(shp), exp)
    assert_equals([get_latlngs(vl)[0]], exp)