def testGeoreferenceMtbWithClip(self):
        dstPath = os.path.join(self.dir, 'mtb-georef-clipped.tif')
        geoTransform = gdal.GCPsToGeoTransform(self.testData1['mtb']['gcps'])
        clipPath = createClipShapefile(self.testData1['mtb']['bbox'], os.path.join(self.dir, 'test_shp'), self.testData1['mtb']['srs'])
        response = georeference(self.testData1['mtb']['srcFile'], dstPath, self.dir, 
                                geoTransform, self.testData1['mtb']['srs'], 'polynom', self.logger, clipPath)
        
        print '====================='
        print 'Test if testGeoreferenceMtb ...'
        print 'Response: %s'%response
        print '====================='

        self.assertTrue(os.path.exists(dstPath), 'Could not find created vrt file ...')
        self.assertEqual(response, dstPath, 'Response is not like expected ...')
                    
        responseDataset = gdal.Open(response, GA_ReadOnly)
        self.assertTrue(responseDataset.GetProjection() == 'GEOGCS["DHDN",DATUM["Deutsches_Hauptdreiecksnetz",SPHEROID["Bessel 1841",6377397.155,299.1528128000008,AUTHORITY["EPSG","7004"]],TOWGS84[598.1,73.7,418.2,0.202,0.045,-2.455,6.7],AUTHORITY["EPSG","6314"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4314"]]', 'Response has not expected coordinate system ...')
         
        # clear up
        del responseDataset
        if os.path.exists(dstPath):
            os.remove(dstPath)
        if os.path.exists(clipPath):
            os.remove('%s.shp'%os.path.join(self.dir, 'test_shp'))
            os.remove('%s.dbf'%os.path.join(self.dir, 'test_shp'))
            os.remove('%s.prj'%os.path.join(self.dir, 'test_shp'))
            os.remove('%s.shx'%os.path.join(self.dir, 'test_shp'))
Exemple #2
0
def processGeorefImage(mapObj, georefObj, dbsession, logger):
    """ Function process a persistent georeference image

    :type georeference.models.vkdb.map.Map: mapObj
    :type georeference.models.vkdb.georeferenzierungsprozess.Georeferenzierungsprozess: georefObj
    :type sqlalchemy.orm.session.Session: dbsession
    :type logging.Logger: logger
    :return: str """
    gcps = parseGcps(georefObj.georefparams['gcps'])
    georefTargetSRS = stripSRIDFromEPSG(georefObj.georefparams['target'])
    targetPath = os.path.join(GEOREFERENCE_PERSITENT_TARGETDIR, os.path.join(str(mapObj.maptype).lower(), mapObj.apsdateiname+'.tif'))
    transformationAlgorithm = georefObj.georefparams['algorithm'] if 'algorithm' in georefObj.georefparams else 'affine'
    destPath = None

    # create clip shape if exists
    clipShpPath = None
    if georefObj.clip is not None:
        clipShpPath = os.path.join(TMP_DIR, '%s' % uuid.uuid4())
        clipShpPath = createClipShapefile(convertPostgisStringToList(georefObj.clip), clipShpPath, georefObj.getSRIDClip(dbsession))

    logger.debug('Process georeference result ...')
    if transformationAlgorithm == 'affine':
        destPath = rectifyPolynom(mapObj.originalimage, targetPath, [], gcps, georefTargetSRS, logger, TMP_DIR, clipShpPath, order=1)
    elif transformationAlgorithm == 'polynom':
        destPath = rectifyPolynom(mapObj.originalimage, targetPath, [], gcps, georefTargetSRS, logger, TMP_DIR, clipShpPath)
    elif transformationAlgorithm == 'tps':
        destPath = rectifyTps(mapObj.originalimage, targetPath, [], gcps, georefTargetSRS, logger, TMP_DIR, clipShpPath)

    logger.debug('Add overviews to the image ...')
    addOverviews(destPath, '2 4 8 16 32', logger)

    return destPath
    def testCreateGdalGeoTiffDatasetGeoreferenced(self):
        destPath = os.path.join(self.dir, 'test-create-gdal-dataset-georeferenced.tif')
        destPath2 = os.path.join(self.dir, 'test-create-gdal-dataset-georeferenced2.tif')

        image = Image.open(self.file)
        
        ndarray = maskImage(image, self.testData)
        offset = getOffsetValues(self.testData, image)
        driver = gdal.GetDriverByName("GTiff")
        if driver is None:
            raise ValueError("Can't find GeoTiff Driver")
        
        response = createGdalDataset(image, ndarray, driver, None, destPath)
        response1 = createGdalDataset(image, ndarray, driver, offset, destPath2)
        
        print '====================='
        print 'Test if testCreateGdalGeoTiffDataset ...'
        print 'Response: %s'%response
        print '====================='
         
        
        geoTransform = gdal.GCPsToGeoTransform(self.testGcp)
        geoTransform1 = gdal.GCPsToGeoTransform(correctGCPOffset(self.testGcp, offset))
        geoProj = SRC_DICT_WKT[4314]
        response.SetProjection(geoProj)
        response.SetGeoTransform(geoTransform)
        response1.SetProjection(geoProj)
        response1.SetGeoTransform(geoTransform1)        
        response.FlushCache() 
        response1.FlushCache()
        # clear up
        del image
        del ndarray
        del response
        del response1
        
        # create vrt
        dst_path = os.path.join(self.dir, 'test_createVrt.vrt')
        dataset = gdal.Open(self.file, GA_ReadOnly)
        vrt = createVrt(dataset, dst_path)
        vrt.SetProjection(geoProj)
        vrt.SetGeoTransform(geoTransform)
        vrt.FlushCache()
        del vrt
        
        # create georef classic
        dst_path2 = os.path.join(self.dir, 'test-mtb-georef.tif')
        logger = createLogger('GeoreferenceTest', logging.DEBUG)
        georeference(self.file, dst_path2, self.dir, 
                geoTransform, 4314, 'polynom', logger)
