2011-12-05 21:32:37 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from nose.tools import *
|
2013-06-03 02:28:24 +00:00
|
|
|
from utilities import execution_path, run_all
|
2011-12-05 21:32:37 +00:00
|
|
|
import os, mapnik
|
|
|
|
|
|
|
|
def setup():
|
|
|
|
# All of the paths used are relative, if we run the tests
|
|
|
|
# from another directory we need to chdir()
|
|
|
|
os.chdir(execution_path('.'))
|
|
|
|
|
2012-09-05 11:53:37 +00:00
|
|
|
if 'ogr' in mapnik.DatasourceCache.plugin_names():
|
2012-02-24 21:13:56 +00:00
|
|
|
|
2011-12-05 21:32:37 +00:00
|
|
|
# Shapefile initialization
|
|
|
|
def test_shapefile_init():
|
|
|
|
s = mapnik.Ogr(file='../../demo/data/boundaries.shp',layer_by_index=0)
|
2012-02-24 21:13:56 +00:00
|
|
|
|
2011-12-05 21:32:37 +00:00
|
|
|
e = s.envelope()
|
2012-02-24 21:13:56 +00:00
|
|
|
|
2011-12-05 21:32:37 +00:00
|
|
|
assert_almost_equal(e.minx, -11121.6896651, places=7)
|
|
|
|
assert_almost_equal(e.miny, -724724.216526, places=6)
|
|
|
|
assert_almost_equal(e.maxx, 2463000.67866, places=5)
|
|
|
|
assert_almost_equal(e.maxy, 1649661.267, places=3)
|
2012-02-24 21:13:56 +00:00
|
|
|
|
2011-12-05 21:32:37 +00:00
|
|
|
# Shapefile properties
|
|
|
|
def test_shapefile_properties():
|
2012-06-27 01:25:56 +00:00
|
|
|
ds = mapnik.Ogr(file='../../demo/data/boundaries.shp',layer_by_index=0)
|
2013-05-24 08:32:48 +00:00
|
|
|
f = ds.features_at_point(ds.envelope().center(), 0.001).features[0]
|
2012-01-17 23:43:01 +00:00
|
|
|
eq_(ds.geometry_type(),mapnik.DataGeometryType.Polygon)
|
2012-02-24 21:13:56 +00:00
|
|
|
|
2011-12-05 21:32:37 +00:00
|
|
|
eq_(f['CGNS_FID'], u'6f733341ba2011d892e2080020a0f4c9')
|
|
|
|
eq_(f['COUNTRY'], u'CAN')
|
|
|
|
eq_(f['F_CODE'], u'FA001')
|
|
|
|
eq_(f['NAME_EN'], u'Quebec')
|
|
|
|
eq_(f['Shape_Area'], 1512185733150.0)
|
|
|
|
eq_(f['Shape_Leng'], 19218883.724300001)
|
2013-06-03 22:19:33 +00:00
|
|
|
# NOTE: encoding is latin1 but gdal >= 1.9 should now expose utf8 encoded features
|
|
|
|
# See SHAPE_ENCODING for overriding: http://gdal.org/ogr/drv_shapefile.html
|
|
|
|
# Failure for the NOM_FR field is expected for older gdal
|
|
|
|
#eq_(f['NOM_FR'], u'Qu\xe9bec')
|
|
|
|
#eq_(f['NOM_FR'], u'Québec')
|
2011-12-05 21:32:37 +00:00
|
|
|
|
2011-12-05 21:33:37 +00:00
|
|
|
@raises(RuntimeError)
|
|
|
|
def test_that_nonexistant_query_field_throws(**kwargs):
|
|
|
|
ds = mapnik.Ogr(file='../data/shp/world_merc.shp',layer_by_index=0)
|
|
|
|
eq_(len(ds.fields()),11)
|
|
|
|
eq_(ds.fields(),['FIPS', 'ISO2', 'ISO3', 'UN', 'NAME', 'AREA', 'POP2005', 'REGION', 'SUBREGION', 'LON', 'LAT'])
|
|
|
|
eq_(ds.field_types(),['str', 'str', 'str', 'int', 'str', 'int', 'int', 'int', 'int', 'float', 'float'])
|
|
|
|
query = mapnik.Query(ds.envelope())
|
|
|
|
for fld in ds.fields():
|
|
|
|
query.add_property_name(fld)
|
|
|
|
# also add an invalid one, triggering throw
|
|
|
|
query.add_property_name('bogus')
|
|
|
|
fs = ds.features(query)
|
2011-12-05 21:32:37 +00:00
|
|
|
|
2012-11-28 02:06:31 +00:00
|
|
|
# disabled because OGR prints an annoying error: ERROR 1: Invalid Point object. Missing 'coordinates' member.
|
|
|
|
#def test_handling_of_null_features():
|
2013-06-13 20:34:34 +00:00
|
|
|
# ds = mapnik.Ogr(file='../data/json/null_feature.geojson',layer_by_index=0)
|
2012-11-28 02:06:31 +00:00
|
|
|
# fs = ds.all_features()
|
|
|
|
# eq_(len(fs),1)
|
2011-12-05 21:32:37 +00:00
|
|
|
|
2014-08-12 18:44:34 +00:00
|
|
|
# OGR plugin extent parameter
|
|
|
|
def test_ogr_extent_parameter():
|
|
|
|
ds = mapnik.Ogr(file='../data/shp/world_merc.shp',layer_by_index=0,extent='-1,-1,1,1')
|
|
|
|
e = ds.envelope()
|
|
|
|
eq_(e.minx,-1)
|
|
|
|
eq_(e.miny,-1)
|
|
|
|
eq_(e.maxx,1)
|
|
|
|
eq_(e.maxy,1)
|
|
|
|
|
2014-08-16 02:48:27 +00:00
|
|
|
def test_ogr_reading_gpx_waypoint():
|
|
|
|
ds = mapnik.Ogr(file='../data/gpx/empty.gpx',layer='waypoints')
|
|
|
|
e = ds.envelope()
|
|
|
|
eq_(e.minx,-122)
|
|
|
|
eq_(e.miny,48)
|
|
|
|
eq_(e.maxx,-122)
|
|
|
|
eq_(e.maxy,48)
|
|
|
|
|
|
|
|
def test_ogr_empty_data_should_not_throw():
|
|
|
|
default_logging_severity = mapnik.logger.get_severity()
|
|
|
|
mapnik.logger.set_severity(mapnik.severity_type.None)
|
|
|
|
# use logger to silence expected warnings
|
|
|
|
for layer in ['routes', 'tracks', 'route_points', 'track_points']:
|
|
|
|
ds = mapnik.Ogr(file='../data/gpx/empty.gpx',layer=layer)
|
|
|
|
e = ds.envelope()
|
|
|
|
eq_(e.minx,0)
|
|
|
|
eq_(e.miny,0)
|
|
|
|
eq_(e.maxx,0)
|
|
|
|
eq_(e.maxy,0)
|
|
|
|
mapnik.logger.set_severity(default_logging_severity)
|
|
|
|
|
|
|
|
# disabled because OGR prints an annoying error: ERROR 1: Invalid Point object. Missing 'coordinates' member.
|
|
|
|
#def test_handling_of_null_features():
|
|
|
|
# ds = mapnik.Ogr(file='../data/json/null_feature.geojson',layer_by_index=0)
|
|
|
|
# fs = ds.all_features()
|
|
|
|
# eq_(len(fs),1)
|
|
|
|
|
2011-12-05 21:32:37 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
setup()
|
2014-07-14 16:34:20 +00:00
|
|
|
exit(run_all(eval(x) for x in dir() if x.startswith("test_")))
|