🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.gis
Article #12456

metoda ExecuteSQL() w OGR

#12456
From: geowrw
Date: Fri, 03 Jun 2011 21:51
127 lines
6604 bytes
Witam,

Pytanie dotyczy OGR w Python.

Po utworzeniu obiektu źródła danych z pliku SHP próbuję pobrać i
przetwarzać część danych. Korzystam z metody ExecuteSQL(). Zauważyłem,
że zwracany jest zawsze podzbiór liczebnie odpowiadający spodziewanemu
wynikowi lecz zawierający pierwsze rekordy ze zbioru: jeżeli zapytanie
ma zwrócić 3 rekordy, zwraca 3 ale nie te które wynikają z klauzuli
WHERE ale pierwsze trzy; jeżeli pięć - zwracane jest pierwszych 5.

Przykład na danych z http://www.gis.usu.edu/~chrisg/python/2009/ Week
1 (Data), tam jest zbiór sites.shp. (tak przy okazji bardzo polecam
ten tutorial!)

A oto kod:

>>> import os
>>> from osgeo import ogr
>>> drv = ogr.GetDriverByName('ESRI Shapefile')
>>> dsrc = drv.Open('sites.shp',0)
>>> lyr = dsrc.GetLayer(0)
>>> lyr
<osgeo.ogr.Layer; proxy of <Swig Object of type 'OGRLayerShadow *' at
0x014BB120> >
>>> lyr.GetFeatureCount()
42
>>> for i in range(0,lyr.GetFeatureCount()):
	f = lyr.GetFeature(i)
	[i, f.GetField('id'), f.GetField('cover'),
f.geometry().GetX(),f.geometry().GetY()]


[0, 1, 'shrubs', 455552.41836086405, 4641822.053684879]
[1, 2, 'trees', 449959.8408513342, 4633802.508576875]
[2, 3, 'rocks', 441201.6534307497, 4619029.662325286]
[3, 4, 'grass', 468214.85800508264, 4608688.669949174]
[4, 5, 'shrubs', 456713.1419949174, 4617974.459021601]
[5, 6, 'trees', 472330.15088945365, 4619873.824968234]
[6, 7, 'bare', 456396.58100381197, 4627576.8090851335]
[7, 8, 'bare', 480033.13500635326, 4628104.410736976]
[8, 9, 'trees', 477395.12674714107, 4633169.386594663]
[9, 10, 'grass', 491429.33068615, 4635701.874523507]
[10, 11, 'grass', 483093.22458703944, 4642560.695997459]
[11, 12, 'rocks', 490690.68837357056, 4646781.509212199]
[12, 13, 'grass', 488580.2817662008, 4629581.695362135]
[13, 14, 'trees', 488791.32242693775, 4616286.133735705]
[14, 15, 'shrubs', 482671.14326556545, 4603307.133100381]
[15, 16, 'bare', 470114.2239517154, 4603518.173761118]
[16, 17, 'grass', 467898.29701397714, 4597292.474269377]
[17, 18, 'bare', 473068.79320203303, 4592966.14072427]
[18, 19, 'trees', 479716.5740152478, 4591699.896759847]
[19, 20, 'trees', 485098.1108640407, 4596448.31162643]
[20, 21, 'shrubs', 478766.8910419314, 4601091.206162643]
[21, 22, 'trees', 474018.47617534944, 4603518.173761118]
[22, 23, 'rocks', 457029.7029860229, 4603412.65343075]
[23, 24, 'grass', 443312.06003811944, 4599613.921537484]
[24, 25, 'rocks', 435503.55559085135, 4606050.661689962]
[25, 26, 'trees', 432548.9863405337, 4612276.361181702]
[26, 27, 'shrubs', 445000.3853240153, 4611432.198538755]
[27, 28, 'grass', 456607.62166454893, 4611221.157878018]
[28, 29, 'bare', 451437.12547649303, 4619451.74364676]
[29, 30, 'rocks', 445422.4666454892, 4628948.573379924]
[30, 31, 'grass', 440779.57210927573, 4638023.3217916135]
[31, 32, 'rocks', 441096.13310038124, 4647836.712515883]
[32, 33, 'shrubs', 448377.03589580685, 4653007.208703939]
[33, 34, 'trees', 458190.4266200763, 4652690.647712833]
[34, 35, 'bare', 447110.79193138506, 4646148.387229987]
[35, 36, 'trees', 462938.8414866582, 4634541.1508894535]
[36, 37, 'grass', 466209.97172808135, 4625782.963468869]
[37, 38, 'water', 470536.3052731893, 4639817.167407878]
[38, 39, 'water', 470958.3865946633, 4651635.444409149]
[39, 40, 'grass', 474651.5981575604, 4623039.434879288]
[40, 41, 'trees', 428117.1324650572, 4641083.4113723]
[41, 42, 'grass', 443628.62102922495, 4593493.742376111]

