0

I would like to create a tif file from a list of coordinate data and elevation data. They are data I created on my own for a toy problem I am trying to work with. The data are as follows:

-78.5000 32.5000 -78 1 1
-78.5000 32.5100 -78 1 2
-78.5000 32.5200 -78 1 3
-78.5000 32.5300 -78 1 4
-78.5000 32.5400 -78 1 5
-78.5000 32.5500 -78 1 6
-78.5000 32.5600 -78 1 7
-78.5000 32.5700 -78 1 8
-78.5000 32.5800 -78 1 9
-78.5000 32.5900 -78 1 10
-78.5000 32.6000 -78 1 11
-78.5000 32.6100 -78 1 12
-78.5000 32.6200 -78 1 13
-78.5000 32.6300 -78 1 14
-78.5000 32.6400 -78 1 15
-78.5000 32.6500 -78 1 16
-78.5000 32.6600 -78 1 17
-78.5000 32.6700 -78 1 18
-78.5000 32.6800 -78 1 19
-78.5000 32.6900 -78 1 20
-78.5000 32.7000 -78 1 21
-78.5000 32.7100 -78 1 22
-78.5000 32.7200 -78 1 23
-78.5000 32.7300 -78 1 24
-78.5000 32.7400 -78 1 25
-78.5000 32.7500 -78 1 26
...

The first column is the long, second is lat, third is depth in feet. The fourth and fifth are another type of coordinate data for my particular problem (i,j) values for each of the grid cells.

How do I create a tif file of these data? Is there a way to do this in python?

summer
  • 37
  • 6
  • What have you tried? Any code? Just to be clear, what are you referring to when you say “tif”? The only “tif” i’m familiar with is the [Tagged Image File Format](https://en.wikipedia.org/wiki/TIFF). – AMC Nov 11 '19 at 22:26
  • @AlexanderCécile yes that is the tif I am referring to. And I haven't found much to try. I tried using this [link](https://stackoverflow.com/questions/33537599/how-do-i-write-create-a-geotiff-rgb-image-file-in-python), but was unable to figure out how to get it to work for me. I have only succeeded in creating a .csv file. – summer Nov 12 '19 at 15:09
  • What is your goal in creating a TIFF file? What is it for? – AMC Nov 12 '19 at 15:11
  • I need to use it as input for raster stats [link](https://pythonhosted.org/rasterstats/) - I am trying to index raster points (so the long lat coordinates and depth values) that are inside a polygon from a shapefile I have. Then I need to perform statistics on them. – summer Nov 12 '19 at 15:16
  • Alright i'll try to give it ago. – AMC Nov 12 '19 at 22:03

2 Answers2

1

I was almost successful in answering my own question using an answer here:

from osgeo import gdal
from osgeo import osr
import numpy as np

image_size = (201,201)
lon = np.zeros((image_size), dtype=np.float64)
lat = np.zeros((image_size), dtype=np.float64)
red = np.zeros((image_size), dtype=np.int8)
for x in range(0,image_size[0]):
    for y in range(0,image_size[1]):
        lon[y,x] =  -1*(78.5+0.01*-x)
        lat[y,x] =  32.5+0.01*y
        red[y,x] =  lon[y,x]

# set geotransform
nx = red.shape[0]
ny = red.shape[0]
xmin, ymin, xmax, ymax = [lon.min(), lat.min(), lon.max(), lat.max()]
xres = (xmax - xmin) / float(nx)
yres = (ymax - ymin) / float(ny)
geotransform = (xmin, xres, 0, ymax, 0, -yres)

# create the 1-band raster file
dst_ds = gdal.GetDriverByName('GTiff').Create('myGeoTIFF.tif', ny, nx, 1, gdal.GDT_Byte)
dst_ds.SetGeoTransform(geotransform)    # specify coords
srs = osr.SpatialReference()            # establish encoding
srs.ImportFromEPSG(3857)                # WGS84 lat/long
dst_ds.SetProjection(srs.ExportToWkt()) # export coords to file
dst_ds.GetRasterBand(1).WriteArray(red)   # write r-band to the raster
dst_ds.FlushCache()                     # write to disk
dst_ds = None                           # save, close

However, it is writing the red values as positive values instead of negative. The red array is -78..-76 but it writes them as 180..178 and I do not know why.

summer
  • 37
  • 6
0

I had the same goal as you, and the last piece of code you posted worked wonders for me. The values issue seem to come from the fact that you can't write negative values in a tiff file, at least using this method, and it loops around 255.

Paul
  • 1
  • 1
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/32829722) – Michael Halim Oct 05 '22 at 08:17