def DeleteDupGeom(infile): ds = vf.openToWrite(infile) lyr = ds.GetLayer() features = vf.getNbFeat(infile) geoms = dict() newshp = vf.copyShp(infile, "nodoublegeom") for feat in lyr: ge = feat.GetGeometryRef() f = feat.GetFID() if ge is not None: geoms[f] = ge.ExportToWkt() inverted = dict() for (k, v) in list(geoms.items()): if v[0] not in inverted: inverted[v] = k new_dict = dict() for (k, v) in list(inverted.items()): new_dict[v] = k print("Please wait ... copying features running") for k in new_dict: inFeat = lyr.GetFeature(k) vf.copyFeatInShp2(inFeat, newshp) print("Process done") return newshp
def modifID(shapefile, fieldID): ds = vf.openToWrite(shapefile) layer = ds.GetLayer() nbFeat = vf.getNbFeat(shapefile) i = 1 for feat in layer: feat.SetField(fieldID, i) layer.SetFeature(feat) i += 1
#!/usr/bin/env python3 #-*- coding: utf-8 -*- import sys from iota2.VectorTools import vector_functions as vf if len(sys.argv) != 2: print("usage: <shapefile>") sys.exit(1) else: print(vf.getNbFeat(sys.argv[1])) sys.exit(0)
def clipVectorfile(path, vector, clipfile, clipfield="", clipvalue="", outpath="", prefix="", debulvl="info", logger=logger): timeinit = time.time() if outpath == "": out = os.path.join(os.path.dirname(vector), "%s_%s.shp" % (prefix, str(clipvalue))) else: out = os.path.join(outpath, "%s_%s.shp" % (prefix, str(clipvalue))) epsgin = vf.get_vector_proj(vector) if vf.get_vector_proj(clipfile) != epsgin: logger.error( "Land cover vector file and clip file projections are different please provide a clip file with same projection as Land cover file (EPSG = %s)" % (epsgin)) sys.exit(-1) # clean geometries tmp = os.path.join(path, "tmp.shp") checkGeom.checkGeometryAreaThreshField(vector, 1, 0, tmp) for ext in [".shp", ".dbf", ".shx", ".prj"]: shutil.copy( os.path.splitext(tmp)[0] + ext, os.path.splitext(vector)[0] + ext) if not os.path.exists(out): if clipfile is not None: logger.info("Clip vector file %s with %s (%s == %s)" % ( os.path.basename(vector), os.path.basename(clipfile), clipfield, clipvalue, )) print("Clip vector file %s with %s (%s == %s)" % ( os.path.basename(vector), os.path.basename(clipfile), clipfield, clipvalue, )) # local environnement localenv = os.path.join(path, "tmp%s" % (str(clipvalue))) if os.path.exists(localenv): shutil.rmtree(localenv) os.mkdir(localenv) for ext in [".shp", ".dbf", ".shx", ".prj"]: shutil.copy(os.path.splitext(clipfile)[0] + ext, localenv) clipfile = os.path.join(localenv, os.path.basename(clipfile)) if vf.getNbFeat(clipfile) != 1: clip = os.path.join(localenv, "clip.shp") layer = vf.getFirstLayer(clipfile) fieldType = vf.getFieldType(os.path.join(localenv, clipfile), clipfield) if fieldType == str: command = ( "ogr2ogr -sql \"SELECT * FROM %s WHERE %s = '%s'\" %s %s" % (layer, clipfield, clipvalue, clip, clipfile)) Utils.run(command) elif fieldType == int or fieldType == float: command = 'ogr2ogr -sql "SELECT * FROM %s WHERE %s = %s" %s %s' % ( layer, clipfield, clipvalue, clip, clipfile, ) Utils.run(command) else: raise Exception("Field type %s not handled" % (fieldType)) else: clip = os.path.join(path, clipfile) logger.info( "'%s' shapefile has only one feature which will used to clip data" % (clip)) # clip clipped = os.path.join(localenv, "clipped.shp") command = "ogr2ogr -select cat -clipsrc %s %s %s" % (clip, clipped, vector) Utils.run(command) else: clipped = os.path.join(localenv, "merge.shp") timeclip = time.time() logger.info(" ".join([ " : ".join(["Clip final shapefile", str(timeclip - timeinit)]), "seconds", ])) # Delete duplicate geometries ddg.deleteDuplicateGeometriesSqlite(clipped) for ext in [".shp", ".shx", ".dbf", ".prj"]: shutil.copy( os.path.splitext(clipped)[0] + ext, os.path.join(localenv, "clean") + ext, ) os.remove(os.path.splitext(clipped)[0] + ext) timedupli = time.time() logger.info(" ".join([ " : ".join( ["Delete duplicated geometries", str(timedupli - timeclip)]), "seconds", ])) # Check geom vf.checkValidGeom(os.path.join(localenv, "clean.shp")) # Add Field Area (hectare) afa.addFieldArea(os.path.join(localenv, "clean.shp"), 10000) for ext in [".shp", ".shx", ".dbf", ".prj"]: shutil.copy(os.path.join(localenv, "clean" + ext), os.path.splitext(out)[0] + ext) shutil.rmtree(localenv) timeclean = time.time() logger.info(" ".join([ " : ".join([ "Clean empty geometries and compute areas (ha)", str(timeclean - timedupli), ]), "seconds", ])) else: logger.info("Output vector file '%s' already exists" % (out))
def tilesRastersMergeVectSimp(path, tiles, out, grass, mmu, \ fieldclass, clipfile, fieldclip, valueclip, tileId, tileNamePrefix, tilesfolder, \ douglas, hermite, angle): timeinit = time.time() print("Production of vector file %s" % (os.path.splitext(out)[0] + str(valueclip))) # local environnement localenv = os.path.join(path, "tmp%s" % (str(valueclip))) if os.path.exists(localenv): shutil.rmtree(localenv) os.mkdir(localenv) # Find vector tiles concerned by the given zone listTilesFiles = getTilesFiles(clipfile, tiles, tilesfolder, tileId, tileNamePrefix, localenv, fieldclip, valueclip) # Merge rasters localListTilesFiles = [] for tile in listTilesFiles: shutil.copy(tile, localenv) localListTilesFiles.append( os.path.join(localenv, os.path.basename(tile))) finalraster = mergeTileRaster(path, localListTilesFiles, fieldclip, valueclip, localenv) timemerge = time.time() print(" ".join( [" : ".join(["Merge Tiles", str(timemerge - timeinit)]), "seconds"])) # Raster vectorization and simplification outvect = os.path.join(localenv, finalraster[:-4] + '.shp') if os.path.exists(outvect): os.remove(outvect) vas.simplification(localenv, finalraster, grass, outvect, douglas, hermite, mmu, angle) # Delete raster after vectorisation os.remove(finalraster) timevect = time.time() print(" ".join([ " : ".join( ["Vectorisation and Simplification", str(timevect - timemerge)]), "seconds" ])) # Get clip shafile layer if clipfile is not None: for ext in ['.shp', '.dbf', '.shx', '.prj']: shutil.copy(os.path.splitext(clipfile)[0] + ext, localenv) clipfile = os.path.join(localenv, os.path.basename(clipfile)) if vf.getNbFeat(os.path.join(localenv, clipfile)) != 1: clip = os.path.join(localenv, "clip.shp") layer = vf.getFirstLayer(clipfile) fieldType = vf.getFieldType(os.path.join(localenv, clipfile), fieldclip) if fieldType == str: command = "ogr2ogr -sql \"SELECT * FROM %s WHERE %s = \'%s\'\" %s %s"%(layer, \ fieldclip, \ valueclip, \ clip, \ clipfile) Utils.run(command) elif fieldType == int or fieldType == float: command = "ogr2ogr -sql \"SELECT * FROM %s WHERE %s = %s\" %s %s"%(layer, \ fieldclip, \ valueclip, \ clip, \ clipfile) Utils.run(command) else: raise Exception('Field type %s not handled' % (fieldType)) else: clip = os.path.join(localenv, clipfile) print( "'%s' shapefile has only one feature which will used to clip data" % (clip)) # clip clipped = os.path.join(localenv, "clipped.shp") command = "ogr2ogr -select cat -clipsrc %s %s %s"%(clip, \ clipped, \ outvect) Utils.run(command) for ext in ['.shp', '.dbf', '.shx', '.prj']: if os.path.exists(os.path.splitext(outvect)[0] + ext): os.remove(os.path.splitext(outvect)[0] + ext) if os.path.exists(os.path.splitext(clipfile)[0] + ext): os.remove(os.path.splitext(clipfile)[0] + ext) if os.path.exists(os.path.splitext(clip)[0] + ext): os.remove(os.path.splitext(clip)[0] + ext) else: clipped = os.path.join(localenv, "merge.shp") timeclip = time.time() print(" ".join([ " : ".join(["Clip final shapefile", str(timeclip - timevect)]), "seconds" ])) # Delete duplicate geometries ddg.deleteDuplicateGeometriesSqlite(clipped) for ext in [".shp", ".shx", ".dbf", ".prj"]: shutil.copy( os.path.splitext(clipped)[0] + ext, os.path.join(localenv, "clean") + ext) os.remove(os.path.splitext(clipped)[0] + ext) timedupli = time.time() print(" ".join([ " : ".join(["Delete duplicated geometries", str(timedupli - timeclip)]), "seconds" ])) # Input shapefile init_grass(path, grass) gscript.run_command("v.in.ogr", flags="e", input=os.path.join(localenv, "clean.shp"), output="cleansnap", snap="1e-07") # Rename column if fieldclass: gscript.run_command("v.db.renamecolumn", map="cleansnap@datas", column="cat_,%s" % (fieldclass)) # Export shapefile outtmp = os.path.join( localenv, os.path.splitext(os.path.basename(out))[0] + str(valueclip) + os.path.splitext(os.path.basename(out))[1]) if os.path.exists(outtmp): os.remove(outtmp) gscript.run_command("v.out.ogr", flags="s", input="cleansnap@datas", output=outtmp, format="ESRI_Shapefile") # Check geom vf.checkValidGeom(outtmp) # Add Field Area (hectare) afa.addFieldArea(outtmp, 10000) timeprodvect = time.time() print(" ".join([" : ".join(["Production of final shapefile geometry of %s"%(os.path.splitext(out)[0] + str(valueclip) + ext), \ str(timeprodvect - timeinit)]), "seconds"])) for ext in ['.shp', '.dbf', '.shx', '.prj']: shutil.copyfile( os.path.splitext(outtmp)[0] + ext, os.path.splitext(out)[0] + str(valueclip) + ext) if os.path.exists(os.path.splitext(outtmp)[0] + ext): os.remove(os.path.splitext(outtmp)[0] + ext) if os.path.exists(os.path.join(localenv, "clean%s" % (ext))): os.remove(os.path.join(localenv, "clean%s" % (ext))) if os.path.exists(os.path.join(localenv, "clipped%s" % (ext))): os.remove(os.path.join(localenv, "clipped%s" % (ext))) if os.path.exists(os.path.join(localenv, "grassdata")): shutil.rmtree(os.path.join(localenv, "grassdata"))
def test_iota2_vectortools(self): """Test how many samples must be add to the sample set """ # Add Field for ext in ['.shp', '.dbf', '.shx', '.prj']: shutil.copyfile( os.path.splitext(self.classif)[0] + ext, os.path.splitext(self.classifwd)[0] + ext) afp.addFieldPerimeter(self.classifwd) tmpbuff = os.path.join(self.wd, "tmpbuff.shp") bfo.bufferPoly(self.classifwd, tmpbuff, -10) for ext in ['.shp', '.dbf', '.shx', '.prj']: shutil.copyfile( os.path.splitext(tmpbuff)[0] + ext, os.path.splitext(self.classifwd)[0] + ext) cnf.changeName(self.classifwd, "Classe", "class") self.assertEqual(vf.getNbFeat(self.classifwd), 144, "Number of features does not fit") self.assertEqual(vf.getFields(self.classifwd), [ 'Validmean', 'Validstd', 'Confidence', 'Hiver', 'Ete', 'Feuillus', 'Coniferes', 'Pelouse', 'Landes', 'UrbainDens', 'UrbainDiff', 'ZoneIndCom', 'Route', 'PlageDune', 'SurfMin', 'Eau', 'GlaceNeige', 'Prairie', 'Vergers', 'Vignes', 'Perimeter', 'class' ], "List of fields does not fit") self.assertEqual( vf.ListValueFields(self.classifwd, "class"), ['11', '12', '211', '222', '31', '32', '36', '42', '43', '51'], "Values of field 'class' do not fit") self.assertEqual( vf.getFieldType(self.classifwd, "class"), str, "Type of field 'class' (%s) do not fit, 'str' expected" % (vf.getFieldType(self.classifwd, "class"))) cfr.conFieldRecode(self.classifwd, "class", "mask", 11, 0) so.intersectSqlites(self.classifwd, self.inter, self.wd, self.outinter, 2154, "intersection", [ 'class', 'Validmean', 'Validstd', 'Confidence', 'ID', 'Perimeter', 'Aire', "mask" ]) check.checkGeometryAreaThreshField(self.outinter, 100, 1, self.classifwd) self.assertEqual(vf.getNbFeat(self.classifwd), 102, "Number of features does not fit") sba.extractFeatureFromShape(self.classifwd, 3, "mask", self.wd) mf.mergeVectors([ os.path.join(self.wd, "classif0_0.shp"), os.path.join(self.wd, "classif0_1.shp"), os.path.join(self.wd, "classif0_2.shp") ], self.classifwd) self.assertEqual(vf.getFirstLayer(self.classifwd), 'classif', "Layer does not exist in this shapefile") self.assertTrue( testutils.compareVectorFile(self.classifwd, self.classifout, 'coordinates', 'polygon', "ESRI Shapefile"), "Generated shapefile vector does not fit with shapefile reference file" )