def generateGeoreferenceProcessForMapObj(mapObjId, request, log):
    """ Function generates a process process for a given map object id
         
        :type mapObjId: int
        :type request: pyramid.request
        :type log: logging.Logger
        :return: dict """
         
    def getMtbGLSpecificGeoreferenceInformation(mapObj, request):
        """ Query the specific process base data for a messtischblatt/geological map
     
        :type mapObj: georeference.models.vkdb.map.Map
        :type request: pyramid.request
        :return: dict """
        srid = mapObj.getSRID(request.db)
        extent = mapObj.getExtent(request.db, srid)
        return { 
            'georeference': {
                'source':'pixel',
                'target':'EPSG:%s'%srid,
                'gcps': [
                    {"source":[], "target":[extent[0],extent[1]]},
                    {"source":[], "target":[extent[0],extent[3]]},
                    {"source":[], "target":[extent[2],extent[1]]},
                    {"source":[], "target":[extent[2],extent[3]]}
                ],
                'algorithm': 'affine'
            },
            'extent':extent
        }
         
    mapObj = Map.by_id(mapObjId, request.db)    
     
    # get general metadata
    log.debug('Get general process process information ...')
    generalMetadata = getGeneralMetadata(mapObj, request)
         
    # check if there exist already a activate process process for
    # this mapObj
    log.debug('Get specific process process information ...')
    if Georeferenzierungsprozess.isGeoreferenced(mapObj.id, request.db):
        # there does exist a process process for this mapObj
        georefProcessObj = Georeferenzierungsprozess.getActualGeoreferenceProcessForMapId(mapObj.id, request.db)
        georeferenceData = getSpecificGeoreferenceData(georefProcessObj, mapObj, 4326, request.db)
    else: 
        # there does not exist a process process for this mapObj
        georeferenceData = {
            "timestamp": "",
            "type": "new"
        }

    # This blog could be used for attaching information regarding the boundingbox and the extent to the
    # response. In case no georeference information is registered yet this could be used by the client for
    # doing a matching of corner point coordinates and the extent corners.
    #
    # log.debug('Check if there is special behavior needed in case of messtischblatt')
    # mtbGeorefBaseData = {}
    # if mapObj.maptype == 'M' and 'georeference' not in georeferenceData and mapObj.boundingbox is not None:
    #     mtbGeorefBaseData = getMtbGLSpecificGeoreferenceInformation(mapObj, request)
    #     response.update(mtbGeorefBaseData)

    log.debug('Check if there are pending processes in the database')
    warnMsg = {}
    if checkIfPendingProcessesExist(mapObj, request):
        warnMsg["warn"] = 'Right now another users is working on the georeferencing of this map sheet. For preventing information losses please try again in 15 minutes.'
         
    # now merge dictionaries and create response
    response = {
        "recommendedsrid": mapObj.recommendedsrid
    }
    response.update(generalMetadata)
    response.update(georeferenceData)
    response.update(warnMsg)
    return response
Exemple #2
0
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)