Exemple #1
0
    def test_read_with_where(self):
        crud_dbanalysis = crud.CrudDB(schema=es_constants.es2globals['schema_analysis'])

        product_key = {
            "productcode": 'vgt-ndvi',
            "subproductcode": 'ndvi-linearx2',
            "version": 'sv2-pv2.2'
        }
        crud_dbanalysis.read('timeseries_drawproperties_new', **product_key)
Exemple #2
0
# -*- coding: utf-8 -*-
from __future__ import absolute_import

__author__ = "Jurriaan van 't Klooster"

import unittest
from config import es_constants
from database import crud
from database import querydb

crud_db = crud.CrudDB(schema=es_constants.es2globals['schema_products'])


class TestCrud(unittest.TestCase):
    def test_crud(self):
        records = len(crud_db.read('date_format'))
        self.assertTrue(records > 0)

        record = {
            'date_format': 'TESTING123',
            'definition': 'We are testing crud!'
        }
        crud_db.create('date_format', record)

        self.assertEquals(
            len(crud_db.read('date_format', date_format='TESTING123')), 1)

        record = {
            'date_format': 'TESTING123',
            'definition': 'Updating this record!'
        }
def convert_to_ecoargis(productinfo, startdate, enddate, aggregateinfo,
                        vectorlayer, regionidattr, regionlevel):

    crud_db = crud.CrudDB(schema=es_constants.es2globals['schema_products'])

    productcode = productinfo['productcode']
    subproductcode = productinfo['subproductcode']
    version = productinfo['version']
    mapsetcode = productinfo['mapsetcode']

    product_info = querydb.get_product_out_info(productcode=productcode,
                                                subproductcode=subproductcode,
                                                version=version)
    if product_info.__len__() > 0:
        for row in product_info:
            product_descriptive_name = row.descriptive_name
            product_description = row.description
            product_dateformat = row.date_format
            product_provider = row.provider
    else:
        logger.error('Product does not exist: %s - %s - %s - %s' % productcode,
                     version, subproductcode, mapsetcode)
        exit()

    from_date = datetime.datetime.strptime(startdate, '%Y-%m-%d').date()
    to_date = datetime.datetime.strptime(enddate, '%Y-%m-%d').date()

    filename, file_extension = os.path.splitext(vectorlayer)
    if file_extension == '.shp':
        driver = ogr.GetDriverByName('ESRI Shapefile')
    elif file_extension == '.geojson':
        driver = ogr.GetDriverByName('GeoJSON')
    else:
        logger.error(
            'Vector layer file in a wrong format or has a wrong extention: %s'
            % vectorlayer)
        exit()

    vectorlayer = driver.Open(vectorlayer)

    # Get Projection from layer
    layer = vectorlayer.GetLayer()
    # spatialRef = layer.GetSpatialRef()

    # Get Shapefile Fields and Types
    layerDefinition = layer.GetLayerDefn()

    idattr_exists = False
    for i in range(layerDefinition.GetFieldCount()):
        fieldName = layerDefinition.GetFieldDefn(i).GetName()
        # fieldTypeCode = layerDefinition.GetFieldDefn(i).GetType()
        # fieldType = layerDefinition.GetFieldDefn(i).GetFieldTypeName(fieldTypeCode)
        # fieldWidth = layerDefinition.GetFieldDefn(i).GetWidth()
        # GetPrecision = layerDefinition.GetFieldDefn(i).GetPrecision()
        # print fieldName + " - " + fieldType + " " + str(fieldWidth) + " " + str(GetPrecision)
        if fieldName == regionidattr:
            idattr_exists = True

    if not idattr_exists:
        logger.error('ID Attribute does not exist in vector layer: %s' %
                     regionidattr)
        return

    for feature in layer:
        geom = feature.GetGeometryRef()
        # wkt = geom.ExportToWkt()
        regionid = feature.GetField(regionidattr)

        delete_ecoagrisrec = {
            "productcode": productcode,
            "subproductcode": subproductcode,
            "version": version,
            "mapsetcode": mapsetcode,
            "regionid": regionid,
            'aggregation_type': aggregateinfo['aggregation_type'],
        }

        if crud_db.delete('ecoagris', **delete_ecoagrisrec):
            logger.info('ecoagris record deleted')
        else:
            logger.error('Error deleting ecoagris record')

        timeseries = getTimeseries(productcode, subproductcode, version,
                                   mapsetcode, geom, from_date, to_date,
                                   aggregateinfo)
        # Loop through the timeseries and get each date/value and create a record
        for timeserie in timeseries:
            if timeserie['meanvalue'] not in [None, '']:
                productdate = timeserie['date'].strftime("%Y%m%d")
                if product_dateformat == 'YYYYMMDDHHMM':
                    productdate = timeserie['date'].strftime("%Y%m%d%H%M")
                if product_dateformat == 'YYYYMMDD':
                    productdate = timeserie['date'].strftime("%Y%m%d")
                if product_dateformat == 'YYYYMM':
                    productdate = timeserie['date'].strftime("%Y%m")
                if product_dateformat == 'YYYY':
                    productdate = timeserie['date'].strftime("%Y")
                if product_dateformat == 'MMDD':
                    productdate = timeserie['date'].strftime("%m%d")

                ecoagris_record = {
                    "productcode": productcode,
                    "subproductcode": subproductcode,
                    "version": version,
                    "mapsetcode": mapsetcode,
                    "product_descriptive_name": product_descriptive_name,
                    "product_description": product_description,
                    "provider": product_provider,
                    "regionid": regionid,
                    "regionlevel": regionlevel,
                    'aggregation_type': aggregateinfo['aggregation_type'],
                    'aggregation_min': aggregateinfo['aggregation_min'],
                    'aggregation_max': aggregateinfo['aggregation_max'],
                    "product_dateformat": product_dateformat,
                    "product_date": productdate,
                    "tsvalue": timeserie['meanvalue']
                }
                # print ecoagris_record
                # Insert record in DB table ecoagris
                if crud_db.create('ecoagris', ecoagris_record):
                    logger.info('ecoagris record created')
                else:
                    logger.error('Error creating ecoagris record')

        # Return the latest computed record (M.C.)
        return ecoagris_record