ODM2 / ODM2PythonAPI

A set of Python functions that provides data read/write access to an ODM2 database by leveraging SQLAlchemy.
http://odm2.github.io/ODM2PythonAPI/
BSD 3-Clause "New" or "Revised" License
4 stars 13 forks source link

Query Sampling Feature Always Returns Nonetype #3

Open Castronova opened 9 years ago

Castronova commented 9 years ago

When querying a SamplingFeature (e.g. getSamplingFeatureById, getSamplingFeatureByGeometry), None is always returned. When querying all SamplingFeatures TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' is raised. See sample code below to replicate this error:

from osgeo import gdal, ogr
point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(1198054.34, 648493.09)

samplingFeatureID = self.insert_sampling_feature(type='site',geometryType=point.GetGeometryName(), WKTgeometry=point.ExportToWkt())

# Querying SamplingFeatures Fails?!
# However, querying the database directly or with sqlite3 works fine.
samplingfeature = self.read.getSamplingFeatureByGeometry(geom.wkt)
test = self.read.getSamplingFeatureById(samplingFeatureID)

# raises an exception that is not caught
all = self.read.getSamplingFeatures()

### Function to insert sampling feature geometries
def insert_sampling_feature(self, type='site',code='',name=None,description=None,geometryType=None,elevation=None,elevationDatum=None,WKTgeometry=None):
        '''
        Inserts a sampling feature.  This function was created to support the insertion of Geometry object since this
        functionality is currently lacking from the ODM2PythonAPI.
        :param type: Type of sampling feature.  Must match FeatureTypeCV, e.g. "site"
        :param name: Name of sampling feature (optional)
        :param description: Description of sampling feature (optional)
        :param geometryType: String representation of the geometry type, e.g. Polygon, Point, etc.
        :param elevation: Elevation of the sampling feature (float)
        :param elevationDatum: String representation of the spatial datum used for the elevation
        :param geometry: Geometry of the sampling feature (Shapely Geometry object)
        :return: ID of the sampling feature which was inserted into the database
        '''
        UUID=str(uuid.uuid4())
        FeatureTypeCV=type
        FeatureCode = code
        FeatureName=name
        FeatureDescription=description
        FeatureGeoTypeCV=geometryType
        FeatureGeometry=WKTgeometry
        Elevation=elevation
        ElevationDatumCV=elevationDatum

        # get the last record index
        res = self.spatialDb.execute('SELECT SamplingFeatureID FROM SamplingFeatures ORDER BY SamplingFeatureID DESC LIMIT 1').fetchall()
        ID = res[0][0]  # get the last id value
        ID += 1 # increment the last id

        values = [ID,UUID,FeatureTypeCV,FeatureCode,FeatureName,FeatureDescription, FeatureGeoTypeCV, FeatureGeometry, Elevation,ElevationDatumCV]
        self.spatialDb.execute('INSERT INTO SamplingFeatures VALUES (?, ?, ?, ?, ?, ?, ?, geomFromText(?), ?, ?)'
                               ,values)

        self.spatial_connection.commit()

        pts = self.spatial_connection.execute('SELECT ST_AsText(FeatureGeometry) from SamplingFeatures').fetchall()

        return ID
sreeder commented 9 years ago

@Castronova you were trying to connect to a sqlite database right?