воскресенье, 25 апреля 2010 г.

Скрипты для mapserver: mapinfo.py и mapdraw.py

Скрипты давно уже выложены, но приведу еще и здесь, для гарантии, что не потеряются. Реализация на питоне, поскольку именно в питоне лучшая поддержка модуля mapscript.

mapinfo.py
#!/usr/bin/python
# This program comes with ABSOLUTELY NO WARRANTY.  This is free software, and
# you are welcome to redistribute it under certain conditions.  
# See the GNU General Public Licence for details.
# Copyright (C) 2007, Alexey Pechnikov

import sys
import mapscript 

def Usage():
    print 'Usage: map.py [-extent    ] -conf  [-point  ] [-width ] [-height ]'
    sys.exit(1)

in_file  = None
extent   = None
point    = None
width    = None
height   = None

args = sys.argv
arg = args.pop(0)
while len(args) > 0:
    arg = args.pop(0)

    if arg == '-extent':
        if len(args) < 3:
            Usage()
        else:
            minx = float(args.pop(0))
            miny = float(args.pop(0))
            maxx = float(args.pop(0))
            maxy = float(args.pop(0))
            extent = 1

    elif arg == '-point':
        if len(args) < 2:
            Usage()
        else:
            point_x = float(args.pop(0))
            point_y = float(args.pop(0))
            point = 1

    elif arg == '-width':
        if len(args) < 1:
            Usage()
        else:
            width_px = int(args.pop(0))
            width = 1

    elif arg == '-height':
        if len(args) < 1:
            Usage()
        else:
            height_px = int(args.pop(0))
            height = 1

    elif arg == '-conf':
        if len(args) < 1:
            Usage()
        else:
            in_file = args.pop(0)

    else:
        Usage()

if in_file is None:
    Usage()

mf = mapscript.mapObj(in_file) 

if not extent is None:
    mf.extent.minx = minx
    mf.extent.miny = miny
    mf.extent.maxx = maxx
    mf.extent.maxy = maxy

if not width is None:
    mf.width = width_px

if not height is None:
    mf.height = height_px
    
if not point is None:
    point_geo = mapscript.pointObj();
    point_geo.setXY (point_x, point_y);
    mf.queryByPoint (point_geo, mapscript.MS_SINGLE, 5);

for i in range(mf.numlayers):
 layer = mf.getLayer(i)
 numResults = layer.getNumResults()
 if numResults > 0:
  layer.open()
  results = layer.getResults()
  for j in range(numResults):
   result = results.getResult(j)
   shape = layer.getFeature(result.shapeindex, result.tileindex)
   numItems = layer.numitems
   print 'array set ident_layer { map_name {',mf.name, '} layer_name {',layer.name,'} ', \
    'layer_title {', layer.getMetaData('LYRNAME'), '} }'
   print 'array set ident_fields { '
   for k in range(numItems):
    print '{',layer.getItem (k),'}'
    print '{',shape.getValue(k),'}'
   print '}'

  layer.close()

mapdraw.py
#!/usr/bin/python
# This program comes with ABSOLUTELY NO WARRANTY.  This is free software, and
# you are welcome to redistribute it under certain conditions.  
# See the GNU General Public Licence for details.
# Copyright (C) 2007, Alexey Pechnikov

import sys
import mapscript 

def Usage():
    print 'Usage: map.py [-extent    ] -conf  -image  [-point  ] [-width ] [-height ]'
    sys.exit(1)

in_file  = None
out_file = None
extent   = None
point    = None
width    = None
height   = None

args = sys.argv
arg = args.pop(0)
while len(args) > 0:
    arg = args.pop(0)

    if arg == '-extent':
        if len(args) < 3:
            Usage()
        else:
            minx = float(args.pop(0))
            miny = float(args.pop(0))
            maxx = float(args.pop(0))
            maxy = float(args.pop(0))
            extent = 1

    elif arg == '-point':
        if len(args) < 2:
            Usage()
        else:
            point_x = float(args.pop(0))
            point_y = float(args.pop(0))
            point = 1

    elif arg == '-width':
        if len(args) < 1:
            Usage()
        else:
            width_px = int(args.pop(0))
            width = 1

    elif arg == '-height':
        if len(args) < 1:
            Usage()
        else:
            height_px = int(args.pop(0))
            height = 1

    elif arg == '-image':
        if len(args) < 1:
            Usage()
        else:
            out_file = args.pop(0)

    elif arg == '-conf':
        if len(args) < 1:
            Usage()
        else:
            in_file = args.pop(0)

    else:
        Usage()

if in_file is None:
    Usage()

if out_file is None:
    Usage()

mf = mapscript.mapObj(in_file) 

if not extent is None:
    mf.extent.minx = minx
    mf.extent.miny = miny
    mf.extent.maxx = maxx
    mf.extent.maxy = maxy

if not width is None:
    mf.width = width_px

if not height is None:
    mf.height = height_px
    
if not point is None:
    point_geo = mapscript.pointObj();
    point_geo.setXY (point_x, point_y);
    mf.queryByPoint (point_geo, mapscript.MS_SINGLE, -1);

img = mf.drawQuery() 
img.save(out_file) 

Комментариев нет:


(C) Alexey Pechnikov aka MBG, mobigroup.ru