Python struct and mapscript saveQuery

Sean Gillies sgillies at FRII.COM
Wed Nov 17 19:42:16 PST 2004


On Nov 17, 2004, at 6:54 PM, Ryan, Adam wrote:

> Dear List,
>
> This is a picky little question concerning Python struct.pack and
> unpack
> methods, and mapscript saveQuery and loadQuery methods.
>
> I'm using Python Mapscript 4.4.0_beta2 on Windows XP and saving query
> files
> using map.saveQuery().  I subsequently manipulate the saved query file.
> When I read the shape, tile and class index values using
> struct.unpack, the
> only format that seems to work is "iib0i", which means two integers
> and a
> signed char aligned at the end to the requirements of three integers, I
> think.  The issue is the class index value.  Using "iii" or any other
> 12
> byte format gives me bad class index values.
>
> The byte order is not declared in my format string, so it defaults to
> native
> byte order, size and alignment.  Any declaration of byte order doesn't
> work
> with this format.
>
> Here's the snippet that reads the indexes:
>     ...
>     for r in range(nr):
>         tup = struct.unpack("iib0i",f.read(struct.calcsize("iib0i")))
>         print str(tup[0]) + "," + str(tup[1]) + "," + str(tup[2])
>     ...
>
> Can anyone tell me why I can't just use "iii"?  I can write query files
> using struct.pack("iii",... and mapserver will load them without a
> problem.
> Am I doing something wrong or is there something a little freaky in how
> mapserver writes class indexes out to file?
>
> Cheers,
>
> Adam
>

Adam, I can't reproduce your problem on Linux (i686) or with
windows 2000.  Am using Hobu's 4.4.0 beta 1 on the latter, but
there haven't been any recent changes.

Python 2.3.4 (#3, Oct  9 2004, 19:23:21)
[GCC 3.3.3 20040412 (Red Hat Linux 3.3.3-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import mapscript
 >>> import struct
 >>> m = mapscript.mapObj('projects/ms_43/mapserver/tests/test.map')
 >>> l = m.getLayerByName('POLYGON')
 >>> l.template = 'foo'
 >>> l.queryByPoint(m, mapscript.pointObj(0.0, 51.5), 1, 0.0)
 >>> r = l.getResults()
 >>> m.saveQuery('foo.q')
 >>> q = open('foo.q','r').read(12)
 >>> q
'\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00'
 >>> struct.unpack("iii", q)
(1, 1, 1)

You're not by any chance saving the queries from another machine
are you?  In that case you'd have to explicitly deal with endian-ness.
See the end of this doc

   http://docs.python.org/lib/module-struct.html


Sean

--
Sean Gillies
sgillies at frii dot com
http://users.frii.com/sgillies



More information about the MapServer-users mailing list