#         if os.path.exists(destPath):
#             os.remove(destPath)

        # create georef clipped 
        dst_path3 = os.path.join(self.dir, 'test-mtb-georef-clipped.tif')
        clipPath = createClipShapefile(self.bbox, os.path.join(self.dir, 'test_shp'), 4314)
        response = georeference(self.file, dst_path3, self.dir, 
                                geoTransform, 4314, 'polynom', logger, clipPath)
Exemple #4
0
def processGeorefImage(mapObj, georefObj, dbsession, logger):
    """ Function process a persistent georeference image

    :type georeference.models.vkdb.map.Map: mapObj
    :type georeference.models.vkdb.georeferenzierungsprozess.Georeferenzierungsprozess: georefObj
    :type sqlalchemy.orm.session.Session: dbsession
    :type logging.Logger: logger
    :return: str """
    gcps = parseGcps(georefObj.georefparams['gcps'])
    georefTargetSRS = stripSRIDFromEPSG(georefObj.georefparams['target'])
    targetPath = os.path.join(
        GEOREFERENCE_PERSITENT_TARGETDIR,
        os.path.join(
            str(mapObj.maptype).lower(), mapObj.apsdateiname + '.tif'))
    transformationAlgorithm = georefObj.georefparams[
        'algorithm'] if 'algorithm' in georefObj.georefparams else 'affine'
    destPath = None

    # create clip shape if exists
    clipShpPath = None
    if georefObj.clip is not None:
        clipShpPath = os.path.join(TMP_DIR, '%s' % uuid.uuid4())
        clipShpPath = createClipShapefile(
            convertPostgisStringToList(georefObj.clip), clipShpPath,
            georefObj.getSRIDClip(dbsession))

    logger.debug('Process georeference result ...')
    if transformationAlgorithm == 'affine':
        destPath = rectifyPolynom(mapObj.originalimage,
                                  targetPath, [],
                                  gcps,
                                  georefTargetSRS,
                                  logger,
                                  TMP_DIR,
                                  clipShpPath,
                                  order=1)
    elif transformationAlgorithm == 'polynom':
        destPath = rectifyPolynom(mapObj.originalimage, targetPath, [], gcps,
                                  georefTargetSRS, logger, TMP_DIR,
                                  clipShpPath)
    elif transformationAlgorithm == 'tps':
        destPath = rectifyTps(mapObj.originalimage, targetPath, [], gcps,
                              georefTargetSRS, logger, TMP_DIR, clipShpPath)

    logger.debug('Add overviews to the image ...')
    addOverviews(destPath, '2 4 8 16 32', logger)

    return destPath
    def testCreateClipShapefile(self):
        dst_path = os.path.join(self.dir, 'test_shapefile')
        response = createClipShapefile(self.boundingbox, dst_path, 4314)
        
        print '====================='
        print 'Test if testCreateClipShapefile ...'
        print 'Response: %s'%response
        print '====================='
        
        self.assertTrue('%s.shp'%dst_path == response, 'Response is not like expected ...')

        # clean up 
        if os.path.exists('%s.shp'%dst_path):
            os.remove('%s.shp'%dst_path)
            os.remove('%s.dbf'%dst_path)
            os.remove('%s.prj'%dst_path)
            os.remove('%s.shx'%dst_path)