Pokazywanie postów oznaczonych etykietą py. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą py. Pokaż wszystkie posty

wtorek, 22 października 2013

Updating a field in a Geodatabase

I wanted to update in ArcGIS a point feature class with values representing polygons ID's in which the points are located:



I could do this by means of "Join Data" tool based on spatial location (ArcMap > Table of content > local menu > Joins and Relates > Join...). But this tool creates a new feature class which I do not need.

I could use "Spatial Join" tool from the ToolBox but then also a new feature class  is created ("The output must be unique from the input.").

Maybe there's a way in ArcGIS to just update like I wanted but I couldn't find it (some people say there isn't - see:

So I run SQL Server client and typed:

UPDATE Geobaza_08.dbo.PUNKT_ODWIERT
SET OBSZAR_ID = ob.Obszar_ID
FROM Geobaza_08.dbo.PUNKT_ODWIERT po, Geobaza_08.dbo.OBSZAR_BADAN ob
WHERE po.SHAPE.STIntersects(ob.SHAPE) = 1
;


I refreshed the table and I got what I needed:


I tried to make this in ArcGIS without using pure SQL. So I used Python and Arcpy (there should be an edit session opened before executing the following script):


#create update cursor 
uc = arcpy.da.UpdateCursor("PKTO",("OBSZAR_ID","SHAPE@","OBJECTID")) 
#iterate on points
for pkt in uc: 
     #select a polygon containing the point
     arcpy.SelectLayerByLocation_management("OBSZ","CONTAINS",pkt[1]) 
     #create select cursor from the selection
     obs = arcpy.SearchCursor("OBSZ")   
     #iterate on the select cursor 
     for ob in obs: 
          #write the selected polygon id into point field
          pkt[0] = ob.getValue("Obszar_ID")
          #update row in update cursor
          uc.updateRow(pkt)


Hmm... I rather prefer simple SQL update in the DBMS.

wtorek, 25 września 2012

DatabaseError: Invalid handle! while reading LOB in Python/cx_Oracle


I've spend much time on this:


orcl = cx_Oracle.connect(usrpass+'@'+dbase)
c = orcl.cursor()
c.execute(sq)
dane =  c.fetchall()

orcl.close() # before reading LOB to str

wkt = dane[0][0].read()

And I get: DatabaseError: Invalid handle!

But the following code works, because connection is closed after reading LOB to STR:
 
orcl = cx_Oracle.connect(usrpass+'@'+dbase)
c = orcl.cursor()
c.execute(sq)
dane =  c.fetchall()

wkt = dane[0][0].read()

orcl.close() # after reading LOB to str

[http://stackoverflow.com/questions/8646968/how-do-i-read-cx-oracle-lob-data-in-python/12590977#12590977]

środa, 2 marca 2011

Dictionary iteration - ValueError: too many values to unpack

If you get the following error trying to iterate on a dictionary:
 
ValueError: too many values to unpack
 
you have forgotten add iteritems() method:
 
for k,v in dict.iteritems():
    print k, v

wtorek, 1 marca 2011

How to select value of SDO_GEOMETRY Object

http://download.oracle.com/docs/cd/E11882_01/appdev.112/e11830/sdo_objrelschema.htm#i1004087

States:

"Oracle Spatial defines the object type SDO_GEOMETRY as:

CREATE TYPE sdo_geometry AS OBJECT (
 SDO_GTYPE NUMBER,
 SDO_SRID NUMBER,
 SDO_POINT SDO_POINT_TYPE,
 SDO_ELEM_INFO SDO_ELEM_INFO_ARRAY,
 SDO_ORDINATES SDO_ORDINATE_ARRAY);
"

Let's assume we have a "TABLE" with SDO_GEOMETRY Type field named "GEOM".
If you want to get the values of certain SDO_GEOMETRY Type field and make query like this:

select geom.SDO_GTYPE from table;

You'll get ORA-00904 Error: "invalid identifier".

Try with table name alias:

select t.geom.SDO_GTYPE from table t;

GEOM.SDO_GTYPE   
----------------------
2003      

poniedziałek, 7 lutego 2011

OGR in Python - an example

Required Python and GDAL.

1. Open Python
2. Import OGR module
3. Set data type
4. Open data source
5. Get layers (here we have only one)
And now we can e.g.:
6. Get layer extension
7. Get an object on the layer
8. Get attribute
9. Get object geometry
10. Get coordinates (point in this case).


from osgeo import ogr #2
driver = ogr.GetDriverByName('ESRI Shapefile') #3
dataSource = driver.Open('plik.shp',0) #4
layer = dataSource.GetLayer() #5
layer.GetExtent() #6
feature = layer.GetFeature(0) #7 (pobieramy obiekt pierwszy)
feature.GetField('ID') #8
geometry = feature.GetGeometryRef() #9
geometry.GetX() #10
geometry.GetY() #10


______________________________________________________________
Src: Geoprocessing with Python using Open Source GIS

[PL: Przykład wykorzystania OGR (GDAL) w Python]

niedziela, 6 lutego 2011

cx_Oracle - how to get SDO_GEOMETRY data

#Let's connect to the database
import cx_Oracle
orcl = cx_Oracle.connect ('scott/tiger@orcl')
c = orcl.cursor()

#we need some spatial data
sql = "SELECT geom FROM geometry_tab WHERE (...)"
res = c.execute(sql)
obj = res.fetchone()[0]

#ok, what do we have?
obj
<cx_Oracle.OBJECT object at 0x0158A4C0>
#if you type dir(obj) you will not get the attributes names - try..
# you MUST know (or read here) the you can use attributes from SDO_GEOMETRY object in ORCL, like:
#'SDO_GTYPE','SDO_SRID','SDO_POINT','SDO_ELEM_INFO','SDO_ORDINATES'

obj.SDO_GTYPE
2003.0

obj.SDO_ELEM_INFO
[1.0, 1003.0, 1.0]

obj.SDO_ORDINATES
[20.623358189, 52.88806347, 20.623203441, 52.887897231000004, 20.623488043000002, 52.887861954, (...) , 52.888026894, 20.623358189, 52.88806347]