Skip to content

Commit 72d93c4

Browse files
author
Stephanie Reeder
committed
Merge pull request #30 from ODM2/sfgeometry_em_1
Sfgeometry em 1, approved merge
2 parents 6bd3bb7 + f55b727 commit 72d93c4

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

odm2api/ODM2/models.py

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
from sqlalchemy import BigInteger, Column, Date, DateTime, Float, ForeignKey, Integer, String, Boolean, BLOB
22
from sqlalchemy.orm import relationship
3-
4-
3+
from sqlalchemy.dialects import postgresql, mysql, sqlite
54
# Should not be importing anything from a specific dialect
65
# from sqlalchemy.dialects.mssql.base import BIT
76

8-
# from apiCustomType import Geometry
9-
10-
117
from geoalchemy import GeometryDDL, GeometryColumn
128
from geoalchemy.geometry import Geometry
13-
#geoalchemy.geometry.Geometry
14-
15-
#from base import modelBase as Base
9+
from shapely import wkb, wkt
1610

1711
from odm2api.base import modelBase
18-
Base = modelBase.Base
12+
# from apiCustomType import Geometry
1913

20-
from sqlalchemy.dialects import postgresql, mysql, sqlite
14+
Base = modelBase.Base
2115

2216
BigIntegerType = BigInteger()
2317
BigIntegerType = BigIntegerType.with_variant(sqlite.INTEGER(), 'sqlite')
2418
BigIntegerType = BigIntegerType.with_variant(postgresql.BIGINT(), 'postgresql')
2519
BigIntegerType = BigIntegerType.with_variant(mysql.BIGINT(), 'mysql')
2620

21+
22+
def is_hex(s):
23+
try:
24+
int(s, base=16)
25+
return True
26+
except ValueError:
27+
return False
28+
29+
2730
################################################################################
2831
# CV
2932
################################################################################
@@ -559,24 +562,40 @@ class SamplingFeatures(Base):
559562
index=True)
560563
Elevation_m = Column('elevation_m', Float(53))
561564
ElevationDatumCV = Column('elevationdatumcv', ForeignKey(CVElevationDatum.Name), index=True)
562-
#FeatureGeometry = Column('featuregeometry', Geometry) # Geoalchemy 2
563-
FeatureGeometry = GeometryColumn('featuregeometry', Geometry) #Geoalchemy 1, #wkb.loads(str(self.FeatureGeometry.geom_wkb)).wkt if self.FeatureGeometry is not None else None
564-
# FeatureGeometry = Column('featuregeometry', BLOB)# custom geometry queries
565-
565+
FeatureGeometry = Column('featuregeometry', Geometry)
566+
# FeatureGeometry = Column('featuregeometry', BLOB) # custom geometry queries
567+
568+
def shape(self):
569+
"""
570+
Method name based on shapely shapely.geometry.shape() function.
571+
Returns a shapely geometry object
572+
:return geomshape:
573+
"""
574+
_FeatureGeometry = self.FeatureGeometry
575+
geomshape = None
576+
if _FeatureGeometry is not None:
577+
if is_hex(_FeatureGeometry.geom_wkb):
578+
# to parse wkb hex string directly
579+
geomshape = wkb.loads(_FeatureGeometry.geom_wkb, hex=True)
580+
# _FeatureGeometry = GeometryColumn('featuregeometry', Geometry)
581+
else:
582+
geomshape = wkt.loads(str(_FeatureGeometry.geom_wkb))
583+
584+
return geomshape
566585

567586
def __repr__(self):
568-
from shapely import wkb
569-
geom = None
570-
if hasattr(self.FeatureGeometry, 'geom_wkt'):
571-
geom = wkb.loads(str(self.FeatureGeometry.geom_wkb)).wkt
587+
geom = self.shape()
588+
if geom is not None:
589+
geomkt = geom.wkt
572590
else:
573-
geom = self.FeatureGeometry
591+
geomkt = None
574592

575593
return "<SamplingFeatures('%s', '%s', '%s', '%s', '%s')>" % (
576594
self.SamplingFeatureCode, self.SamplingFeatureName, self.SamplingFeatureDescription,
577-
self.Elevation_m, geom)
595+
self.Elevation_m, geomkt)
596+
597+
GeometryDDL(SamplingFeatures.__table__) # Geoalchemy1
578598

579-
GeometryDDL(SamplingFeatures.__table__) #Geoalchemy1
580599

581600
class FeatureActions(Base):
582601
__tablename__ = u'featureactions'

odm2api/ODMconnection.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,13 @@ def deleteConnection(self, conn_dict):
185185
# return conn_string
186186

187187
def __buildConnectionString(self, conn_dict):
188-
driver = ""
189-
print "****", conn_dict
188+
# driver = ""
189+
# print "****", conn_dict
190190
if conn_dict['engine'] == 'mssql' and sys.platform != 'win32':
191191
driver = "pyodbc"
192192
#'DRIVER={FreeTDS};DSN=%s;UID=%s;PWD=%s;' % (conn_dict['address'], conn_dict['user'], conn_dict['password'])
193193
quoted = urllib.quote_plus('DRIVER={FreeTDS};DSN=%s;UID=%s;PWD=%s;' % (conn_dict['address'], conn_dict['user'], conn_dict['password']))
194194
conn_string = 'mssql+pyodbc:///?odbc_connect={}'.format(quoted)
195-
196195
else:
197196
if conn_dict['engine'] == 'mssql':
198197
driver = "pyodbc"
@@ -201,8 +200,6 @@ def __buildConnectionString(self, conn_dict):
201200
if "sqlncli11.dll" in os.listdir("C:\\Windows\\System32"):
202201
conn = "%s+%s://%s:%s@%s/%s?driver=SQL+Server+Native+Client+11.0"
203202
self._connection_format = conn
204-
205-
206203
elif conn_dict['engine'] == 'mysql':
207204
driver = "pymysql"
208205
elif conn_dict['engine'] == 'postgresql':
@@ -214,5 +211,5 @@ def __buildConnectionString(self, conn_dict):
214211
conn_dict['engine'], driver, conn_dict['user'], conn_dict['password'], conn_dict['address'],
215212
conn_dict['db'])
216213

217-
print "******", conn_string
214+
# print "******", conn_string
218215
return conn_string

0 commit comments

Comments
 (0)