|
1 | 1 | from sqlalchemy import BigInteger, Column, Date, DateTime, Float, ForeignKey, Integer, String, Boolean, BLOB |
2 | 2 | from sqlalchemy.orm import relationship |
3 | | - |
4 | | - |
| 3 | +from sqlalchemy.dialects import postgresql, mysql, sqlite |
5 | 4 | # Should not be importing anything from a specific dialect |
6 | 5 | # from sqlalchemy.dialects.mssql.base import BIT |
7 | 6 |
|
8 | | -# from apiCustomType import Geometry |
9 | | - |
10 | | - |
11 | 7 | from geoalchemy import GeometryDDL, GeometryColumn |
12 | 8 | from geoalchemy.geometry import Geometry |
13 | | -#geoalchemy.geometry.Geometry |
14 | | - |
15 | | -#from base import modelBase as Base |
| 9 | +from shapely import wkb, wkt |
16 | 10 |
|
17 | 11 | from odm2api.base import modelBase |
18 | | -Base = modelBase.Base |
| 12 | +# from apiCustomType import Geometry |
19 | 13 |
|
20 | | -from sqlalchemy.dialects import postgresql, mysql, sqlite |
| 14 | +Base = modelBase.Base |
21 | 15 |
|
22 | 16 | BigIntegerType = BigInteger() |
23 | 17 | BigIntegerType = BigIntegerType.with_variant(sqlite.INTEGER(), 'sqlite') |
24 | 18 | BigIntegerType = BigIntegerType.with_variant(postgresql.BIGINT(), 'postgresql') |
25 | 19 | BigIntegerType = BigIntegerType.with_variant(mysql.BIGINT(), 'mysql') |
26 | 20 |
|
| 21 | + |
| 22 | +def is_hex(s): |
| 23 | + try: |
| 24 | + int(s, base=16) |
| 25 | + return True |
| 26 | + except ValueError: |
| 27 | + return False |
| 28 | + |
| 29 | + |
27 | 30 | ################################################################################ |
28 | 31 | # CV |
29 | 32 | ################################################################################ |
@@ -559,24 +562,40 @@ class SamplingFeatures(Base): |
559 | 562 | index=True) |
560 | 563 | Elevation_m = Column('elevation_m', Float(53)) |
561 | 564 | 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 |
566 | 585 |
|
567 | 586 | 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 |
572 | 590 | else: |
573 | | - geom = self.FeatureGeometry |
| 591 | + geomkt = None |
574 | 592 |
|
575 | 593 | return "<SamplingFeatures('%s', '%s', '%s', '%s', '%s')>" % ( |
576 | 594 | self.SamplingFeatureCode, self.SamplingFeatureName, self.SamplingFeatureDescription, |
577 | | - self.Elevation_m, geom) |
| 595 | + self.Elevation_m, geomkt) |
| 596 | + |
| 597 | +GeometryDDL(SamplingFeatures.__table__) # Geoalchemy1 |
578 | 598 |
|
579 | | -GeometryDDL(SamplingFeatures.__table__) #Geoalchemy1 |
580 | 599 |
|
581 | 600 | class FeatureActions(Base): |
582 | 601 | __tablename__ = u'featureactions' |
|
0 commit comments