Extracting x,y,z values from a postgis linestring to python using ogr

المشرف العام

Administrator
طاقم الإدارة
I am trying to load a postgis linestring into python (using ST_DumpPoints) and extracting x,y,z coordinates, which is working but extremely slow..and if i mean slow, i really mean it (yes, it takes about 5 seconds per row, so imagine how long this is for around 5000 features).Funnily enough, if i read the same from a points layer (it is originally taken from a gpx-file, so it has track points and a track), the same amount of points is done in a jiffy. I have no idea where the performance loss is coming from and how to resolve it. I rely on this linestring though to calculate a comparing sample. Is it known that linestrings are not be meant for such kind of processing?

The code is as follows:

from osgeo import ogrimport numpysource = ogr.Open("PG:host=localhost dbname=tracks user=anush")if source is None: print "connection was unsuccessful"else: gFC = source.ExecuteSQL("SELECT st_numpoints(wkb_geometry) from w_130401_aster_comp.w_130401_aster_comp;") gFCi =(gFC.GetFeature(0)).GetField(0) tP = source.ExecuteSQL("SELECT (ST_DumpPoints(wkb_geometry)).path as path, ST_X(st_astext((ST_DumpPoints(wkb_geometry)).geom)) as X, ST_Y(st_astext((ST_DumpPoints(wkb_geometry)).geom)) as Y, ST_Z(st_astext((ST_DumpPoints(wkb_geometry)).geom)) as Z from w_130401_aster_comp.w_130401_aster_comp;") ele_comp = [] for i in range(1,gFCi): f = tP.GetFeature(i) z = f.GetFieldAsDouble(3) ele_comp.append(z)If i use the track_points alternative, the results are collectable pretty much right away, i.e.

from osgeo import ogrsource = ogr.Open("PG:host=localhost dbname=tracks user=postgres")if source is None: print "connection was unsuccessful"else: data = source.ExecuteSQL("SELECT ogc_fid, the_geom, ele from w_130401.track_points") ele = {} for i in range(1,data.GetFeatureCount()): f = data.GetFeature(i) coords = f.GetGeometryRef() x = coords.GetX(); y = coords.GetY()Is this not bizarre? Another really strange thing is that for the linestring i cannot use an ogr.GetFeatureCount() which returns 0, so i have to pre-run a st_numpoints to initiate the for loop (which returns the number of vertices as integer).

Can anyone help me out on what is going on here? It would be great if someone could help.Many thanks in advance, Arne



أكثر...
 
أعلى