def test_create_extent(self):

        package = factories.Dataset()

        geojson = json.loads(self.geojson_examples['point'])

        shape = asShape(geojson)
        package_extent = PackageExtent(package_id=package['id'],
                                       the_geom=WKTElement(
                                           shape.wkt, self.db_srid))
        package_extent.save()

        assert_equals(package_extent.package_id, package['id'])
        if legacy_geoalchemy:
            assert_equals(Session.scalar(package_extent.the_geom.x),
                          geojson['coordinates'][0])
            assert_equals(Session.scalar(package_extent.the_geom.y),
                          geojson['coordinates'][1])
            assert_equals(Session.scalar(package_extent.the_geom.srid),
                          self.db_srid)
        else:
            from sqlalchemy import func
            assert_equals(
                Session.query(func.ST_X(package_extent.the_geom)).first()[0],
                geojson['coordinates'][0])
            assert_equals(
                Session.query(func.ST_Y(package_extent.the_geom)).first()[0],
                geojson['coordinates'][1])
            assert_equals(package_extent.the_geom.srid, self.db_srid)
    def test_create_extent(self):

        package = factories.Dataset()

        geojson = json.loads(self.geojson_examples['point'])

        shape = asShape(geojson)
        package_extent = PackageExtent(package_id=package['id'],
                                       the_geom=WKTElement(shape.wkt,
                                                           self.db_srid))
        package_extent.save()

        assert_equals(package_extent.package_id, package['id'])
        if legacy_geoalchemy:
            assert_equals(Session.scalar(package_extent.the_geom.x),
                          geojson['coordinates'][0])
            assert_equals(Session.scalar(package_extent.the_geom.y),
                          geojson['coordinates'][1])
            assert_equals(Session.scalar(package_extent.the_geom.srid),
                          self.db_srid)
        else:
            from sqlalchemy import func
            assert_equals(
                Session.query(func.ST_X(package_extent.the_geom)).first()[0],
                geojson['coordinates'][0])
            assert_equals(
                Session.query(func.ST_Y(package_extent.the_geom)).first()[0],
                geojson['coordinates'][1])
            assert_equals(package_extent.the_geom.srid, self.db_srid)
Example #3
0
    def test_create_extent(self):

        package = factories.Dataset()

        geojson = json.loads(self.geojson_examples["point"])

        shape = asShape(geojson)
        package_extent = PackageExtent(
            package_id=package["id"],
            the_geom=WKTElement(shape.wkt, self.db_srid),
        )
        package_extent.save()

        assert (package_extent.package_id == package["id"])
        if legacy_geoalchemy:
            assert (Session.scalar(
                package_extent.the_geom.x) == geojson["coordinates"][0])
            assert (Session.scalar(
                package_extent.the_geom.y) == geojson["coordinates"][1])
            assert (Session.scalar(
                package_extent.the_geom.srid) == self.db_srid)
        else:
            from sqlalchemy import func

            assert (Session.query(func.ST_X(package_extent.the_geom)).first()
                    [0] == geojson["coordinates"][0])
            assert (Session.query(func.ST_Y(package_extent.the_geom)).first()
                    [0] == geojson["coordinates"][1])
            assert (package_extent.the_geom.srid == self.db_srid)
Example #4
0
    def test_create_extent(self):
        package = Package.get('annakarenina')
        assert package

        geojson = json.loads(self.geojson_examples['point'])

        shape = asShape(geojson)
        package_extent = PackageExtent(package_id=package.id,the_geom=WKTSpatialElement(shape.wkt, self.db_srid))
        package_extent.save()

        assert package_extent.package_id == package.id
        assert Session.scalar(package_extent.the_geom.x) == geojson['coordinates'][0]
        assert Session.scalar(package_extent.the_geom.y) == geojson['coordinates'][1]
        assert Session.scalar(package_extent.the_geom.srid) == self.db_srid
Example #5
0
    def test_update_extent(self):

        package = Package.get('annakarenina')

        geojson = json.loads(self.geojson_examples['point'])

        shape = asShape(geojson)
        package_extent = PackageExtent(package_id=package.id,
                                       the_geom=WKTSpatialElement(
                                           shape.wkt, self.db_srid))
        package_extent.save()
        assert Session.scalar(
            package_extent.the_geom.geometry_type) == 'ST_Point'

        # Update the geometry (Point -> Polygon)
        geojson = json.loads(self.geojson_examples['polygon'])

        shape = asShape(geojson)
        package_extent.the_geom = WKTSpatialElement(shape.wkt, self.db_srid)
        package_extent.save()

        assert package_extent.package_id == package.id
        assert Session.scalar(
            package_extent.the_geom.geometry_type) == 'ST_Polygon'
        assert Session.scalar(package_extent.the_geom.srid) == self.db_srid
    def _get_extent_object(self, geometry):
        '''

        :param geometry: 

        '''
        if isinstance(geometry, basestring):
            geometry = json.loads(geometry)
        shape = asShape(geometry)
        return PackageExtent(package_id=u'xxx',
                             the_geom=WKTElement(shape.wkt, 4326))
