Exemplo n.º 1
0
def populateFeatures(args):
    pid = os.getpid()
    session = None
    shpFile = args.shpFile
    reproject = args.reproject
    keepfiles = args.keepfiles

    if reproject:
        try:
            shpFile = reprojectShp(shpFile, args)
        except Exception as e:
            raise Exception(e)

    try:
        models = modelsPyramid.models
        engine = sqlalchemy.create_engine(args.engineURL)
        session = scoped_session(sessionmaker(bind=engine))
        model = models[args.modelIndex]

        if not os.path.exists(shpFile):
            logger.error('[%s]: Shapefile %s does not exists' % (pid, shpFile))
            sys.exit(1)

        count = 1
        shp = ShpToGDALFeatures(shpFile)
        logger.info('[%s]: Processing %s' % (pid, shpFile))
        bulk = BulkInsert(model, session, withAutoCommit=1000)
        for feature in shp.getFeatures():
            polygon = feature.GetGeometryRef()
            # add shapefile path to dict
            # self.shpFilePath
            bulk.add(dict(
                the_geom = WKTElement(polygon.ExportToWkt(), 4326),
                shapefilepath=shpFile
            ))
            count += 1
        bulk.commit()
        logger.info('[%s]: Commit %s features for %s.' % (pid, count, shpFile))
    except Exception as e:
        logger.error(e, exc_info=True)
        raise Exception(e)
    finally:
        if session is not None:
            session.close_all()
            engine.dispose()

    if reproject:
        # Discard file after reprojection if specified in config
        if not keepfiles:
            logger.info('[%s] Removing %s...' % (pid, shpFile))
            cleanup(shpFile)

    return count
Exemplo n.º 2
0
def populateFeatures(args):
    pid = os.getpid()
    session = None
    shpFile = args.shpFile
    reproject = args.reproject
    keepfiles = args.keepfiles

    if reproject:
        try:
            shpFile = reprojectShp(shpFile, args)
        except Exception as e:
            raise Exception(e)

    try:
        models = modelsPyramid.models
        engine = sqlalchemy.create_engine(args.engineURL)
        session = scoped_session(sessionmaker(bind=engine))
        model = models[args.modelIndex]

        if not os.path.exists(shpFile):
            logger.error('[%s]: Shapefile %s does not exists' % (pid, shpFile))
            sys.exit(1)

        count = 1
        shp = ShpToGDALFeatures(shpFile)
        logger.info('[%s]: Processing %s' % (pid, shpFile))
        bulk = BulkInsert(model, session, withAutoCommit=1000)
        for feature in shp.getFeatures():
            polygon = feature.GetGeometryRef()
            # add shapefile path to dict
            # self.shpFilePath
            bulk.add(
                dict(shapefilepath=shpFile,
                     the_geom='SRID=4326;' + polygon.ExportToWkt()))
            count += 1
        bulk.commit()
        logger.info('[%s]: Commit %s features for %s.' % (pid, count, shpFile))
    except Exception as e:
        logger.error(e, exc_info=True)
        raise Exception(e)
    finally:
        if session is not None:
            session.close_all()
            engine.dispose()

    if reproject:
        # Discard file after reprojection if specified in config
        if not keepfiles:
            logger.info('[%s] Removing %s...' % (pid, shpFile))
            cleanup(shpFile)

    return count
Exemplo n.º 3
0
def reprojectShp(shpFilePath, args):
    logger.info('Action reprojectShapefile(%s)' % shpFilePath)
    outDirectory = args.outDirectory
    outFile = '%s%s' % (outDirectory, os.path.basename(shpFilePath))

    try:
        # If out file already exists clean it up first
        cleanup(outFile)

        command = 'mono %(geosuiteCmd)s -calc reframe -in %(inFile)s ' \
            '-out %(outFile)s -pframes %(fromPFrames)s,%(toPFrames)s ' \
            '-aframes %(fromAFrames)s,%(toAFrames)s -log %(logfile)s ' \
            '-err %(errorfile)s' % dict(
                geosuiteCmd = args.geosuiteCmd,
                inFile      = args.shpFile,
                outFile     = outFile,
                fromPFrames = args.fromPFrames,
                toPFrames   = args.toPFrames,
                fromAFrames = args.fromAFrames,
                toAFrames   = args.toAFrames,
                logfile     = args.logfile,
                errorfile   = args.errorfile
            )
        logger.info('Command: %s' % command)
        subprocess.call(command, shell=True)
    except Exception as e:
        logger.error('Could not reproject %(inFile)s into '
            '%(outFile)s: %(err)s' % dict(
                inFile  = shpFilePath,
                outFile = outFile,
                err     = e
            ), exc_info=True)
        raise Exception(e)

    # TODO new version has error codes this can now be changed
    # As we can't detect a success from an error with the current implementation
    # We determine if the output file exists and exit if not
    if not os.path.isfile(outFile):
        logger.error('File could not be reprojected')
        logger.error('Have a look at %(logfile)s or %(errorfile)s for '
            'more information.' % dict(
                logfile   = args.logfile,
                errorfile = args.errorfile
            ))
        raise Exception('File could not be reprojected!!')

    return outFile
Exemplo n.º 4
0
def reprojectShp(shpFilePath, args):
    logger.info('Action reprojectShapefile(%s)' % shpFilePath)
    outDirectory = args.outDirectory
    outFile = '%s%s' % (outDirectory, os.path.basename(shpFilePath))

    try:
        # If out file already exists clean it up first
        cleanup(outFile)

        command = 'mono %(geosuiteCmd)s -calc reframe -in %(inFile)s ' \
            '-out %(outFile)s -pframes %(fromPFrames)s,%(toPFrames)s ' \
            '-aframes %(fromAFrames)s,%(toAFrames)s -log %(logfile)s ' \
            '-err %(errorfile)s' % dict(
                geosuiteCmd = args.geosuiteCmd,
                inFile      = args.shpFile,
                outFile     = outFile,
                fromPFrames = args.fromPFrames,
                toPFrames   = args.toPFrames,
                fromAFrames = args.fromAFrames,
                toAFrames   = args.toAFrames,
                logfile     = args.logfile,
                errorfile   = args.errorfile
            )
        logger.info('Command: %s' % command)
        subprocess.call(command, shell=True)
    except Exception as e:
        logger.error('Could not reproject %(inFile)s into '
                     '%(outFile)s: %(err)s' %
                     dict(inFile=shpFilePath, outFile=outFile, err=e),
                     exc_info=True)
        raise Exception(e)

    # TODO new version has error codes this can now be changed
    # As we can't detect a success from an error with the current implementation
    # We determine if the output file exists and exit if not
    if not os.path.isfile(outFile):
        logger.error('File could not be reprojected')
        logger.error('Have a look at %(logfile)s or %(errorfile)s for '
                     'more information.' %
                     dict(logfile=args.logfile, errorfile=args.errorfile))
        raise Exception('File could not be reprojected!!')

    return outFile