Ejemplo n.º 1
0
def txt2geotiff(filename, delimiter, start, field_x, field_y, field_z, crs,
                output_directory, input_encoding):
    index_max = max([field_x, field_y, field_z])

    fp = codecs.open(file, "r", input_encoding)
    name, ext = os.path.splitext(os.path.basename(filename))
    output_file = os.path.join(output_directory, name) + ".tif"
    xyz_file = tempfile.NamedTemporaryFile(mode='w+t',
                                           delete=False,
                                           suffix=".xyz")

    row_num = 0
    for dummy in range(0, start - 1):
        fp.readline()
        row_num = row_num + 1

    rows = fp.readline()
    while rows:
        row_num = row_num + 1
        row = rows.split(delimiter)

        if len(row) < index_max:
            ProcessingLog().addToLog(
                ProcessingLog.LOG_INFO,
                "skip row({0}) : {1}".format(row_num, filename))
            continue
        xyz_file.write("{0} {1} {2}\n".format(row[field_x - 1],
                                              row[field_y - 1],
                                              row[field_z - 1]))

        rows = fp.readline()

    fp.close()
    xyz_file.close()

    dem_layer = QgsRasterLayer(xyz_file.name, "temp")
    dem_layer.setCrs(QgsCoordinateReferenceSystem(crs))

    extent = dem_layer.extent()
    projwin = "{0},{1},{2},{3}".format(extent.xMinimum(), extent.xMaximum(),
                                       extent.yMinimum(), extent.yMaximum())

    processing.runalg("gdalogr:translate", dem_layer, 100, True, "", 0, crs,
                      projwin, False, 5, 4, 75, 6, 1, False, 0, True, "",
                      output_file)