2012-07-26 01:11:51 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2015-02-02 19:31:16 +01:00
|
|
|
from nose.tools import eq_,assert_almost_equal
|
2013-06-03 04:28:24 +02:00
|
|
|
from utilities import execution_path, run_all
|
2012-07-26 01:11:51 +02:00
|
|
|
import os, mapnik
|
2015-02-12 20:37:46 +01:00
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
import simplejson as json
|
2012-07-26 01:11:51 +02:00
|
|
|
|
|
|
|
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 13:53:37 +02:00
|
|
|
if 'geojson' in mapnik.DatasourceCache.plugin_names():
|
2012-07-26 01:11:51 +02:00
|
|
|
|
|
|
|
def test_geojson_init():
|
2013-06-13 22:34:34 +02:00
|
|
|
ds = mapnik.Datasource(type='geojson',file='../data/json/escaped.geojson')
|
2012-09-03 19:01:01 +02:00
|
|
|
e = ds.envelope()
|
2012-07-26 01:11:51 +02:00
|
|
|
assert_almost_equal(e.minx, -81.705583, places=7)
|
|
|
|
assert_almost_equal(e.miny, 41.480573, places=6)
|
|
|
|
assert_almost_equal(e.maxx, -81.705583, places=5)
|
|
|
|
assert_almost_equal(e.maxy, 41.480573, places=3)
|
|
|
|
|
|
|
|
def test_geojson_properties():
|
2013-06-13 22:34:34 +02:00
|
|
|
ds = mapnik.Datasource(type='geojson',file='../data/json/escaped.geojson')
|
2014-07-28 22:07:27 +02:00
|
|
|
f = ds.features_at_point(ds.envelope().center()).features[0]
|
2015-02-04 18:34:53 +01:00
|
|
|
eq_(len(ds.fields()),9)
|
2012-09-03 19:01:01 +02:00
|
|
|
desc = ds.describe()
|
|
|
|
eq_(desc['geometry_type'],mapnik.DataGeometryType.Point)
|
2012-07-26 01:11:51 +02:00
|
|
|
|
2014-07-28 22:07:27 +02:00
|
|
|
eq_(f['name'], u'Test')
|
2012-07-26 01:11:51 +02:00
|
|
|
eq_(f['int'], 1)
|
2014-07-28 22:07:27 +02:00
|
|
|
eq_(f['description'], u'Test: \u005C')
|
|
|
|
eq_(f['spaces'], u'this has spaces')
|
|
|
|
eq_(f['double'], 1.1)
|
2012-07-26 01:11:51 +02:00
|
|
|
eq_(f['boolean'], True)
|
|
|
|
eq_(f['NOM_FR'], u'Qu\xe9bec')
|
|
|
|
eq_(f['NOM_FR'], u'Québec')
|
2015-02-12 20:37:46 +01:00
|
|
|
eq_(f['array'], u'[[[1],["deux"]],[["\\u0442\\u0440\\u0438","four","\\u4e94"]]]')
|
|
|
|
array = json.loads(f['array'])
|
|
|
|
eq_(array,[[[1], [u'deux']], [[u'\u0442\u0440\u0438', u'four', u'\u4e94']]])
|
|
|
|
eq_(f['object'], u'{"value":{"type":"\\u041c\\u0430pni\\u043a","array":[3,0,"x"]}}')
|
|
|
|
object = json.loads(f['object'])
|
|
|
|
eq_(object,{u'value': {u'array': [3, 0, u'x'], u'type': u'\u041c\u0430pni\u043a'}})
|
2012-07-26 01:11:51 +02:00
|
|
|
|
2013-06-13 22:34:34 +02:00
|
|
|
ds = mapnik.Datasource(type='geojson',file='../data/json/escaped.geojson')
|
2012-09-03 19:01:01 +02:00
|
|
|
f = ds.all_features()[0]
|
2015-02-04 18:34:53 +01:00
|
|
|
eq_(len(ds.fields()),9)
|
2012-09-03 19:01:01 +02:00
|
|
|
|
|
|
|
desc = ds.describe()
|
|
|
|
eq_(desc['geometry_type'],mapnik.DataGeometryType.Point)
|
2012-07-26 01:11:51 +02:00
|
|
|
|
|
|
|
eq_(f['name'], u'Test')
|
|
|
|
eq_(f['int'], 1)
|
2014-07-28 22:07:27 +02:00
|
|
|
eq_(f['description'], u'Test: \u005C')
|
|
|
|
eq_(f['spaces'], u'this has spaces')
|
2015-02-12 15:53:29 +01:00
|
|
|
eq_(f['double'], 1.1)
|
|
|
|
eq_(f['boolean'], True)
|
|
|
|
eq_(f['NOM_FR'], u'Qu\xe9bec')
|
|
|
|
eq_(f['NOM_FR'], u'Québec')
|
2015-02-12 20:37:46 +01:00
|
|
|
eq_(f['array'], u'[[[1],["deux"]],[["\\u0442\\u0440\\u0438","four","\\u4e94"]]]')
|
|
|
|
array = json.loads(f['array'])
|
|
|
|
eq_(array,[[[1], [u'deux']], [[u'\u0442\u0440\u0438', u'four', u'\u4e94']]])
|
|
|
|
eq_(f['object'], u'{"value":{"type":"\\u041c\\u0430pni\\u043a","array":[3,0,"x"]}}')
|
|
|
|
object = json.loads(f['object'])
|
|
|
|
eq_(object,{u'value': {u'array': [3, 0, u'x'], u'type': u'\u041c\u0430pni\u043a'}})
|
2015-02-12 15:53:29 +01:00
|
|
|
|
|
|
|
def test_large_geojson_properties():
|
|
|
|
ds = mapnik.Datasource(type='geojson',file='../data/json/escaped.geojson',cache_features = False)
|
|
|
|
f = ds.features_at_point(ds.envelope().center()).features[0]
|
|
|
|
eq_(len(ds.fields()),9)
|
|
|
|
desc = ds.describe()
|
|
|
|
eq_(desc['geometry_type'],mapnik.DataGeometryType.Point)
|
|
|
|
|
|
|
|
eq_(f['name'], u'Test')
|
|
|
|
eq_(f['int'], 1)
|
|
|
|
eq_(f['description'], u'Test: \u005C')
|
|
|
|
eq_(f['spaces'], u'this has spaces')
|
|
|
|
eq_(f['double'], 1.1)
|
|
|
|
eq_(f['boolean'], True)
|
|
|
|
eq_(f['NOM_FR'], u'Qu\xe9bec')
|
|
|
|
eq_(f['NOM_FR'], u'Québec')
|
2015-02-12 20:37:46 +01:00
|
|
|
eq_(f['array'], u'[[[1],["deux"]],[["\\u0442\\u0440\\u0438","four","\\u4e94"]]]')
|
|
|
|
array = json.loads(f['array'])
|
|
|
|
eq_(array,[[[1], [u'deux']], [[u'\u0442\u0440\u0438', u'four', u'\u4e94']]])
|
|
|
|
eq_(f['object'], u'{"value":{"type":"\\u041c\\u0430pni\\u043a","array":[3,0,"x"]}}')
|
|
|
|
object = json.loads(f['object'])
|
|
|
|
eq_(object,{u'value': {u'array': [3, 0, u'x'], u'type': u'\u041c\u0430pni\u043a'}})
|
2015-02-12 15:53:29 +01:00
|
|
|
|
|
|
|
ds = mapnik.Datasource(type='geojson',file='../data/json/escaped.geojson')
|
|
|
|
f = ds.all_features()[0]
|
|
|
|
eq_(len(ds.fields()),9)
|
|
|
|
|
|
|
|
desc = ds.describe()
|
|
|
|
eq_(desc['geometry_type'],mapnik.DataGeometryType.Point)
|
|
|
|
|
|
|
|
eq_(f['name'], u'Test')
|
|
|
|
eq_(f['int'], 1)
|
|
|
|
eq_(f['description'], u'Test: \u005C')
|
|
|
|
eq_(f['spaces'], u'this has spaces')
|
2012-07-26 01:11:51 +02:00
|
|
|
eq_(f['double'], 1.1)
|
|
|
|
eq_(f['boolean'], True)
|
|
|
|
eq_(f['NOM_FR'], u'Qu\xe9bec')
|
|
|
|
eq_(f['NOM_FR'], u'Québec')
|
2015-02-12 20:37:46 +01:00
|
|
|
eq_(f['array'], u'[[[1],["deux"]],[["\\u0442\\u0440\\u0438","four","\\u4e94"]]]')
|
|
|
|
array = json.loads(f['array'])
|
|
|
|
eq_(array,[[[1], [u'deux']], [[u'\u0442\u0440\u0438', u'four', u'\u4e94']]])
|
|
|
|
eq_(f['object'], u'{"value":{"type":"\\u041c\\u0430pni\\u043a","array":[3,0,"x"]}}')
|
|
|
|
object = json.loads(f['object'])
|
|
|
|
eq_(object,{u'value': {u'array': [3, 0, u'x'], u'type': u'\u041c\u0430pni\u043a'}})
|
2012-07-26 01:11:51 +02:00
|
|
|
|
2014-07-29 01:16:10 +02:00
|
|
|
def test_geojson_from_in_memory_string():
|
2014-07-29 03:39:07 +02:00
|
|
|
# will silently fail since it is a geometry and needs to be a featurecollection.
|
|
|
|
#ds = mapnik.Datasource(type='geojson',inline='{"type":"LineString","coordinates":[[0,0],[10,10]]}')
|
|
|
|
# works since it is a featurecollection
|
|
|
|
ds = mapnik.Datasource(type='geojson',inline='{ "type":"FeatureCollection", "features": [ { "type":"Feature", "properties":{"name":"test"}, "geometry": { "type":"LineString","coordinates":[[0,0],[10,10]] } } ]}')
|
|
|
|
eq_(len(ds.fields()),1)
|
2014-07-29 01:16:10 +02:00
|
|
|
f = ds.all_features()[0]
|
|
|
|
desc = ds.describe()
|
2014-07-29 03:39:07 +02:00
|
|
|
eq_(desc['geometry_type'],mapnik.DataGeometryType.LineString)
|
|
|
|
eq_(f['name'], u'test')
|
2014-07-29 01:16:10 +02:00
|
|
|
|
2012-07-26 01:11:51 +02:00
|
|
|
# @raises(RuntimeError)
|
|
|
|
def test_that_nonexistant_query_field_throws(**kwargs):
|
2013-06-13 22:34:34 +02:00
|
|
|
ds = mapnik.Datasource(type='geojson',file='../data/json/escaped.geojson')
|
2015-02-04 18:34:53 +01:00
|
|
|
eq_(len(ds.fields()),9)
|
2012-07-26 01:11:51 +02:00
|
|
|
# TODO - this sorting is messed up
|
2012-07-26 01:37:05 +02:00
|
|
|
#eq_(ds.fields(),['name', 'int', 'double', 'description', 'boolean', 'NOM_FR'])
|
|
|
|
#eq_(ds.field_types(),['str', 'int', 'float', 'str', 'bool', 'str'])
|
2012-07-26 01:11:51 +02:00
|
|
|
# TODO - should geojson plugin throw like others?
|
|
|
|
# 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)
|
|
|
|
|
2014-05-16 21:21:36 +02:00
|
|
|
def test_parsing_feature_collection_with_top_level_properties():
|
|
|
|
ds = mapnik.Datasource(type='geojson',file='../data/json/feature_collection_level_properties.json')
|
|
|
|
f = ds.all_features()[0]
|
|
|
|
|
|
|
|
desc = ds.describe()
|
|
|
|
eq_(desc['geometry_type'],mapnik.DataGeometryType.Point)
|
|
|
|
eq_(f['feat_name'], u'feat_value')
|
|
|
|
|
2012-07-26 01:11:51 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
setup()
|
2014-07-14 18:34:20 +02:00
|
|
|
exit(run_all(eval(x) for x in dir() if x.startswith("test_")))
|