<commit_before># -*- coding: utf-8 -*-
##############################################################
# OGR wrapper for convenient vector data handling and processing
# <NAME> 2015-2018
##############################################################

"""
This is intended as a vector meta information handler with options for reading and writing vector data in a convenient manner by simplifying the numerous options provided
by the OGR python binding
"""

import os

from osgeo import ogr, osr

from pyroSAR.spatial.auxil import crsConvert
from pyroSAR.ancillary import parse_literal

ogr.UseExceptions()
osr.UseExceptions()


class Vector(object):
    def __init__(self, filename=None, driver='ESRI Shapefile'):

        if driver not in ['ESRI Shapefile', 'Memory']:
            raise IOError('driver not supported')

        if filename is None:
            driver = 'Memory'
        else:
            self.filename = filename

        self.driver = ogr.GetDriverByName(driver)

        self.vector = self.driver.CreateDataSource('out') if driver == 'Memory' else self.driver.Open(filename)

        nlayers = self.vector.GetLayerCount()
        if nlayers > 1:
            raise IOError('multiple layers are currently not supported')
        elif nlayers == 1:
            self.init_layer()

    def __getitem__(self, expression):
        if isinstance(expression, int):
            return self.getFeatureByIndex(expression)
        elif isinstance(parse_literal(expression), int):
            return self.getFeatureByIndex(parse_literal(expression))
        else:
            try:
                field, value = expression.split('=')
            except AttributeError:
                raise KeyError('invalid expression')
            feat = self.getFeatureByAttribute(field, parse_literal(value))
            return feature2vector(feat, ref=self)

    def init_layer(self):
        self.layer = self.vector.GetLayer()
        self.__features = [None]*self.nfeatures

    def init_features(self):
        del self.__features
        self.__features = [None]*self.nfeatures

    @property
    def extent(self):
        return dict(zip(["xmin", "xmax", "ymin", "ymax"], self.layer.GetExtent()))

    @property
    def fieldDefs(self):
        return [self.layerdef.GetFieldDefn(x) for x in range(0, self.nfields)]

    @property
    def fieldnames(self):
        return [field.GetName() for field in self.fieldDefs]

    @property
    def geomType(self):
        return self.layerdef.GetGeomType()

    @property
    def layerdef(self):
        return self.layer.GetLayerDefn()

    @property
    def layername(self):
        return self.layer.GetName()

    @property
    def nlayers(self):
        return self.vector.GetLayerCount()

    @property
    def nfeatures(self):
        return len(self.layer)

    @property
    def nfields(self):
        return self.layerdef.GetFieldCount()

    def getProjection(self, type):
        """
        type can be either "epsg", "wkt", "proj4" or "osr"
        """
        return crsConvert(self.layer.GetSpatialRef(), type)

    @property
    def proj4(self):
        return self.srs.ExportToProj4().strip()

    @property
    def srs(self):
        return self.layer.GetSpatialRef()

    # todo Should return the wkt of the object, not of the projection
    @property
    def wkt(self):
        return self.srs.ExportToWkt()

    def addfield(self, name, type=ogr.OFTString, width=10):
        fieldDefn = ogr.FieldDefn(name, type)
        if type == ogr.OFTString:
            fieldDefn.SetWidth(width)
        self.layer.CreateField(fieldDefn)

    def addlayer(self, name, srs, geomType):
        self.vector.CreateLayer(name, srs, geomType)
        self.init_layer()

    def addfeature(self, geometry, fieldname, fieldvalue):

        if fieldname not in self.fieldnames:
            raise IOError("field does not exist")

        featureDefn = self.layerdef
        feature = ogr.Feature(featureDefn)
        feature.SetGeometry(geometry)
        feature.SetField(fieldname, fieldvalue)
        self.layer.CreateFeature(feature)
        feature.Destroy()
        self.init_features()

    def convert2wkt(self, set3D=True):
        """
        export the geometry of each feature as a wkt string
        """
        features = self.getfeatures()
        for feature in features:
            try:
                feature.geometry().Set3D(set3D)
            except AttributeError as e:
                print('apparently you are using a GDAL version lower than 2.1')
                raise e

        return [feature.geometry().ExportToWkt() for feature in features]

    def getArea(self):
        return sum([x.GetGeometryRef().GetArea() for x in self.getfeatures()])

    def getFeatureByAttribute(self, fieldname, attribute):
        if fieldname not in self.fieldnames:
            raise KeyError("invalid field name")
        out = None
        self.layer.ResetReading()
        for feature in self.layer:
            if feature.GetField(fieldname).strip() == attribute.strip():
                out = feature.Clone()
        self.layer.ResetReading()
        return out
<commit_msg>Vector.convert2wkt: introduced ogr.Geometry.SetCoordinateDimension as alternative to Set3D<commit_after># -*- coding: utf-8 -*-
##############################################################
# OGR wrapper for convenient vector data handling and processing
# <NAME> 2015-2018
##############################################################