Example #7
0
def save_package_extent(package_id, geometry=None, srid=None):
    '''Adds, updates or deletes the package extent geometry.
    
       package_id: Package unique identifier
       geometry: a Python object implementing the Python Geo Interface
                (i.e a loaded GeoJSON object)
       srid: The spatial reference in which the geometry is provided.
             If None, it defaults to the DB srid.
    
       Will throw ValueError if the geometry object does not provide a geo interface.
    
       The responsibility for calling model.Session.commit() is left to the
       caller.

    :param package_id: 
    :param geometry:  (Default value = None)
    :param srid:  (Default value = None)

    '''
    db_srid = int(toolkit.config.get(u'ckan.spatial.srid', u'4326'))

    existing_package_extent = Session.query(PackageExtent).filter(
        PackageExtent.package_id == package_id).first()

    if geometry:
        shape = asShape(geometry)

        if not srid:
            srid = db_srid

        package_extent = PackageExtent(package_id=package_id,
                                       the_geom=WKTElement(shape.wkt, srid))

    # Check if extent exists
    if existing_package_extent:

        # If extent exists but we received no geometry, we'll delete the existing one
        if not geometry:
            existing_package_extent.delete()
            log.debug(u'Deleted extent for package %s' % package_id)
        else:
            # Check if extent changed
            if not compare_geometry_fields(package_extent.the_geom,
                                           existing_package_extent.the_geom):
                # Update extent
                existing_package_extent.the_geom = package_extent.the_geom
                existing_package_extent.save()
                log.debug(u'Updated extent for package %s' % package_id)
            else:
                log.debug(u'Extent for package %s unchanged' % package_id)
    elif geometry:
        # Insert extent
        Session.add(package_extent)
        log.debug(u'Created new extent for package %s' % package_id)
Example #8
0
    def test_update_extent(self):

        package = factories.Dataset()

        geojson = json.loads(self.geojson_examples["point"])

        shape = asShape(geojson)
        package_extent = PackageExtent(
            package_id=package["id"],
            the_geom=WKTElement(shape.wkt, self.db_srid),
        )
        package_extent.save()
        if legacy_geoalchemy:
            assert(
                Session.scalar(package_extent.the_geom.geometry_type) ==
                "ST_Point"
            )
        else:
            from sqlalchemy import func

            assert(
                Session.query(
                    func.ST_GeometryType(package_extent.the_geom)
                ).first()[0] ==
                "ST_Point"
            )

        # Update the geometry (Point -> Polygon)
        geojson = json.loads(self.geojson_examples["polygon"])

        shape = asShape(geojson)
        package_extent.the_geom = WKTElement(shape.wkt, self.db_srid)
        package_extent.save()

        assert(package_extent.package_id == package["id"])
        if legacy_geoalchemy:
            assert(
                Session.scalar(package_extent.the_geom.geometry_type) ==
                "ST_Polygon"
            )
            assert(
                Session.scalar(package_extent.the_geom.srid) == self.db_srid
            )
        else:
            assert(
                Session.query(
                    func.ST_GeometryType(package_extent.the_geom)
                ).first()[0] ==
                "ST_Polygon"
            )
            assert(package_extent.the_geom.srid == self.db_srid)
def save_package_extent(package_id, geometry = None, srid = None):
    '''Adds, updates or deletes the package extent geometry.

       package_id: Package unique identifier
       geometry: a Python object implementing the Python Geo Interface
                (i.e a loaded GeoJSON object)
       srid: The spatial reference in which the geometry is provided.
             If None, it defaults to the DB srid.

       Will throw ValueError if the geometry object does not provide a geo interface.

    '''
    db_srid = int(config.get('ckan.spatial.srid', '4326'))


    existing_package_extent = Session.query(PackageExtent).filter(PackageExtent.package_id==package_id).first()

    if geometry:
        shape = asShape(geometry)

        if not srid:
            srid = db_srid

        package_extent = PackageExtent(package_id=package_id,the_geom=WKTSpatialElement(shape.wkt, srid))

    # Check if extent exists
    if existing_package_extent:

        # If extent exists but we received no geometry, we'll delete the existing one
        if not geometry:
            existing_package_extent.delete()
            log.debug('Deleted extent for package %s' % package_id)
        else:
            # Check if extent changed
            if Session.scalar(package_extent.the_geom.wkt) <> Session.scalar(existing_package_extent.the_geom.wkt):
                # Update extent
                existing_package_extent.the_geom = package_extent.the_geom
                existing_package_extent.save()
                log.debug('Updated extent for package %s' % package_id)
            else:
                log.debug('Extent for package %s unchanged' % package_id)
    elif geometry:
        # Insert extent
        Session.add(package_extent)
        log.debug('Created new extent for package %s' % package_id)
