iut-ibk / DynaMind-ToolBox

DynaMind-ToolBox
www.dance4water.org
GNU General Public License v2.0
9 stars 6 forks source link

Python GDAL Example Module #246

Closed csaf4370 closed 10 years ago

csaf4370 commented 10 years ago

Hi,

I tried different things but i couldn't get a simple Python module running. Maybe the problem is on my end, but i share the code here, also as a reference. I also get the following error when executing dynamind:

ERROR 1: Failed to lookup UOM CODE 0

I made 2 modules one writing and one reading.

from osgeo import ogr
from pydynamind import *

class CreateGDALComponentsMartinContainer(Module):
        def __init__(self):
            Module.__init__(self)
            self.setIsGDALModule(True)

        def init(self):
            self.__ress = ViewContainer("testContainer", NODE, WRITE)
            self.__ress.addAttribute("tertiary", Attribute.INT, WRITE)
            views = []
            views.append(self.__ress)
            self.registerViewContainers(views)

        def run(self):
            f = self.__ress.create_feature()
            point = ogr.Geometry(ogr.wkbPoint)
            point.AddPoint(0,0)
            f.SetGeometry(point)
            f.SetField("tertiary", 10)

This is the writing module and here is the reading one:

from osgeo import ogr
from pydynamind import *

class ReadGDALComponentsMartinContainer(Module):
        def __init__(self):
            Module.__init__(self)
            self.setIsGDALModule(True)

        def init(self):
            self.__b28 = ViewContainer("testContainer", NODE, READ)
            self.__b28.addAttribute("tertiary", Attribute.INT, READ)
            views = []
            views.append(self.__b28)
            self.registerViewContainers(views)

        def run(self):
            self.__b28.reset_reading()
            for testContainer in self.__b28:
                print testContainer.GetFieldAsInteger("tertiary")

            """
            ERROR    Di. Okt 14 15:09:09 2014|  Traceback (most recent call last):
            ERROR    Di. Okt 14 15:09:09 2014|    File "/home/marcom/work/CRC/CRC-GH/DynaMind-ToolBox/DynaMind-CRC/read_test_data_martinContainer.py", line 22, in run
            ERROR    Di. Okt 14 15:09:09 2014|       self.__b28.reset_reading()
            ERROR    Di. Okt 14 15:09:09 2014|    File "/home/marcom/work/CRC/CRC-GH/DynaMind-ToolBox/build/output/pydynamind.py", line 5698, in reset_reading
            ERROR    Di. Okt 14 15:09:09 2014|       self.register_layer()
            ERROR    Di. Okt 14 15:09:09 2014|    File "/home/marcom/work/CRC/CRC-GH/DynaMind-ToolBox/build/output/pydynamind.py", line 5675, in register_layer
            ERROR    Di. Okt 14 15:09:09 2014|       self.__ogr_layer = self.__ds.GetLayerByName(table_name)
            ERROR    Di. Okt 14 15:09:09 2014|  AttributeError :  'NoneType' object has no attribute 'GetLayerByName'
            """

could you please point me in the right direction or create a simple python module. Thanks a lot.

christianurich commented 10 years ago

Hi,

the problem seems to be that no geometry has bee written in the first model. You probably saw an error

ERROR 1: sqlite3_step() failed:
  testcontainer.GEOMETRY violates Geometry constraint [geom-type or SRID not allowed] (19)
ERROR 1: COMMIT transaction failed: cannot commit - no transaction is active

The problem is the line

point.AddPoint(0,0)

change it to

point.SetPoint_2D(0, 1, 1)

this should work (at my system it does)

The first error

ERROR 1: Failed to lookup UOM CODE 0 

stems form an unset EPSG code (coordinate system). I still need to implement that in the save file and GUI (see #247)

csaf4370 commented 10 years ago

Thanks now it's working fine with COMPONENT as well as with NODE with setting the geometry.