>>> a = dsrc.ExecuteSQL("SELECT * FROM sites WHERE cover = 'water'") # są dwa takie rekordy
>>> for i in range(0,a.GetFeatureCount()):
	f = lyr.GetFeature(i)
	[i, f.GetField('id'), f.GetField('cover'),
f.geometry().GetX(),f.geometry().GetY()]


[0, 1, 'shrubs', 455552.41836086405, 4641822.053684879]
[1, 2, 'trees', 449959.8408513342, 4633802.508576875]

#Jak widać nie wybiera 'water'!

>>> a = dsrc.ExecuteSQL("SELECT * FROM sites WHERE cover = 'bare'") # takich rekordów jest 6
>>>
>>> for i in range(0,a.GetFeatureCount()):
	f = lyr.GetFeature(i)
	[i, f.GetField('id'), f.GetField('cover'),
f.geometry().GetX(),f.geometry().GetY()]


[0, 1, 'shrubs', 455552.41836086405, 4641822.053684879]
[1, 2, 'trees', 449959.8408513342, 4633802.508576875]
[2, 3, 'rocks', 441201.6534307497, 4619029.662325286]
[3, 4, 'grass', 468214.85800508264, 4608688.669949174]
[4, 5, 'shrubs', 456713.1419949174, 4617974.459021601]
[5, 6, 'trees', 472330.15088945365, 4619873.824968234]

# I znowu: ilość sie zgadza, ale wynik bledny!

>>> a = dsrc.ExecuteSQL("SELECT * FROM sites WHERE id > 35") # siedem ostatnich rekordów
>>> for i in range(0,a.GetFeatureCount()):
	f = lyr.GetFeature(i)
	[i, f.GetField('id'), f.GetField('cover'),
f.geometry().GetX(),f.geometry().GetY()]


[0, 1, 'shrubs', 455552.41836086405, 4641822.053684879]
[1, 2, 'trees', 449959.8408513342, 4633802.508576875]
[2, 3, 'rocks', 441201.6534307497, 4619029.662325286]
[3, 4, 'grass', 468214.85800508264, 4608688.669949174]
[4, 5, 'shrubs', 456713.1419949174, 4617974.459021601]
[5, 6, 'trees', 472330.15088945365, 4619873.824968234]
[6, 7, 'bare', 456396.58100381197, 4627576.8090851335]
>>>

Pytanie: czy jest to błąd biblioteki, czy ja wykorzystuję ją
nieprawidłowo (np. składnia SQL)?
Będę wdzięczny za podpowiedzi!

Pozdrawiam,
Wojtek
Technical Details
Message-ID: <2d43193c-5758-4d63-8a60-2683397cc842@d26g2000prn.googlegroups.com>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.rmf.pl!agh.edu.pl!news.agh.edu.pl!news.onet.pl!news.nask.pl!news.nask.org.pl!news.uni-stuttgart.de!news.belwue.de!news.osn.de!diablo2.news.osn.de!feeder2-2.proxad.net!proxad.net!feeder1-2.proxad.net!74.125.46.80.MISMATCH!postnews.google.com!d26g2000prn.googlegroups.com!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.rmf.pl!agh.edu.pl!news.agh.edu.pl!news.onet.pl!news.nask.pl!news.nask.org.pl!news.uni-stuttgart.de!news.belwue.de!news.osn.de!diablo2.news.osn.de!feeder2-2.proxad.net!proxad.net!feeder1-2.proxad.net!74.125.46.80.MISMATCH!postnews.google.com!d26g2000prn.googlegroups.com!not-for-mail
From: geowrw <geowrw@gmail.com>
Newsgroups: pl.comp.gis
Subject: metoda ExecuteSQL() w OGR
Date: Fri, 3 Jun 2011 14:51:44 -0700 (PDT)
Organization: http://groups.google.com
Lines: 133
Message-ID: <2d43193c-5758-4d63-8a60-2683397cc842@d26g2000prn.googlegroups.com>
NNTP-Posting-Host: 15.203.169.126
Mime-Version: 1.0
X-Trace: posting.google.com 1307137905 10385 127.0.0.1 (3 Jun 2011 21:51:45 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Fri, 3 Jun 2011 21:51:45 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: d26g2000prn.googlegroups.com; posting-host=15.203.169.126; posting-account=hVxSwgoAAACu5092LSSVnzZksc-RZyB2
User-Agent: G2/1.0
X-Google-Web-Client: true
X-Google-Header-Order: HUALESRCNK
X-HTTP-UserAgent: Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1,gzip(gfe)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit