Commit 3e6b670b authored by Lauren Adoram-Kershner's avatar Lauren Adoram-Kershner Committed by GitHub
Browse files

Merge pull request #392 from jlaura/master

Refactors footprint_latlon to geom
parents 1baa6adc 355512a8
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -1684,7 +1684,7 @@ WHERE
        >>> geom = 'LINESTRING(145 10, 145 11, 146 11, 146 10, 145 10)'
        >>> srid = 949900
        >>> outpath = '/scratch/jlaura/fromdb'
        >>> query = f"SELECT * FROM Images WHERE ST_INTERSECTS(footprint_latlon, ST_Polygon(ST_GeomFromText('{geom}'), {srid})) = TRUE"
        >>> query = f"SELECT * FROM Images WHERE ST_INTERSECTS(geom, ST_Polygon(ST_GeomFromText('{geom}'), {srid})) = TRUE"
        >>> ncg = NetworkCandidateGraph.from_remote_database(source_db_config, outpath, query_string=query)
        """

@@ -1733,11 +1733,11 @@ WHERE
        ## Spatial Query
        This example selects those images that intersect a given bounding polygon.  The polygon is
        specified as a Well Known Text LINESTRING with the first and last points being the same.
        The query says, select the footprint_latlon (the bounding polygons in the database) that
        The query says, select the geom (the bounding polygons in the database) that
        intersect the user provided polygon (the LINESTRING) in the given spatial reference system
        (SRID), 949900.

        SELECT * FROM Images WHERE ST_INTERSECTS(footprint_latlon, ST_Polygon(ST_GeomFromText('LINESTRING(159 10, 159 11, 160 11, 160 10, 159 10)'),949900)) = TRUE
        SELECT * FROM Images WHERE ST_INTERSECTS(geom, ST_Polygon(ST_GeomFromText('LINESTRING(159 10, 159 11, 160 11, 160 10, 159 10)'),949900)) = TRUE

        ## Select from a specific orbit
        This example selects those images that are from a particular orbit. In this case,
@@ -1750,7 +1750,7 @@ WHERE
        composite_query = '''WITH i as ({}) SELECT i1.id
        as i1_id,i1.path as i1_path, i2.id as i2_id, i2.path as i2_path
        FROM i  as i1, i as i2
        WHERE ST_INTERSECTS(i1.footprint_latlon, i2.footprint_latlon) = TRUE
        WHERE ST_INTERSECTS(i1.geom, i2.geom) = TRUE
        AND i1.id < i2.id'''.format(query_string)

        session = Session()
+1 −1
Original line number Diff line number Diff line
@@ -524,7 +524,7 @@ class NetworkNode(Node):
            # Create the image
            i = Images(name=kwargs['image_name'],
                       path=kwargs['image_path'],
                       footprint_latlon=fp,
                       geom=fp,
                       keypoints=kps,
                       cameras=cam,
                       serial=self.isis_serial,
+13 −13
Original line number Diff line number Diff line
@@ -188,7 +188,7 @@ class Images(BaseMixin, Base):
    path = Column(String)
    serial = Column(String, unique=True)
    ignore = Column(Boolean, default=False)
    _footprint_latlon = Column("footprint_latlon", Geometry('MultiPolygon', srid=latitudinal_srid, dimension=2, spatial_index=True))
    _geom = Column("geom", Geometry('MultiPolygon', srid=latitudinal_srid, dimension=2, spatial_index=True))
    footprint_bodyfixed = Column(Geometry('MULTIPOLYGON', dimension=2))
    cam_type = Column(String)
    #footprint_bodyfixed = Column(Geometry('POLYGON',dimension=3))
@@ -200,31 +200,31 @@ class Images(BaseMixin, Base):

    def __repr__(self):
        try:
            footprint = to_shape(self.footprint_latlon).__geo_interface__
            footprint = to_shape(self.geom).__geo_interface__
        except:
            footprint = None
        return json.dumps({'id':self.id,
                'name':self.name,
                'path':self.path,
                'footprint_latlon':footprint,
                'geom':footprint,
                'footprint_bodyfixed':self.footprint_bodyfixed})

    @hybrid_property
    def footprint_latlon(self):
    def geom(self):
        try:
            return to_shape(self._footprint_latlon)
            return to_shape(self._geom)
        except:
            return self._footprint_latlon
            return self._geom

    @footprint_latlon.setter
    def footprint_latlon(self, geom):
        if isinstance(geom, osgeo.ogr.Geometry):
    @geom.setter
    def geom(self, newgeom):
        if isinstance(newgeom, osgeo.ogr.Geometry):
            # If an OGR geom, convert to shapely
            geom = shapely.wkt.loads(geom.ExportToWkt())
        if geom is None:
            self._footprint_latlon = None
            newgeom = shapely.wkt.loads(newgeom.ExportToWkt())
        if newgeom is None:
            self._geom = None
        else:
            self._footprint_latlon = from_shape(geom, srid=latitudinal_srid)
            self._geom = from_shape(newgeom, srid=latitudinal_srid)

class Overlay(BaseMixin, Base):
    __tablename__ = 'overlay'
+7 −7
Original line number Diff line number Diff line
@@ -179,22 +179,22 @@ def test_jigsaw_append(mockFunc, session, measure_data, point_data, image_data):
    assert resp.sampler == 0.1

def test_null_footprint(session):
    i = model.Images.create(session, footprint_latlon=None,
    i = model.Images.create(session, geom=None,
                                      serial = 'serial')
    assert i.footprint_latlon is None
    assert i.geom is None

def test_broken_bad_geom(session):
    # An irreperablly damaged poly
    geom = MultiPolygon([Polygon([(0,0), (1,1), (1,2), (1,1), (0,0)])])
    i = model.Images.create(session, footprint_latlon=geom,
    truthgeom = MultiPolygon([Polygon([(0,0), (1,1), (1,2), (1,1), (0,0)])])
    i = model.Images.create(session, geom=truthgeom,
                                      serial = 'serial')
    resp = session.query(model.Images).filter(model.Images.id==i.id).one()
    assert resp.ignore == True

def test_fix_bad_geom(session):
    geom = MultiPolygon([Polygon([(0,0), (0,1), (1,1), (0,1), (1,1), (1,0), (0,0) ])])
    i = model.Images.create(session, footprint_latlon=geom,
    truthgeom = MultiPolygon([Polygon([(0,0), (0,1), (1,1), (0,1), (1,1), (1,0), (0,0) ])])
    i = model.Images.create(session, geom=truthgeom,
                                     serial = 'serial')
    resp = session.query(model.Images).filter(model.Images.id==i.id).one()
    assert resp.ignore == False
    assert resp.footprint_latlon == MultiPolygon([Polygon([(0,0), (0,1), (1,1), (1,0), (0,0) ])])
    assert resp.geom == MultiPolygon([Polygon([(0,0), (0,1), (1,1), (1,0), (0,0) ])])
+1 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ CREATE OR REPLACE FUNCTION validate_geom()
  RETURNS trigger AS
$BODY$
  BEGIN
      NEW.footprint_latlon = ST_MAKEVALID(NEW.footprint_latlon);
      NEW.geom = ST_MAKEVALID(NEW.geom);
      RETURN NEW;
    EXCEPTION WHEN OTHERS THEN
      NEW.ignore = true;
Loading