Package: vectorio
Vector IO - Geoprocessing utility for working with vector data.python >= 3.6gdal >= 2.2rarunrarDescriptionThis project is a tool for working with vectorial data based onGDAL. This tool is an envelope about gdal and aims to work with different types of vector data quickly, intelligently, and simply. The vectorIO provide the support for (read and write) geojson, wkt, Shapefile and KML, support for quick switch between different spatial data types, and provides a exception handler for warnings from gdal.InstallationDockerComplete enviroment on Ubuntu:DockerfileCreating a image and instantiate the container:# access the directory where is the Dockerfiledockerimagebuild-tvectorio-env:001.# build the image# vectorio-env:001 - can be any name with the version of the your preferencedockercontainerrun-itvectorio-env:001# instantiate a new containerUbuntu 18.04Rarapt-getinstallrarunrarGdalInstalling gdal on ubuntuGdal for pythongdalinfo--version
pip3installgdal==<gdal_version>FeaturesRead and write geojsonRead and write WKTRead and write ShapefileRead and write KMLReprojecting a vectorQuick switch between geographic dataSearch UTM Zone from GeometryRaise exception for warnings from gdalReprojecting a datasource directlyCounting VerticesRead and Write GeojsonWorking with geojson data. By default, the datasource is created as WGS84.Preparing the datafromvectorio.vectorimportGeojsondata='{"type": "FeatureCollection","features": [{"type": "Feature","properties":{},"geometry": {"type": "Polygon","coordinates": [[[-44.89013671875,-6.577303118123875],[-46.29638671874999,-7.460517719883772],[-44.4287109375,-7.318881730366743],[-44.89013671875,-6.577303118123875]]]}}]}'gjs=Geojson(data)Read all data# Featuresgjs.feature_collection()# Geometriesgjs.geometry_collection()Reading and iterating over each featureforfeatingjs.features():print(feat)Creating a new geojson filegjs.write('data.geojson')Reading from geojson filewithopen('data.geojson')asf:gj=Geojson(f.read())gj.feature_collection()Read and write WKTWorking with wkt data. Is supported geometry collection and single geometries. By default, the datasource is created as WGS84.The wkt object has some parameters:WKT(as_geometry_collection=True,srid=4326)as_geometry_collection: return a geometry collection same when the data is a single geometry by methodcollection.srid: Initial SRID for WKT.Preparing the datafromvectorio.vectorimportWKTdata="GEOMETRYCOLLECTION(POINT(-48.740641051554974 -9.249606262178954), LINESTRING(-50.278726989054974 -11.023166202413554,-48.608805114054974 -10.375450023701761))"wkt=WKT(data)Read all datawkt.geometry_collection()Reading and iterating over each geometryforgeominwkt.geometries():print(geom)Creating a new wkt filewkt.write('data.wkt')Reading from wkt filewithopen('data.wkt')asf:wkt=WKT(f.read())wkt.geometry_collection()Read and write ShapefileWorking with read and write shapefile. Is supported shapefiles compressed as .zip and .rar. By default, the datasource is created based on projection present on .prj file.obs: read and write of the .rar files is available only for linux OS. Only the vectorio.compress.Rar (engine to compress) class has this restriction. The other classes are available for any OS.Preparing the datafromvectorio.vectorimportShapefileshape=Shapefile('data.shp')Read all data from .shp fileshape.feature_collection()Reading and iterating over each feature from .shp file.# Interanting over featuresforfeatinshape.features():print(feat)# Interanting over geometriesforgeominshape.geometries():print(geom)Creating a new shapefile (Are be created the files .shp, .shx, .dbf, .prj)shape.write('out.shp')# >>> out.shpRead and write Shapefile compressedBy default the algorithm will search recusivly the files .shp, .shx, .dbf, .prj inside of the compressed file. The algorithm will search the first file of the each extension, case the compressed file contains 2 (or more) .shp files, or 2 (or more) .prj file, will be obtained the first .shp file and the first .prj file.Processing from zipfromvectorio.vectorimportShapefile,ShapefileCompressedfromvectorio.compressimportZipshape=ShapefileCompressed(Shapefile('data.zip'),compress_engine=Zip())shape.feature_collection()# reading all dataforfeatinshape.features():# iterating over each itemprint(feat)shape.write('out.zip')# Creating a shapefile compressed as .zip# >>> out.zipProcessing from .rar (available only for linux OS)fromvectorio.vectorimportShapefile,ShapefileCompressedfromvectorio.compressimportRarshape=ShapefileCompressed(Shapefile('data.rar'),compress_engine=Rar())shape.feature_collection()# read all dataforfeatinshape.features():# iterating over each itemprint(feat)shape.write('out.rar')# Creating a shapefile compressed as .rar# >>> out.rarRead and write KMLCurrently the KML is processed as Geojson.fromvectorio.vectorimportKMLkml=KML('data.kml')# Interanting over featuresforfeatinshape.features():print(feat)# Interanting over geometriesforgeominshape.geometries():print(geom)kml.write('out.geojson')Reprojecting a VectorThe spatial reprojection works with same geography type thats implements the interface IVectorIO.
If the input srid (in_srid) are be ommited, will used the srid from geometry.Reprojecting a shapefilefromvectorio.vectorimportShapefile,ShapefileCompressed,VectorReprojectedfromvectorio.compressimportZipshape=VectorReprojected(ShapefileCompressed(Shapefile('data_utm22.zip'),compress_engine=Zip()),in_srid=31982,out_srid=4674)shape.feature_collection()# reading all dataforfeatinshape.features():# iterating by each featureprint(feat)shape.write('data_reprojected.zip')# creating a new shapefileReprojecting a WKTBy default the wkt is in WGS84 spatial reference.fromvectorio.vectorimportWKT,VectorReprojecteddata='POLYGON((-49.698036566343376 -9.951372897703846,-51.148231878843376 -11.591810720955946,-48.467567816343376 -11.763953408065282,-49.698036566343376 -9.951372897703846))'wkt=VectorReprojected(WKT(data),out_srid=31982)wkt.geometry_collection()# reading all dataforgeominwkt.geometries():# iterating by each geometryprint(geom)wkt.write('data-reprojected.wkt')# creating a new wkt fileReprojecting a GeojsonBy default the geojson is in WGS84 spatial reference.fromvectorio.vectorimportGeojson,VectorReprojecteddata='{"type": "FeatureCollection","features": [{"type": "Feature","properties":{},"geometry": {"type": "Polygon","coordinates": [[[-45.992889404296875,-9.654907854199012],[-46.12884521484374,-9.72259300616733],[-45.96954345703125,-9.738835407948073],[-45.992889404296875,-9.654907854199012]]]}}]}'gjs=VectorReprojected(Geojson(data),out_srid=31982)gjs.feature_collection()# reading all dataforfeatingjs.features():# iterating by each featureprint(feat)gjs.write('data-reprojected.geojson')# creating a new geojson fileQuick Switch Between Geographic DataFor execution of the Quick switch must be used theVectorCompositepresent on packagevectorio.vector.VectorComposite(input_vector_obj,ouput_vector_obj)Quick switch from geojson to wktPreparing datafromvectorio.vectorimportGeojson,WKT,VectorCompositedata='{"type": "FeatureCollection","features": [{"type": "Feature","properties":{},"geometry": {"type": "Polygon","coordinates": [[[-44.89013671875,-6.577303118123875],[-46.29638671874999,-7.460517719883772],[-44.4287109375,-7.318881730366743],[-44.89013671875,-6.577303118123875]]]}}]}'vector=VectorComposite(Geojson(data),WKT())Reading all geometry from geojson as wktvector.geometry_collection()Iterating over all geometries as wktforgeom_wktinvector.geometries():print(geom_wkt)Creating a wkt filevector.write('output.wkt')Quick switch from wkt to shapefile as zipfromvectorio.vectorimportShapefile,ShapefileCompressed,WKT,VectorCompositefromvectorio.compressimportZipdata='MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)), ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35), (30 20, 20 15, 20 25, 30 20)))'vector=VectorComposite(WKT(data),ShapefileCompressed(Shapefile(),compress_engine=Zip()))Reading all geometry from wktvector.geometry_collection()Iterating over all geometriesforgeominvector.geometries():print(geom)Creating a shapefile as zipvector.write('output.zip')Search UTM Zone from GeometryThis functionality will search the UTM Zone from some geometry.fromvectorio.vectorimportUTMZone,VectorReprojected,WKTdata='POLYGON((-73.79131452179155 -11.78691590735885,-27.12139264679149 -12.645910804419744,-47.46330883419978 10.894322081983276,-73.79131452179155 -11.78691590735885))'ds_wkt=VectorReprojected(WKT(data),out_srid=4326).datasource()utm=UTMZone()utm.zones(ds_wkt)# getting all UTM Zones that intersect with the geometryThe methodzone_from_biggest_geomget only one zone that has the biggest geometry. For example, if a large geometry is in UTM Zone 25N or 26N, this method will calculate the area (for polygon) or length (for line) of geometry and return the UTM Zone where the area or length is the biggest.Signature:zone_from_biggest_geom(self, inp_ds: DataSource, wkt_prj_for_metrics: str, in_wkt_prj=PRJ_WGS84)wkt_prj_for_metrics: requiredinp_ds: requiredin_wkt_prj: Optional. Used case the input datasource no has a CRS.# prj_for_metrics - necessary for make metrics calculationsprj_for_metrics='PROJCS["Brazil / Albers Equal Area Conic (WGS84)",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Albers_Conic_Equal_Area"],PARAMETER["longitude_of_center",-50.0],PARAMETER["standard_parallel_1",10.0],PARAMETER["standard_parallel_2",-40.0],PARAMETER["latitude_of_center",-25.0],UNIT["Meter",1.0]]'utm.zone_from_biggest_geom(ds_wkt,wkt_prj_for_metrics=prj_for_metrics)# getting one UTM ZonePoints are ignored in this calculation. This method isn't remomended for geometries composed only by Points, (MultiPoint, GeometryCollection of points e etc.), because, the UTM zone returned is based in area/length metrics. Soon, will be implemented a method for get UTM Zone only for Point Geometries.UTM Zone for geometries with Topology ErrorsCase the your geometry has topology errors you should use the methodUTMZone().zones_from_iteration(wkt)passing a WKT as argument. This method will return a set of the utm zones that the wkt intersect.utm.zones_from_iteration('POLYGON((-59.408117902654105 -6.5855114909234445,-62.132727277654105 -8.241414689294965,-58.836828840154105 -8.545736525537883,-62.835852277654105 -5.230474986041972,-58.836828840154105 -6.4981931392341705,-59.408117902654105 -6.5855114909234445))')# {'20SW', '21SW'}Raise Exception for Warnings From GdalFor use the exception from gdal warnings should use the decoratorgdal_warning_as_exceptionpresents onvectorio.gdalpackage. This decorator will throw the error when theIsValid()method fromgeometry()method will be used.fromvectorio.gdalimportgdal_warning_as_exceptionfromvectorio.vectorimportWKTself_intersect_polygon='POLYGON((-54.24438490181399 -5.466896872158672,-54.84863294868899 -5.882330540835073,-54.09057630806399 -5.8714019542356475,-54.83764662056399 -5.379399666352095,-54.24438490181399 -5.466896872158672))'@gdal_warning_as_exceptiondefpossible_error():wkt=WKT(self_intersect_polygon)ds=wkt.datasource()lyr=ds.GetLayer(0)feat=lyr.GetFeature(0)feat.geometry().IsValid()possible_error()# >>> GDALSelfIntersectionGeometry: Self-intersection at or near point -54.469636435829948 -5.6217621987992636Possibles exceptionsGDALSelfIntersectionGeometry: Exception throwed when a polygon contains a self intersection.GDALBadClosedPolygon: Exception throwed when a polygon not correctly close.GDALUnknownException: Exception throwed when occurs a unknown error.Obs:All the exceptions are available on packagevectorio.exceptionsReprojecting a datasource directlyTo make spatial reprojection use the class VectorIOReprojectedmetioned above. However, in same moment will be necessary reproject a datasource directly, for this use the DataSourceReprojected class.Signature:DataSourceReprojected(inp_ds: DataSource, in_srid: int=None, out_srid: int=None, in_wkt_prj=None, out_wkt_prj=None, use_wkt_prj: bool=False)inp_ds: required. Datasource that will be reprojected.in_srid: optional. Used case the input datasource not has a CRS. (Used only when the flaguse_wkt_prjisFalse).out_srid: Required whether theuse_wkt_prjisFalse.in_wkt_prj: optional. WKT Projection on OGC pattern. Used case the input datasource not has a CRS. (Used only when the flaguse_wkt_prjisTrue).out_wkt_prj: Required whether theuse_wkt_prjisTrue. WKT Projection on OGC pattern.use_wkt_prj: Boolean to define whether the datasource will be reprojected with SRID our with the WKT Projection.Methods:ref() -> DataSource: Return the DataSource reprojected.Reprojecting with SRID.fromvectorio.vectorimportWKT,DataSourceReprojecteddata='POLYGON((-45.522540331834634 -6.851736627227062,-47.016680956834634 -7.7670786428296275,-45.434649706834634 -8.332736100352385,-45.522540331834634 -6.851736627227062))'wkt=WKT(data)ds=wkt.datasource()new_ds=DataSourceReprojected(ds,out_srid=31983).ref()wkt.geometry_collection(ds=new_ds)Reprojecting with WKT Projection.fromvectorio.vectorimportWKT,DataSourceReprojecteddata='POLYGON((-45.522540331834634 -6.851736627227062,-47.016680956834634 -7.7670786428296275,-45.434649706834634 -8.332736100352385,-45.522540331834634 -6.851736627227062))'wkt=WKT(data)prj='PROJCS["SIRGAS 2000 / UTM zone 23S",GEOGCS["SIRGAS 2000",DATUM["Sistema_de_Referencia_Geocentrico_para_America_del_Sur_2000",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6674"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4674"]],UNIT["metre",1,AUTHORITY["EPSG","9001"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-45],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",10000000],AUTHORITY["EPSG","31983"],AXIS["Easting",EAST],AXIS["Northing",NORTH]]'ds=wkt.datasource()new_ds=DataSourceReprojected(ds,out_wkt_prj=prj,use_wkt_prj=True).ref()wkt.geometry_collection(ds=new_ds)Counting VerticesAll classes that are vector has the method for count all vertices. Below, are be exemplified
with a WKT, however, this method works with Shapefile, GeoJson too.Signature:vertices_by_feature(ds: DataSource) -> dictds: Required. DataSource for count your vertices.Return:Will be returned a dictionary of vertices by feature.fromvectorio.vectorimportWKTdata='POLYGON((-45.522540331834634 -6.851736627227062,-47.016680956834634 -7.7670786428296275,-45.434649706834634 -8.332736100352385,-45.522540331834634 -6.851736627227062))'wkt=WKT(data)wkt.vertices_by_feature()# works with shapefile or Geojson# shapefile_obj.vertices_by_feature() or geojson_obj.vertices_by_feature()