def exist(self): """Return if the Vector exists or not""" if self.name: if self.mapset == '': mapset = utils.get_mapset_vector(self.name, self.mapset) self.mapset = mapset if mapset else '' return True if mapset else False return bool(utils.get_mapset_vector(self.name, self.mapset)) else: return False
def _get_vector_features_as_wkb_list(lock, conn, data): """Return vector layer features as wkb list supported feature types: point, centroid, line, boundary, area :param lock: A multiprocessing.Lock instance :param conn: A multiprocessing.Pipe instance used to send True or False :param data: The list of data entries [function_id,name,mapset,extent, feature_type, field] """ wkb_list = None try: name = data[1] mapset = data[2] extent = data[3] feature_type = data[4] field = data[5] bbox = None mapset = utils.get_mapset_vector(name, mapset) if not mapset: raise ValueError("Unable to find vector map <%s>"%(name)) layer = VectorTopo(name, mapset) if layer.exist() is True: if extent is not None: bbox = basic.Bbox(north=extent["north"], south=extent["south"], east=extent["east"], west=extent["west"]) layer.open("r") if feature_type.lower() == "area": wkb_list = layer.areas_to_wkb_list(bbox=bbox, field=field) else: wkb_list = layer.features_to_wkb_list(bbox=bbox, feature_type=feature_type, field=field) layer.close() except: raise finally: conn.send(wkb_list)
def _get_vector_table_as_dict(lock, conn, data): """Get the table of a vector map layer as dictionary :param lock: A multiprocessing.Lock instance :param conn: A multiprocessing.Pipe instance used to send True or False :param data: The list of data entries [function_id, name, mapset, where] """ ret = None try: name = data[1] mapset = data[2] where = data[3] mapset = utils.get_mapset_vector(name, mapset) if not mapset: raise ValueError("Unable to find vector map <%s>"%(name)) layer = VectorTopo(name, mapset) if layer.exist() is True: layer.open("r") columns = None table = None if layer.table is not None: columns = layer.table.columns table = layer.table_to_dict(where=where) layer.close() ret = {} ret["table"] = table ret["columns"] = columns except: raise finally: conn.send(ret)
raise GrassError(_("Unable to read area with id %i"%(a_id))) pcat = None c_ok = libvect.Vect_get_area_cats(self.c_mapinfo, a_id, ctypes.byref(line_c)) if c_ok == 0: # Centroid found ok = libvect.Vect_cat_get(ctypes.byref(line_c), field, ctypes.byref(cat)) if ok > 0: pcat = cat.value l.append((a_id, pcat, ctypes.string_at(barray, size.value))) libgis.G_free(barray) return l return None if __name__ == "__main__": import doctest from grass.pygrass import utils utils.create_test_vector_map(test_vector_name) doctest.testmod() """Remove the generated vector map, if exist""" from grass.pygrass.utils import get_mapset_vector from grass.script.core import run_command mset = get_mapset_vector(test_vector_name, mapset='') if mset: run_command("g.remove", flags='f', type='vector', name=test_vector_name)
16 None 41 >>> provider.stop() .. """ self.check_server() self.client_conn.send([RPCDefs.GET_VECTOR_FEATURES_AS_WKB, name, mapset, extent, feature_type, field]) return self.safe_receive("get_vector_features_as_wkb_list") if __name__ == "__main__": import doctest from grass.pygrass import utils from grass.pygrass.modules import Module Module("g.region", n=40, s=0, e=40, w=0, res=10) Module("r.mapcalc", expression="%s = row() + (10 * col())"%(test_raster_name), overwrite=True) utils.create_test_vector_map(test_vector_name) doctest.testmod() """Remove the generated maps, if exist""" mset = utils.get_mapset_raster(test_raster_name, mapset='') if mset: Module("g.remove", flags='f', type='raster', name=test_raster_name) mset = utils.get_mapset_vector(test_vector_name, mapset='') if mset: Module("g.remove", flags='f', type='vector', name=test_vector_name)
self._write(final) if __name__ == "__main__": import doctest from grass.pygrass import utils from grass.script.core import run_command utils.create_test_vector_map(test_vector_name) run_command("g.region", n=50, s=0, e=60, w=0, res=1) run_command("r.mapcalc", expression="%s = 1" % (test_raster_name), overwrite=True) run_command("g.region", n=40, s=0, e=40, w=0, res=2) doctest.testmod() # Remove the generated vector map, if exist mset = utils.get_mapset_vector(test_vector_name, mapset='') if mset: run_command("g.remove", flags='f', type='vector', name=test_vector_name) mset = utils.get_mapset_raster(test_raster_name, mapset='') if mset: run_command("g.remove", flags='f', type='raster', name=test_raster_name)
c_ok = libvect.Vect_get_area_cats(self.c_mapinfo, a_id, ctypes.byref(line_c)) if c_ok == 0: # Centroid found ok = libvect.Vect_cat_get(ctypes.byref(line_c), field, ctypes.byref(cat)) if ok > 0: pcat = cat.value l.append((a_id, pcat, ctypes.string_at(barray, size.value))) libgis.G_free(barray) return l return None if __name__ == "__main__": import doctest from grass.pygrass import utils utils.create_test_vector_map(test_vector_name) doctest.testmod() """Remove the generated vector map, if exist""" from grass.pygrass.utils import get_mapset_vector from grass.script.core import run_command mset = get_mapset_vector(test_vector_name, mapset='') if mset: run_command("g.remove", flags='f', type='vector', name=test_vector_name)
def write_structures( plants, output, elev, stream=None, ndigits=0, resolution=None, contour="", overwrite=False, ): """Write a vector map with the plant structures""" def write_hydrostruct(out, hydro, plant): pot = plant.potential_power( intakes=[ hydro.intake, ] ) (plant_id, itk_id, side, disch, gross_head) = ( plant.id, hydro.intake.id, hydro.side, float(hydro.intake.discharge), float(hydro.intake.elevation - plant.restitution.elevation), ) out.write(hydro.conduct, (plant_id, itk_id, disch, 0.0, 0.0, "conduct", side)) out.write( hydro.penstock, (plant_id, itk_id, disch, gross_head, pot, "penstock", side) ) out.table.conn.commit() tab_cols = [ (u"cat", "INTEGER PRIMARY KEY"), (u"plant_id", "VARCHAR(10)"), (u"intake_id", "INTEGER"), (u"discharge", "DOUBLE"), (u"gross_head", "DOUBLE"), (u"power", "DOUBLE"), (u"kind", "VARCHAR(10)"), (u"side", "VARCHAR(10)"), ] with VectorTopo(output, mode="w", overwrite=overwrite) as out: link = Link(layer=1, name=output, table=output, driver="sqlite") out.open("w") out.dblinks.add(link) out.table = out.dblinks[0].table() out.table.create(tab_cols) print("Number of plants: %d" % len(plants)) # check if contour vector map is provide by the user if contour: cname, cmset = contour.split("@") if "@" in contour else (contour, "") # check if the map already exist if bool(utils.get_mapset_vector(cname, cmset)) and overwrite: compute_contour = True remove = False else: # create a random name contour = "tmp_struct_contour_%05d_%03d" % ( os.getpid(), random.randint(0, 999), ) compute_contour = True remove = True if compute_contour: # compute the levels of the contour lines map levels = [] for p in plants.values(): for itk in p.intakes: levels.append( closest(itk.elevation, ndigits=ndigits, resolution=resolution) ) levels = sorted(set(levels)) # generate the contur line that pass to the point r.contour( input="%s@%s" % (elev.name, elev.mapset), output=contour, step=0, levels=levels, overwrite=True, ) # open the contur lines with VectorTopo(contour, mode="r") as cnt: for plant in plants.values(): print(plant.id) for options in plant.structures( elev, stream=stream, ndigits=ndigits, resolution=resolution, contour=cnt, ): for hydro in options: print("writing: ", hydro.intake) write_hydrostruct(out, hydro, plant) if remove: cnt.remove()
def write_structures(plants, output, elev, stream=None, ndigits=0, resolution=None, contour='', overwrite=False): """Write a vector map with the plant structures""" def write_hydrostruct(out, hydro, plant): pot = plant.potential_power(intakes=[ hydro.intake, ]) (plant_id, itk_id, side, disch, gross_head) = (plant.id, hydro.intake.id, hydro.side, float(hydro.intake.discharge), float(hydro.intake.elevation - plant.restitution.elevation)) out.write(hydro.conduct, (plant_id, itk_id, disch, 0., 0., 'conduct', side)) out.write(hydro.penstock, (plant_id, itk_id, disch, gross_head, pot, 'penstock', side)) out.table.conn.commit() tab_cols = [ (u'cat', 'INTEGER PRIMARY KEY'), (u'plant_id', 'VARCHAR(10)'), (u'intake_id', 'INTEGER'), (u'discharge', 'DOUBLE'), (u'gross_head', 'DOUBLE'), (u'power', 'DOUBLE'), (u'kind', 'VARCHAR(10)'), (u'side', 'VARCHAR(10)'), ] with VectorTopo(output, mode='w', overwrite=overwrite) as out: link = Link(layer=1, name=output, table=output, driver='sqlite') out.open('w') out.dblinks.add(link) out.table = out.dblinks[0].table() out.table.create(tab_cols) print('Number of plants: %d' % len(plants)) # check if contour vector map is provide by the user if contour: cname, cmset = (contour.split('@') if '@' in contour else (contour, '')) # check if the map already exist if bool(utils.get_mapset_vector(cname, cmset)) and overwrite: compute_contour = True remove = False else: # create a random name contour = 'tmp_struct_contour_%05d_%03d' % (os.getpid(), random.randint(0, 999)) compute_contour = True remove = True if compute_contour: # compute the levels of the contour lines map levels = [] for p in plants.values(): for itk in p.intakes: levels.append( closest(itk.elevation, ndigits=ndigits, resolution=resolution)) levels = sorted(set(levels)) # generate the contur line that pass to the point r.contour(input='%s@%s' % (elev.name, elev.mapset), output=contour, step=0, levels=levels, overwrite=True) # open the contur lines with VectorTopo(contour, mode='r') as cnt: for plant in plants.values(): print(plant.id) for options in plant.structures(elev, stream=stream, ndigits=ndigits, resolution=resolution, contour=cnt): for hydro in options: print('writing: ', hydro.intake) write_hydrostruct(out, hydro, plant) if remove: cnt.remove()