def test_object_versioning(): """ Exclude whole objects if they were added in a later version of MapServer """ s = """MAP NAME "sample" LAYER TYPE POLYGON COMPOSITE COMPOP "lighten" OPACITY 50 COMPFILTER "blur(10)" END END END""" d = mappyfile.loads(s, include_position=False) v = Validator() errors = v.validate(d, add_comments=True, version=6.0) assert len(errors) == 1 d = mappyfile.loads(s, include_position=False) errors = v.validate(d, add_comments=True, version=7.0) assert len(errors) == 0
def test_adding_layers(): """ Test adding layers to an existing collection """ m = mappyfile.loads("MAP LAYER TYPE POLYGON NAME 'TEST' END END") lyr = mappyfile.loads("LAYER TYPE POLYGON NAME 'TEST' END") assert len(m["layers"]) == 1 m["layers"].insert(0, lyr) assert len(m["layers"]) == 2
def test_adding_layers_to_empty(): """ Test adding layers when the layers object has not yet been created """ m = mappyfile.loads("MAP END") lyr = mappyfile.loads("LAYER TYPE POLYGON NAME 'TEST' END") assert len(m["layers"]) == 0 m["layers"].insert(0, lyr) assert len(m["layers"]) == 1 print(mappyfile.dumps(m))
def test_double_attributes(): s = 'MAP CONFIG "MS_ERRORFILE" "stderr" END' ast = mappyfile.loads(s) pp = PrettyPrinter(indent=0, quote='"', newlinechar=" ") res = pp.pprint(ast) assert (res == 'MAP CONFIG "MS_ERRORFILE" "stderr" END') s = 'MAP CONFIG "MS_ERRORFILE" "stderr" END' ast = mappyfile.loads(s) pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") # expected res = pp.pprint(ast) assert (res == "MAP CONFIG 'MS_ERRORFILE' 'stderr' END")
def test_loads(): s = """MAP NAME "TEST" END""" d = mappyfile.loads(s) assert d["name"] == "TEST" d = mappyfile.loads(s, expand_includes=False) assert d["name"] == "TEST" d = mappyfile.loads(s, include_position=True) assert d["name"] == "TEST" d = mappyfile.loads(s, include_comments=True) assert d["name"] == "TEST"
def test_findall(): s = """ MAP LAYER NAME "Layer1" TYPE POLYGON GROUP "test" END LAYER NAME "Layer2" TYPE POLYGON GROUP "1test" END LAYER NAME "Layer3" TYPE POLYGON GROUP "test2" END LAYER NAME "Layer4" TYPE POLYGON GROUP "test" END END """ d = mappyfile.loads(s) layers = mappyfile.findall(d["layers"], "group", "test") assert len(layers) == 2 assert layers[0]["name"] == "Layer1"
def test_double_error(): s = """MAP NAME "sample" STATUS ON SIZE 600 400 SYMBOLSET "../etc/symbols.txt" EXTENT -180 -90 180 UNITS DD SHAPEPATH "../data" IMAGECOLOR 255 255 256 FONTSET "../etc/fonts.txt" WEB IMAGEPATH "/ms4w/tmp/ms_tmp/" IMAGEURL "/ms_tmp/" END LAYER NAME "global-raster" TYPE RASTER STATUS DEFAULT DATA "bluemarble.gif" END END""" d = mappyfile.loads(s, include_position=True) # print(json.dumps(d, indent=4)) v = Validator() errors = v.validate(d, add_comments=True) # print(json.dumps(d, indent=4)) # print(errors) for e in errors: print(e) assert (len(errors) == 2) print(mappyfile.dumps(d))
def test_findkey(): s = """ MAP LAYER NAME "Layer1" TYPE POLYGON END LAYER NAME "Layer2" TYPE POLYGON CLASS NAME "Class1" COLOR 0 0 -8 END END END """ d = mappyfile.loads(s) pth = ["layers", 1] cmp = mappyfile.findkey(d, *pth) assert cmp["name"] == "Layer2" pth = ["layers", 1, "classes", 0] cmp = mappyfile.findkey(d, *pth) assert cmp["name"] == "Class1"
def __call__(self, map_file: types.Mapfile): backend_file = mappyfile.loads(self.template) backend_file.update(self.map_file_to_dict(map_file)) errors = mappyfile.validate(backend_file) if errors: raise Exception(errors) return mappyfile.dumps(backend_file)
def test_double_error(): s = """MAP NAME "sample" STATUS ON SIZE 600 400 SYMBOLSET "../etc/symbols.txt" EXTENT -180 -90 180 UNITS DD SHAPEPATH "../data" IMAGECOLOR 255 255 256 FONTSET "../etc/fonts.txt" WEB IMAGEPATH "/ms4w/tmp/ms_tmp/" IMAGEURL "/ms_tmp/" END LAYER NAME "global-raster" TYPE RASTER STATUS DEFAULT DATA "bluemarble.gif" END END""" d = mappyfile.loads(s, include_position=True) # print(json.dumps(d, indent=4)) v = Validator() errors = v.validate(d, add_comments=True) # print(json.dumps(d, indent=4)) # print(errors) for e in errors: print(e) assert(len(errors) == 2) print(mappyfile.dumps(d))
def test_format_list(): s = """ CLASS STYLE COLOR 173 216 230 END STYLE OUTLINECOLOR 2 2 2 WIDTH 1 LINECAP BUTT PATTERN 5 5 10 10 END END END """ ast = mappyfile.loads(s) pp = PrettyPrinter(indent=0) # expected k = "pattern" lst = [[5, 5, 10, 10]] assert (pp.is_paired_list(k)) r = pp.process_list(k, lst, 0) exp = [u'PATTERN', '5 5\n10 10', u'END'] assert (r == exp)
def test_get_mapfile_projections(): s = """ MAP PROJECTION "init=epsg:23700" END METADATA "wms_srs" "EPSG:4230 EPSG:4210 EPSG:4269 EPSG:4326" "ows_srs" "epsg:3857 EPSG:42101" END LAYER PROJECTION "init=epsg:23700" END METADATA "wfs_srs" "EPSG:4136 EPSG:29902" "ows_srs" "epsg:3857 EPSG:4210" END END END """ m = mappyfile.loads(s) prjs = projections.get_mapfile_projections(m) print(prjs) assert (prjs == [ 'epsg:23700', 'epsg:29902', 'epsg:3857', 'epsg:4136', 'epsg:4210', 'epsg:42101', 'epsg:4230', 'epsg:4269', 'epsg:4326' ])
def test_line_position_mutlilines(): s = """MAP NAME "sample" LAYER NAME "test" STATUS DEFAULT DATA "SELECT GEOM FROM TABLE" TYPE LINEX END END""" p = Parser() ast = p.parse(s) print(ast) d = mappyfile.loads(s, include_position=True) v = Validator() errors = v.validate(d, add_comments=True) print(json.dumps(d, indent=4)) # print(errors) for e in errors: print(e) assert (len(errors) == 1) err = errors[0] assert (err["line"] == 9) assert (err["column"] == 9) print(mappyfile.dumps(d))
def test_update(): s1 = """ MAP LAYER NAME "Layer1" TYPE POLYGON END LAYER NAME "Layer2" TYPE POLYGON CLASS NAME "Class1" COLOR 255 255 0 END END END """ d1 = mappyfile.loads(s1) s2 = """ MAP LAYER NAME "Layer1" TYPE POLYGON END LAYER NAME "LayerNew" TYPE POINT CLASS NAME "Class1" COLOR 0 0 255 END CLASS NAME "Class2" COLOR 0 0 0 END END END """ d2 = mappyfile.loads(s2) d = mappyfile.update(d1, d2) output = mappyfile.dumps(d) print(output)
def test_already_escaped(): """ Don't escape an already escaped quote """ s = r'CLASS EXPRESSION "\"Tignish" END' ast = mappyfile.loads(s) pp = PrettyPrinter(indent=0, quote='"', newlinechar=" ") res = pp.pprint(ast) exp = r'CLASS EXPRESSION "\"Tignish" END' assert (res == exp) s = r"CLASS EXPRESSION '\'Tignish' END" ast = mappyfile.loads(s) pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") res = pp.pprint(ast) exp = r"CLASS EXPRESSION '\'Tignish' END" assert (res == exp)
def test_dumps(): s = '''MAP NAME "TEST" END''' d = mappyfile.loads(s) output = mappyfile.dumps(d, indent=1, spacer="\t", newlinechar=" ") print(output) assert output == 'MAP NAME "TEST" END'
def test_already_escaped(): """ Don't escape an already escaped quote """ s = r'CLASS EXPRESSION "\"Tignish" END' ast = mappyfile.loads(s) pp = PrettyPrinter(indent=0, quote='"', newlinechar=" ") res = pp.pprint(ast) exp = r'CLASS EXPRESSION "\"Tignish" END' assert(res == exp) s = r"CLASS EXPRESSION '\'Tignish' END" ast = mappyfile.loads(s) pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") res = pp.pprint(ast) exp = r"CLASS EXPRESSION '\'Tignish' END" assert(res == exp)
def test_class(): # START OF ADD CLASS EXAMPLE # find a layer using its name layer = mappyfile.find(mapfile["layers"], "name", "sea") new_class_string = """ CLASS NAME 'highlights' STYLE COLOR 107 208 107 OUTLINECOLOR 2 2 2 WIDTH 1 END END """ new_class = mappyfile.loads(new_class_string) layer["classes"].insert(1, new_class) # can insert the new class at any index print(mappyfile.dumps(mapfile)) # END OF ADD CLASS EXAMPLE assert (layer['classes'][1]['name'] == "highlights") # multiple classes # define all classes in a single string TODO - allow on single line classes = """ CLASS NAME 'The World' STYLE OUTLINECOLOR 0 255 0 END END CLASS NAME 'Roads' STYLE OUTLINECOLOR 0 0 0 END END """ # parse the string and replace the existing classes for the layer layer["classes"] = mappyfile.loads(classes) print(mappyfile.dumps(mapfile))
def test_class(): # START OF ADD CLASS EXAMPLE # find a layer using its name layer = mappyfile.find(mapfile["layers"], "name", "sea") new_class_string = """ CLASS NAME 'highlights' STYLE COLOR 107 208 107 OUTLINECOLOR 2 2 2 WIDTH 1 END END """ new_class = mappyfile.loads(new_class_string) layer["classes"].insert(1, new_class) # can insert the new class at any index print(mappyfile.dumps(mapfile)) # END OF ADD CLASS EXAMPLE assert(layer['classes'][1]['name'] == "highlights") # multiple classes # define all classes in a single string TODO - allow on single line classes = """ CLASS NAME 'The World' STYLE OUTLINECOLOR 0 255 0 END END CLASS NAME 'Roads' STYLE OUTLINECOLOR 0 0 0 END END """ # parse the string and replace the existing classes for the layer layer["classes"] = mappyfile.loads(classes) print(mappyfile.dumps(mapfile))
def test_dump(): s = """MAP NAME "TEST" END""" d = mappyfile.loads(s) with tempfile.NamedTemporaryFile(mode="w+", delete=False) as fp: mappyfile.dump(d, fp) with open(fp.name) as fp: d = mappyfile.load(fp) assert d["name"] == "TEST"
def test_write(): s = """MAP NAME "TEST" END""" fn = tempfile.mktemp() d = mappyfile.loads(s) mappyfile.write(d, fn) d = mappyfile.open(fn) assert d["name"] == "TEST" mappyfile.write(d, fn, indent=2, spacer="\t", quote="'", newlinechar="") d = mappyfile.open(fn) assert d["name"] == "TEST"
def test_save(): s = """MAP NAME "TEST" END""" d = mappyfile.loads(s) output_file = os.path.join(tempfile.mkdtemp(), 'test_mapfile.map') mappyfile.save(d, output_file) with open(output_file) as fp: d = mappyfile.load(fp) assert d["name"] == "TEST"
def test_dump_with_end_comments(): s = '''MAP NAME "TEST" END''' d = mappyfile.loads(s) output = mappyfile.dumps(d, indent=1, spacer="\t", newlinechar=" ", end_comment=True) print(output) assert output == 'MAP NAME "TEST" END # MAP'
def test_end_comment(): s = "MAP LAYER TYPE POINT NAME 'Test' END END" ast = mappyfile.loads(s) pp = PrettyPrinter(indent=4, quote='"', newlinechar="\n", end_comment=True) res = pp.pprint(ast) print(res) exp = """MAP LAYER TYPE POINT NAME "Test" END # LAYER END # MAP""" assert res == exp
def test_get_projection(): s = """ MAP PROJECTION "init=epsg:23700" END END """ m = mappyfile.loads(s) prj = projections.get_projection(m) print(prj) assert (prj == "epsg:23700")
def test_metadata_duplicated_key_comment(): txt = """METADATA 'wms_title' 'Title1' # title1 'wms_title' 'Title2' # title2 END""" d = mappyfile.loads(txt, include_comments=True, include_position=False) s = mappyfile.dumps(d, indent=0, quote="'", newlinechar="\n") expected = """METADATA 'wms_title' 'Title2' # title2 END""" assert s == expected
def test_config(): s = """ MAP config "MS_ERRORFILE" "my.log" END """ ast = mappyfile.loads(s) pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") res = pp.pprint(ast) #print(res) exp = u"MAP CONFIG 'MS_ERRORFILE' 'my.log' END" assert (res == exp)
def clean(self): """clean is called during .save() to validate the model fields""" try: parsed_mapfile = mappyfile.loads(self.mapfile) except Exception as e: raise ValidationError("couldn't parse mapfile content: {}".format(e)) else: # print(parsed_mapfile) self.mapfile_json = parsed_mapfile self.mapfile_json['metadata']['wms_title'] = self.group.dataset.name self.mapfile_json['metadata']['wms_timeextent'] = self.group.dataset.timing self.mapfile_json['type'] = self.group.dataset.datatype.upper() if self.group.dataset.srs: self.mapfile_json['projection'] = ["init={}".format(self.group.dataset.srs.epsg.lower())]
def test_unicode(): s = u""" MAP METADATA "ows_title" "éúáí" END END """ ast = mappyfile.loads(s) pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") res = pp.pprint(ast) exp = u"MAP METADATA 'ows_title' 'éúáí' END END" assert(res == exp)
def test_unicode(): s = u""" MAP METADATA "ows_title" "éúáí" END END """ ast = mappyfile.loads(s) pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") res = pp.pprint(ast) exp = u"MAP METADATA 'ows_title' 'éúáí' END END" assert (res == exp)
def test_two_includes(): s = """ MAP INCLUDE "include1.txt" INCLUDE "include2.txt" END """ d = mappyfile.loads(s, expand_includes=False) logging.debug(json.dumps(d, indent=4)) pp = PrettyPrinter(indent=0, newlinechar=" ", quote="'") output = pp.pprint(d) print(output) expected = "MAP INCLUDE 'include1.txt' INCLUDE 'include2.txt' END" assert(output == expected)
def test_metadata_unquoted_comment(): txt = """ METADATA WMS_TITLE "Minor Civil Divisions" # comment END """ d = mappyfile.loads(txt, include_comments=True, include_position=False) s = mappyfile.dumps(d, indent=0, quote='"', newlinechar="\n") expected = """METADATA "wms_title" "Minor Civil Divisions" # comment END""" assert s == expected
def test_single_layer_data(): s = u""" LAYER DATA "dataset1" END """ jsn = mappyfile.loads(s) print(json.dumps(jsn, indent=4)) jsn["data"][0] = "dataset1" print(mappyfile.dumps(jsn)) print(output(s, schema_name="layer")) exp = u"LAYER DATA 'dataset1' END" assert (output(s, schema_name="layer") == exp)
def test_two_includes(): s = """ MAP INCLUDE "include1.txt" INCLUDE "include2.txt" END """ d = mappyfile.loads(s, expand_includes=False) logging.debug(json.dumps(d, indent=4)) pp = PrettyPrinter(indent=0, newlinechar=" ", quote="'") output = pp.pprint(d) print(output) expected = "MAP INCLUDE 'include1.txt' INCLUDE 'include2.txt' END" assert (output == expected)
def test_includes_no_expand(): """ https://github.com/geographika/mappyfile/issues/39 """ s = """ MAP INCLUDE "includes/mymapfile.map" END """ d = mappyfile.loads(s, expand_includes=False) pp = PrettyPrinter(indent=0, newlinechar=" ", quote="'") output = pp.pprint(d) expected = "MAP INCLUDE 'includes/mymapfile.map' END" assert(output == expected)
def test_includes_no_expand(): """ https://github.com/geographika/mappyfile/issues/39 """ s = """ MAP INCLUDE "includes/mymapfile.map" END """ d = mappyfile.loads(s, expand_includes=False) pp = PrettyPrinter(indent=0, newlinechar=" ", quote="'") output = pp.pprint(d) expected = "MAP INCLUDE 'includes/mymapfile.map' END" assert (output == expected)
def test_include_from_string(): """ Check that a file is correctly included when parsing text and that the current working directory is used as the root path for includes - see https://github.com/geographika/mappyfile/issues/55 """ s = """ MAP INCLUDE "tests/samples/include3.map" END """ d = mappyfile.loads(s, expand_includes=True) pp = PrettyPrinter(indent=0, newlinechar=" ", quote="'") output = pp.pprint(d) expected = "MAP NAME 'test' END" assert(output == expected)
def test_nested_quotes(): """ If values contain quotes then make sure they are escaped shp2img -m C:\Temp\msautotest\misc\ogr_vrtconnect.tmp.map """ s = """ LAYER NAME shppoly TYPE polygon CONNECTIONTYPE OGR CONNECTION '<OGRVRTDataSource><OGRVRTLayer name="poly"><SrcDataSource relativeToVRT="0">data/shppoly</SrcDataSource><SrcLayer>poly</SrcLayer></OGRVRTLayer></OGRVRTDataSource>' END""" ast = mappyfile.loads(s) pp = PrettyPrinter(indent=0, quote='"', newlinechar=" ") # expected res = pp.pprint(ast) print(res) exp = 'LAYER NAME "shppoly" TYPE POLYGON CONNECTIONTYPE OGR CONNECTION "<OGRVRTDataSource><OGRVRTLayer name="poly">' \ '<SrcDataSource relativeToVRT="0">data/shppoly</SrcDataSource><SrcLayer>poly</SrcLayer></OGRVRTLayer></OGRVRTDataSource>" END' assert(res == exp)
def test_metadata_comment(): txt = """MAP # metadata comment # on two lines METADATA # another comment 'wms_title' 'Test simple wms' # prop comment END END""" d = mappyfile.loads(txt, include_comments=True, include_position=False) # print(json.dumps(d, indent=4)) s = mappyfile.dumps(d, indent=0, quote="'", newlinechar="\n") # print(s) expected = """MAP # metadata comment # on two lines METADATA 'wms_title' 'Test simple wms' # prop comment # another comment END END""" assert s == expected
def test_layer(): # START OF ADD LAYER EXAMPLE layers = mapfile["layers"] new_layer_string = """ LAYER NAME 'land' TYPE POLYGON DATA '../data/vector/naturalearth/ne_110m_land' CLASS STYLE COLOR 107 208 107 OUTLINECOLOR 2 2 2 WIDTH 1 END END END """ new_layer = mappyfile.loads(new_layer_string) layers.insert(0, new_layer) # can insert the new layer at any index # END OF ADD LAYER EXAMPLE assert(layers[0]['name'] == "land")
def test_findunique(): s = """ LAYER CLASS GROUP "group1" NAME "Class1" COLOR 0 0 -8 END CLASS GROUP "group2" NAME "Class2" COLOR 0 0 0 END CLASS GROUP "group1" NAME "Class3" COLOR 0 0 0 END END """ d = mappyfile.loads(s) groups = mappyfile.findunique(d["classes"], "group") assert groups == ["group1", "group2"]