def main(): args = getArguments() raster = HASC() raster.initWithExtent(args.side, args.xmin, args.ymin, args.xmax, args.ymax) print("Geometries:" + "\n Hexagon cell area : " + str(raster.cellArea()) + "\n Hexagon side length : " + str(raster.side) + "\n Hexagon perpendicular : " + str(raster.hexPerp) + "\n Number of rows in mesh : " + str(raster.nrows) + "\n Number of columns in mesh : " + str(raster.ncols)) moduleName = args.module.rsplit('/', 1)[1].rsplit('.py', 1)[0] # Dynamically import surface function try: # Pyhton 2 runtime if sys.version_info[0] < 3: import imp module = imp.load_source(moduleName, args.module) else: # Python 3 runtime import importlib.util spec = importlib.util.spec_from_file_location( moduleName, args.module) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) function = getattr(module, args.function) except (Exception) as ex: print("Failed to import module or function: %s" % (ex)) sys.exit() for i in range(raster.ncols): for j in range(raster.nrows): x, y = raster.getCellCentroidCoords(i, j) raster.set(i, j, function(x, y)) try: raster.save(args.output) raster.saveAsGML(args.output + ".gml") except (ImportError, IOError) as ex: print("Error saving the raster %s: %s" % (args.output, ex)) sys.exit() print("Created new raster successfully")
def main(): args = getArguments() raster = HASC() raster.initWithExtent(args.side, args.xmin, args.ymin, args.xmax, args.ymax) print("Geometries:" + "\n Hexagon cell area : " + str(raster.cellArea()) + "\n Hexagon side length : " + str(raster.side) + "\n Hexagon perpendicular : " + str(raster.hexPerp) + "\n Number of rows in mesh : " + str(raster.nrows) + "\n Number of columns in mesh : " + str(raster.ncols)) moduleName = args.module.rsplit('/', 1)[1].rsplit('.py', 1)[0] # Dynamically import surface function try: # Pyhton 2 runtime if sys.version_info[0] < 3: import imp module = imp.load_source(moduleName, args.module) else: # Python 3 runtime import importlib.util spec = importlib.util.spec_from_file_location(moduleName, args.module) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) function = getattr(module, args.function) except(Exception) as ex: print("Failed to import module or function: %s" % (ex)) sys.exit() for i in range(raster.ncols): for j in range(raster.nrows): x, y = raster.getCellCentroidCoords(i, j) raster.set(i, j, function(x, y)) try: raster.save(args.output) raster.saveAsGML(args.output + ".gml") except (ImportError, IOError) as ex: print("Error saving the raster %s: %s" % (args.output, ex)) sys.exit() print("Created new raster successfully")
def main(): processArguments(sys.argv) hexRaster = HASC() try: hexRaster.loadFromFile(inputFile) except (ValueError, IOError) as ex: print("Error loading the raster %s: %s" % (inputFile, ex)) sys.exit() print ("Loaded input HASC, converting...") try: hexRaster.saveAsGML(outputFile) except (ImportError, IOError) as ex: print("Error saving the raster %s: %s" % (inputFile, ex)) sys.exit() print ("Conversion successfully completed.")
def run(self): """Run method that performs all the real work""" # show the dialog self.dlg.show() # Run the dialog event loop result = self.dlg.exec_() # See if OK was pressed if result: # Do something useful here - delete the line containing pass and # substitute with your code. fileName = self.dlg.lineEdit.text() # Load the HexASCII file hexASCII = HASC() try: hexASCII.loadFromFile(fileName) hexASCII.saveAsGML(fileName + ".gml") except (ValueError, IOError) as ex: self.iface.messageBar().pushMessage( "Error", "Failed to load the raster %s: %s" % (fileName, ex), level=QgsMessageBar.CRITICAL) # Add HexASCII to the layer heap vector = fileName.split("/") layerName = vector[len(vector) - 1] layerName = layerName.split(".")[0] layer = self.iface.addVectorLayer(fileName + ".gml", layerName, "ogr") if not layer: self.iface.messageBar().pushMessage( "Error", "Failed to add raster to the layer heap", level=QgsMessageBar.CRITICAL) self.createChoropleth(layer, hexASCII.min, hexASCII.max)
def main(): neighbours = 5 tolerance = 0.1 epsilon = 100 coords_list = [] values_list = [] args = getArguments() with open(args.input, 'rt') as csvfile: spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|') for row in spamreader: raw = str(row[0]).split(',') coords_list.append([float(raw[0]), float(raw[1])]) values_list.append(float(raw[2])) coords = numpy.array(coords_list) values = numpy.array(values_list) tree = spatial.KDTree(coords) # Set maximum and minimum admissable values max_value = values.max() + (values.max() - values.min()) * tolerance min_value = values.min() - (values.max() - values.min()) * tolerance hexRaster = HASC() hexRaster.initWithExtent(args.side, args.xmin, args.ymin, args.xmax, args.ymax) print("Geometries:" + "\n Hexagon cell area : " + str(hexRaster.cellArea()) + "\n Hexagon side length : " + str(hexRaster.side) + "\n Hexagon perpendicular : " + str(hexRaster.hexPerp) + "\n Number of rows in mesh : " + str(hexRaster.nrows) + "\n Number of columns in mesh : " + str(hexRaster.ncols)) for j in range(hexRaster.nrows): for i in range(hexRaster.ncols): xx = [] yy = [] vals = [] x, y = hexRaster.getCellCentroidCoords(i, j) d, ind = tree.query(numpy.array([x, y]),neighbours) for n in range(neighbours): xx.append(tree.data[ind[n]][0]) yy.append(tree.data[ind[n]][1]) vals.append(values[ind[n]]) f = interpolate.Rbf(xx, yy, vals, epsilon=epsilon) new_value = f(x,y) if new_value < min_value: hexRaster.set(i, j, min_value) elif new_value > max_value: hexRaster.set(i, j, max_value) else: hexRaster.set(i, j, new_value) hexRaster.save(args.output) hexRaster.saveAsGeoJSON(args.output + ".json") hexRaster.saveAsGML(args.output + ".gml") print("\nSuccessfully created new HexASCII raster.")
def main(): neighbours = 5 tolerance = 0.1 epsilon = 100 coords_list = [] values_list = [] args = getArguments() with open(args.input, 'rt') as csvfile: spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|') for row in spamreader: raw = str(row[0]).split(',') coords_list.append([float(raw[0]), float(raw[1])]) values_list.append(float(raw[2])) coords = numpy.array(coords_list) values = numpy.array(values_list) tree = spatial.KDTree(coords) # Set maximum and minimum admissable values max_value = values.max() + (values.max() - values.min()) * tolerance min_value = values.min() - (values.max() - values.min()) * tolerance hexRaster = HASC() hexRaster.initWithExtent(args.side, args.xmin, args.ymin, args.xmax, args.ymax) print("Geometries:" + "\n Hexagon cell area : " + str(hexRaster.cellArea()) + "\n Hexagon side length : " + str(hexRaster.side) + "\n Hexagon perpendicular : " + str(hexRaster.hexPerp) + "\n Number of rows in mesh : " + str(hexRaster.nrows) + "\n Number of columns in mesh : " + str(hexRaster.ncols)) # Supress warnings from numpy with warnings.catch_warnings(): warnings.filterwarnings('ignore', r'scipy.linalg.solve') for j in range(hexRaster.nrows): for i in range(hexRaster.ncols): xx = [] yy = [] vals = [] x, y = hexRaster.getCellCentroidCoords(i, j) d, ind = tree.query(numpy.array([x, y]), neighbours) for n in range(neighbours): xx.append(tree.data[ind[n]][0]) yy.append(tree.data[ind[n]][1]) vals.append(values[ind[n]]) try: f = interpolate.Rbf(xx, yy, vals, epsilon=epsilon) new_value = f(x, y) except (Exception) as ex: new_value = sum(vals) / len(vals) else: if new_value < min_value: hexRaster.set(i, j, min_value) elif new_value > max_value: hexRaster.set(i, j, max_value) else: hexRaster.set(i, j, new_value) hexRaster.save(args.output) hexRaster.saveAsGeoJSON(args.output + ".json") hexRaster.saveAsGML(args.output + ".gml") print("\nSuccessfully created new HexASCII raster.")