Example #10
0
    def test_update_extent(self):
        ''' '''

        package = factories.Dataset()

        geojson = json.loads(self.geojson_examples[u'point'])

        shape = asShape(geojson)
        package_extent = PackageExtent(package_id=package[u'id'],
                                       the_geom=WKTElement(shape.wkt,
                                                           self.db_srid))
        package_extent.save()
        if legacy_geoalchemy:
            assert_equals(
                Session.scalar(package_extent.the_geom.geometry_type),
                u'ST_Point')
        else:
            from sqlalchemy import func
            assert_equals(
                Session.query(
                    func.ST_GeometryType(package_extent.the_geom)).first()[0],
                u'ST_Point')

        # Update the geometry (Point -> Polygon)
        geojson = json.loads(self.geojson_examples[u'polygon'])

        shape = asShape(geojson)
        package_extent.the_geom = WKTElement(shape.wkt, self.db_srid)
        package_extent.save()

        assert_equals(package_extent.package_id, package[u'id'])
        if legacy_geoalchemy:
            assert_equals(
                Session.scalar(package_extent.the_geom.geometry_type),
                u'ST_Polygon')
            assert_equals(
                Session.scalar(package_extent.the_geom.srid),
                self.db_srid)
        else:
            assert_equals(
                Session.query(
                    func.ST_GeometryType(package_extent.the_geom)).first()[0],
                u'ST_Polygon')
            assert_equals(package_extent.the_geom.srid, self.db_srid)
    def test_update_extent(self):

        package = factories.Dataset()

        geojson = json.loads(self.geojson_examples['point'])

        shape = asShape(geojson)
        package_extent = PackageExtent(package_id=package['id'],
                                       the_geom=WKTElement(shape.wkt,
                                                           self.db_srid))
        package_extent.save()
        if legacy_geoalchemy:
            assert_equals(
                Session.scalar(package_extent.the_geom.geometry_type),
                'ST_Point')
        else:
            from sqlalchemy import func
            assert_equals(
                Session.query(
                    func.ST_GeometryType(package_extent.the_geom)).first()[0],
                'ST_Point')

        # Update the geometry (Point -> Polygon)
        geojson = json.loads(self.geojson_examples['polygon'])

        shape = asShape(geojson)
        package_extent.the_geom = WKTElement(shape.wkt, self.db_srid)
        package_extent.save()

        assert_equals(package_extent.package_id, package['id'])
        if legacy_geoalchemy:
            assert_equals(
                Session.scalar(package_extent.the_geom.geometry_type),
                'ST_Polygon')
            assert_equals(
                Session.scalar(package_extent.the_geom.srid),
                self.db_srid)
        else:
            assert_equals(
                Session.query(
                    func.ST_GeometryType(package_extent.the_geom)).first()[0],
                'ST_Polygon')
            assert_equals(package_extent.the_geom.srid, self.db_srid)
    def test_update_extent(self):

        package = Package.get('annakarenina')

        geojson = json.loads(self.geojson_examples['point'])

        shape = asShape(geojson)
        package_extent = PackageExtent(package_id=package.id,the_geom=WKTSpatialElement(shape.wkt, self.db_srid))
        package_extent.save()
        assert Session.scalar(package_extent.the_geom.geometry_type) == 'ST_Point'

        # Update the geometry (Point -> Polygon)
        geojson = json.loads(self.geojson_examples['polygon'])

        shape = asShape(geojson)
        package_extent.the_geom=WKTSpatialElement(shape.wkt, self.db_srid)
        package_extent.save()

        assert package_extent.package_id == package.id
        assert Session.scalar(package_extent.the_geom.geometry_type) == 'ST_Polygon'
        assert Session.scalar(package_extent.the_geom.srid) == self.db_srid
Example #13
0
 def _get_extent_object(self, geometry):
     if isinstance(geometry, six.string_types):
         geometry = json.loads(geometry)
     shape = asShape(geometry)
     return PackageExtent(package_id="xxx",
                          the_geom=WKTElement(shape.wkt, 4326))