Coverage for gws-app/gws/lib/mapserver/__init__.py: 100%

1 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-05 13:35 +0200

1"""MapServer support. 

2 

3Creates and renders MapServer maps dynamically, through the ``mapscript`` Python bindings. 

4 

5To render a map, create a map object with ``new_map``, optionally from a mapfile string, 

6add layers to it with ``Map.add_layer`` (from ``gws.MapServerLayerOptions``) or 

7``Map.add_layer_from_config`` (from a mapfile ``LAYER`` block), and call ``Map.draw``, 

8which renders a transparent PNG for the given bounds and size and returns it as a 

9``gws.Image``. ``Map.to_string`` returns the resulting mapfile, which helps with debugging. 

10 

11Layer options support raster files and tile indexes, PostGIS connections, SLD styling 

12and a part of the GWS style values (geometry and label styles, markers and icons). 

13 

14Submodules: 

15 

16- ``core``: the ``Map`` wrapper and ``new_map``. 

17- ``live_config``: a standalone development tool, not used by the application. It runs an 

18 HTTP server with a page to edit a mapfile and render it live (see its ``README.md``). 

19 

20Reference: 

21 - https://mapserver.org/documentation.html 

22 

23Example:: 

24 

25 import gws 

26 import gws.lib.mapserver as ms 

27 

28 # create a new map 

29 map = ms.new_map() 

30 

31 # add a raster layer from an image file 

32 map.add_layer( 

33 gws.MapServerLayerOptions( 

34 type=gws.MapServerLayerType.raster, 

35 path='/path/to/image.tif', 

36 crs=gws.lib.crs.WEBMERCATOR, 

37 ) 

38 ) 

39 

40 # add a layer using a configuration string 

41 map.add_layer_from_config(''' 

42 LAYER 

43 TYPE LINE 

44 STATUS ON 

45 FEATURE 

46 POINTS 

47 751539 6669003 

48 751539 6672326 

49 755559 6672326 

50 END 

51 END 

52 CLASS 

53 STYLE 

54 COLOR 0 255 0 

55 WIDTH 5 

56 END 

57 END 

58 END 

59 ''') 

60 

61 # draw the map into an Image object 

62 img = map.draw( 

63 bounds=gws.Bounds( 

64 extent=[738040, 6653804, 765743, 6683686], 

65 crs=gws.lib.crs.WEBMERCATOR, 

66 ), 

67 size=(800, 600), 

68 ) 

69 

70 # save the image to a file 

71 img.to_path('/path/to/output.png') 

72""" 

73 

74from .core import version, Error, new_map, Map