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 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 georeferenceValidation(request): """ The function generates a process validation result and publish it via a WMS instance. The wms references are returned. :type request: pyramid.request :return: dict :raise: HTTPBadRequest :raise: HTTPInternalServerError """ LOGGER.info('Receive process validation request with parameters: %s'%request.json_body) try: # extract validation data LOGGER.debug('Parse request params ...') requestParams = parseGeoreferenceParamsFromRequest(request) LOGGER.debug('Parse and validate SRS params ...') georefSourceSRS = requestParams['georeference']['source'] if str(georefSourceSRS).lower() != 'pixel': raise ParameterException('Only "pixel" is yet supported as source srs.') if ':' not in str(requestParams['georeference']['target']): raise ParameterException('Missing "EPSG:...." syntax for target srs.') georefTargetSRS = stripSRIDFromEPSG(requestParams['georeference']['target']) # generate a temporary process result and publish it via wms LOGGER.debug('Parse and validate gcps params ...') if len(requestParams['georeference']['gcps']) < 2: raise ParameterException('Not enough gcps for valid georeferencing ...') gcps = parseGcps(requestParams['georeference']['gcps']) # calculate validation result return createValidationResult(requestParams, gcps, georefTargetSRS, LOGGER) except ParameterException as e: LOGGER.error(e) LOGGER.error(traceback.format_exc()) raise HTTPBadRequest(ERROR_MSG) except Exception as e: LOGGER.error(e) LOGGER.error(traceback.format_exc()) raise HTTPInternalServerError(ERROR_MSG)
def georeferenceValidation(request): """ The function generates a process validation result and publish it via a WMS instance. The wms references are returned. :type request: pyramid.request :return: dict :raise: HTTPBadRequest :raise: HTTPInternalServerError """ LOGGER.info("Receive process validation request with parameters: %s" % request.json_body) try: # extract validation data LOGGER.debug("Parse request params ...") requestParams = parseGeoreferenceParamsFromRequest(request) LOGGER.debug("Parse and validate SRS params ...") georefSourceSRS = requestParams["georeference"]["source"] if str(georefSourceSRS).lower() != "pixel": raise ParameterException('Only "pixel" is yet supported as source srs.') if ":" not in str(requestParams["georeference"]["target"]): raise ParameterException('Missing "EPSG:...." syntax for target srs.') georefTargetSRS = stripSRIDFromEPSG(requestParams["georeference"]["target"]) # generate a temporary process result and publish it via wms LOGGER.debug("Parse and validate gcps params ...") if len(requestParams["georeference"]["gcps"]) < 2: raise ParameterException("Not enough gcps for valid georeferencing ...") gcps = parseGcps(requestParams["georeference"]["gcps"]) # calculate validation result return createValidationResult(requestParams, gcps, georefTargetSRS, LOGGER) except ParameterException as e: LOGGER.error(e) LOGGER.error(traceback.format_exc()) raise HTTPBadRequest(ERROR_MSG) except Exception as e: LOGGER.error(e) LOGGER.error(traceback.format_exc()) raise HTTPInternalServerError(ERROR_MSG)
def createSearchRecord(mapObj, dbsession, logger, georefObj=None): """ Function creates an elasticsearch record for a given mapObj :type mapObj: vkviewer.python.models.vkdb.Map :type dbsession: sqlalchemy.orm.session.Session :type logger: logging.Logger :type georefObj: georeference.models.vkdb.georeferenzierungsprozess.Georeferenzierungsprozess|None """ mapData = {} # check if map is georeferenced because only # georeferenced map should be published via the index metadataObj = Metadata.by_id(mapObj.id, dbsession) timepublish = metadataObj.timepublish.date() oai = OAI_ID_PATTERN%mapObj.id onlineResList = getOnlineResourceData(mapObj, metadataObj, metadataObj.timepublish.year, oai, dbsession) mapData["oai:de:slub-dresden:vk:id-%s"%mapObj.id] = { "id": mapObj.id, "dataid": mapObj.apsdateiname, "title": metadataObj.titleshort, "titlelong": metadataObj.title, "description": metadataObj.description, "online-resources": onlineResList, "denominator": int(metadataObj.scale.split(':')[1]), "keywords": ";".join([metadataObj.type,metadataObj.technic]), "time": "%s-%s-%s"%(timepublish.year, timepublish.month, timepublish.day), "plink": metadataObj.apspermalink, "org":metadataObj.imagejpg, "georeference": mapObj.isttransformiert, "maptype": mapObj.maptype, "thumb": metadataObj.thumbssmall, "zoomify":metadataObj.imagezoomify } # if there is a geometry then add it to the record boundingbox = None try: elasticsearchSRS = int(ELASTICSEARCH_SRS.split(':')[1]) extent = mapObj.getExtent(dbsession, elasticsearchSRS) boundingbox = { "type": "polygon", "coordinates": [[ [extent[0],extent[1]], [extent[0],extent[3]], [extent[2],extent[3]], [extent[2],extent[1]], [extent[0],extent[1]] ]] } except AttributeError: logger.debug('Missing geometry') pass if boundingbox is not None and mapObj.isttransformiert: mapData[oai]["geometry"] = boundingbox # if georeferenced add tms cache if mapObj.isttransformiert: # create tms url file_name, file_extension = os.path.splitext(os.path.basename(mapObj.georefimage)) tmsUrl = GEOREFERENCE_PERSITENT_TMS_URL + '/' + os.path.join(str(mapObj.maptype).lower(), file_name) mapData[oai]["tms"] = tmsUrl if georefObj is not None and georefObj.clip is not None: mapData[oai]["clippolygon"] = convertPostgisStringToList(georefObj.getClipAsString(dbsession, stripSRIDFromEPSG(ELASTICSEARCH_SRS.lower()))) return mapData
def georeferenceConfirm(request): """ The function persistent saves a georeference confirmation request :type request: pyramid.request :return: dict :raise: HTTPBadRequest :raise: HTTPInternalServerError """ LOGGER.info('Receive georeference validation request with parameters: %s' % request.json_body) try: # extract validation data LOGGER.debug('Parse request params ...') requestData = parseGeoreferenceParamsFromRequest(request) # in case of type new, # check if there already exist an actual georeference process for this object with this type if requestData[ 'type'] == 'new' and Georeferenzierungsprozess.isGeoreferenced( requestData['mapObj'].id, request.db): msg = 'There exists already an active georeference process for this map id. It is therefore not possible to save a georeference process of type "new".' LOGGER.debug(msg) georeferenceid = Georeferenzierungsprozess.getActualGeoreferenceProcessForMapId( requestData['mapObj'].id, request.db).id return {'text': msg, 'georeferenceid': georeferenceid} LOGGER.debug('Save georeference parameter in datase ...') timestamp = convertTimestampToPostgisString(datetime.now()) georefParam = str(convertUnicodeDictToUtf(requestData['georeference'])) overwrites = requestData[ 'overwrites'] if 'overwrites' in requestData else 0 georefProcess = Georeferenzierungsprozess( messtischblattid=requestData['mapObj'].apsobjectid, nutzerid=requestData['userid'], georefparams=ast.literal_eval(georefParam), timestamp=timestamp, isactive=False, type=requestData['type'], overwrites=overwrites, adminvalidation='', processed=False, mapid=requestData['mapObj'].id, algorithm=requestData['georeference']['algorithm']) request.db.add(georefProcess) request.db.flush() # add polygon if 'clip' in requestData: clipParam = convertUnicodeDictToUtf(requestData['clip']) polygonAsString = convertListToPostgisString( clipParam['polygon'], 'POLYGON') georefProcess.setClip(polygonAsString, stripSRIDFromEPSG(clipParam['source']), request.db) LOGGER.debug('Create response ...') # @TODO has to be updated points = int(len(requestData['georeference']['gcps']) * 5) return { 'text': 'Georeference result saved. It will soon be ready for use.', 'georeferenceid': georefProcess.id, 'points': points } except ParameterException as e: LOGGER.error(e) LOGGER.error(traceback.format_exc()) raise HTTPBadRequest(ERROR_MSG) except Exception as e: LOGGER.error(e) LOGGER.error(traceback.format_exc()) raise HTTPInternalServerError(ERROR_MSG)
def createSearchRecord(mapObj, dbsession, logger, georefObj=None): """ Function creates an elasticsearch record for a given mapObj :type mapObj: vkviewer.python.models.vkdb.Map :type dbsession: sqlalchemy.orm.session.Session :type logger: logging.Logger :type georefObj: georeference.models.vkdb.georeferenzierungsprozess.Georeferenzierungsprozess|None """ mapData = {} # check if map is georeferenced because only # georeferenced map should be published via the index metadataObj = Metadata.by_id(mapObj.id, dbsession) timepublish = metadataObj.timepublish.date() oai = OAI_ID_PATTERN % mapObj.id onlineResList = getOnlineResourceData(mapObj, metadataObj, metadataObj.timepublish.year, oai, dbsession) mapData["oai:de:slub-dresden:vk:id-%s" % mapObj.id] = { "id": mapObj.id, "dataid": mapObj.apsdateiname, "title": metadataObj.titleshort, "titlelong": metadataObj.title, "description": metadataObj.description, "online-resources": onlineResList, "denominator": int(metadataObj.scale.split(':')[1]), "keywords": ";".join([metadataObj.type, metadataObj.technic]), "time": "%s-%s-%s" % (timepublish.year, timepublish.month, timepublish.day), "plink": metadataObj.apspermalink, "org": metadataObj.imagejpg, "georeference": mapObj.isttransformiert, "maptype": mapObj.maptype, "thumb": metadataObj.thumbssmall, "zoomify": metadataObj.imagezoomify } # if there is a geometry then add it to the record boundingbox = None try: elasticsearchSRS = int(ELASTICSEARCH_SRS.split(':')[1]) extent = mapObj.getExtent(dbsession, elasticsearchSRS) boundingbox = { "type": "polygon", "coordinates": [[[extent[0], extent[1]], [extent[0], extent[3]], [extent[2], extent[3]], [extent[2], extent[1]], [extent[0], extent[1]]]] } except AttributeError: logger.debug('Missing geometry') pass if boundingbox is not None and mapObj.isttransformiert: mapData[oai]["geometry"] = boundingbox # if georeferenced add tms cache if mapObj.isttransformiert: # create tms url file_name, file_extension = os.path.splitext( os.path.basename(mapObj.georefimage)) tmsUrl = GEOREFERENCE_PERSITENT_TMS_URL + '/' + os.path.join( str(mapObj.maptype).lower(), file_name) mapData[oai]["tms"] = tmsUrl if georefObj is not None and georefObj.clip is not None: mapData[oai]["clippolygon"] = convertPostgisStringToList( georefObj.getClipAsString( dbsession, stripSRIDFromEPSG(ELASTICSEARCH_SRS.lower()))) return mapData