"""
This is intended as a vector meta information handler with options for reading and writing vector data in a convenient manner by simplifying the numerous options provided
by the OGR python binding
"""

import os

from osgeo import ogr, osr

from pyroSAR.spatial.auxil import crsConvert
from pyroSAR.ancillary import parse_literal

ogr.UseExceptions()
osr.UseExceptions()


class Vector(object):
    def __init__(self, filename=None, driver='ESRI Shapefile'):

        if driver not in ['ESRI Shapefile', 'Memory']:
            raise IOError('driver not supported')

        if filename is None:
            driver = 'Memory'
        else:
            self.filename = filename

        self.driver = ogr.GetDriverByName(driver)

        self.vector = self.driver.CreateDataSource('out') if driver == 'Memory' else self.driver.Open(filename)

        nlayers = self.vector.GetLayerCount()
        if nlayers > 1:
            raise IOError('multiple layers are currently not supported')
        elif nlayers == 1:
            self.init_layer()

    def __getitem__(self, expression):
        if isinstance(expression, int):
            return self.getFeatureByIndex(expression)
        elif isinstance(parse_literal(expression), int):
            return self.getFeatureByIndex(parse_literal(expression))
        else:
            try:
                field, value = expression.split('=')
            except AttributeError:
                raise KeyError('invalid expression')
            feat = self.getFeatureByAttribute(field, parse_literal(value))
            return feature2vector(feat, ref=self)

    def init_layer(self):
        self.layer = self.vector.GetLayer()
        self.__features = [None]*self.nfeatures

    def init_features(self):
        del self.__features
        self.__features = [None]*self.nfeatures

    @property
    def extent(self):
        return dict(zip(["xmin", "xmax", "ymin", "ymax"], self.layer.GetExtent()))

    @property
    def fieldDefs(self):
        return [self.layerdef.GetFieldDefn(x) for x in range(0, self.nfields)]

    @property
    def fieldnames(self):
        return [field.GetName() for field in self.fieldDefs]

    @property
    def geomType(self):
        return self.layerdef.GetGeomType()

    @property
    def layerdef(self):
        return self.layer.GetLayerDefn()

    @property
    def layername(self):
        return self.layer.GetName()

    @property
    def nlayers(self):
        return self.vector.GetLayerCount()

    @property
    def nfeatures(self):
        return len(self.layer)

    @property
    def nfields(self):
        return self.layerdef.GetFieldCount()

    def getProjection(self, type):
        """
        type can be either "epsg", "wkt", "proj4" or "osr"
        """
        return crsConvert(self.layer.GetSpatialRef(), type)

    @property
    def proj4(self):
        return self.srs.ExportToProj4().strip()

    @property
    def srs(self):
        return self.layer.GetSpatialRef()

    # todo Should return the wkt of the object, not of the projection
    @property
    def wkt(self):
        return self.srs.ExportToWkt()

    def addfield(self, name, type=ogr.OFTString, width=10):
        fieldDefn = ogr.FieldDefn(name, type)
        if type == ogr.OFTString:
            fieldDefn.SetWidth(width)
        self.layer.CreateField(fieldDefn)

    def addlayer(self, name, srs, geomType):
        self.vector.CreateLayer(name, srs, geomType)
        self.init_layer()

    def addfeature(self, geometry, fieldname, fieldvalue):

        if fieldname not in self.fieldnames:
            raise IOError("field does not exist")

        featureDefn = self.layerdef
        feature = ogr.Feature(featureDefn)
        feature.SetGeometry(geometry)
        feature.SetField(fieldname, fieldvalue)
        self.layer.CreateFeature(feature)
        feature.Destroy()
        self.init_features()

    def convert2wkt(self, set3D=True):
        """
        export the geometry of each feature as a wkt string
        """
        features = self.getfeatures()
        for feature in features:
            try:
                feature.geometry().Set3D(set3D)
            except AttributeError:
                dim = 3 if set3D else 2
                feature.geometry().SetCoordinateDimension(dim)

        return [feature.geometry().ExportToWkt() for feature in features]

    def getArea(self):
        return sum([x.GetGeometryRef().GetArea() for x in self.getfeatures()])

    def getFeatureByAttribute(self, fieldname, attribute):
        if fieldname not in self.fieldnames:
            raise KeyError("invalid field name")
        out = None
        self.layer.ResetReading()
        for feature in self.layer:
            if feature.GetField(fieldname).strip() == attribute.strip():
                out = feature.Clone()
        self.layer.ResetReading()
        return out

{'fraction_non_alphanumeric': 0.12005799903334945, 'fraction_numerical': 0.004929917834702755, 'mean_word_length': 2.558995528035776, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2851905', 'n_tokens_mistral': 2939, 'n_tokens_neox': 2807, 'n_words': 763}