Python struct and mapscript saveQuery

Ryan, Adam ARyan at CO.LINN.OR.US
Thu Nov 18 11:33:29 PST 2004


Oops...I meant to post this to the masses...
Adam

> -----Original Message-----
> From: Sean Gillies [mailto:sgillies at FRII.COM]
> Sent: Wednesday, November 17, 2004 7:42 PM
> To: MAPSERVER-USERS at LISTS.UMN.EDU
> Subject: Re: [UMN_MAPSERVER-USERS] Python struct and
> mapscript saveQuery
>
>
> 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

Sean,

Thanks for taking a look at this for me.  I'm not sure if the command lines
you sent were chronological, but if so, the first three integers of a query
file are the number of layers in the query file, the index of the first one,
and the number of records saved in the first one.  For your query file, that
may indeed be 1,1,1.  It's the class index values that I'm having trouble
with.

Here is the routine I've got to test this:

#--------------

import mapscript, struct

m = mapscript.mapObj("C:/ryan/imap/website/Project1/maps/bas.map")
l = m.getLayerByName("Taxlots")
l.queryByRect(m,mapscript.rectObj(7550000,300000,7555000,310000))

m.saveQuery("foo.qy")
qh = open("foo.qy","rb")

n = struct.unpack("i",qh.read(struct.calcsize("i")))[0]
print "numlayers:" + str(n)

for l in range(n):
    lHead =  struct.unpack("ii4d",qh.read(struct.calcsize("ii4d")))
    print "index:" + str(lHead[0])
    print "numrecs:" + str(lHead[1])
    print "minx:" + str(lHead[2])
    print "miny:" + str(lHead[3])
    print "maxx:" + str(lHead[4])
    print "maxy:" + str(lHead[5])

    for r in range(lHead[1]):
        tup = struct.unpack("iib0i",qh.read(struct.calcsize("iib0i")))
        print str(tup[0]) + "," + str(tup[1]) + "," + str(tup[2])

qh.close()

#----------------

And here are my results:

numlayers:1
index:3
numrecs:14
minx:7546410.49461
miny:298868.944762
maxx:7558144.86958
maxy:310504.828106
1017,-1,1
1018,-1,-1
36500,-1,-1
36537,-1,1
37081,-1,1
37096,-1,1
37454,-1,2
37494,-1,1
37496,-1,1
37657,-1,1
37766,-1,-1
37792,-1,1
37841,-1,0
37846,-1,0

And here are my results if I change the format in the third to last line
from "iib0i" to "iii":

numlayers:1
index:3
numrecs:14
minx:7546410.49461
miny:298868.944762
maxx:7558144.86958
maxy:310504.828106
1017,-1,1
1018,-1,255
36500,-1,255
36537,-1,1
37081,-1,1
37096,-1,1
37454,-1,2
37494,-1,1
37496,-1,1
37657,-1,1
37766,-1,1668507135
37792,-1,1869571841
37841,-1,809329664
37846,-1,892812544

And I'm doing all this on one machine, a Windows XP box, Intel processor.

What do you make of this?

Cheers,

Adam



More information about the MapServer-users mailing list