Open KipCrossing opened 3 years ago
Please refer to our package, it provide an alternative solution for making geotiff tags. https://github.com/UTokyo-FieldPhenomics-Lab/EasyIDP/blob/ff687a6d3e27a0155d137019a15ba8461ce2222e/easyidp/geotiff.py#L351
But it only suitable for cutting a region from given geotiff (only modify the left-top corner & size for cropped region to save), unable to make a new geotiff and save.
Please refer to our package, it provide an alternative solution for making geotiff tags. https://github.com/UTokyo-FieldPhenomics-Lab/EasyIDP/blob/ff687a6d3e27a0155d137019a15ba8461ce2222e/easyidp/geotiff.py#L351
But it only suitable for cutting a region from given geotiff (only modify the left-top corner & size for cropped region to save), unable to make a new geotiff and save.
Very nice! Thanks for the reference!
PS, I also work in agriculture. Would love to know more about the applications for your package. Send me an email if you can.
Very nice! Thanks for the reference!
PS, I also work in agriculture. Would love to know more about the applications for your package. Send me an email if you can.
Please kindly check the documentation for more details. https://easyidp.readthedocs.io/en/latest/index.html
Sooooo... I just got a solution from ChatGTP:
import xarray as xr
import tifffile
import tifftools
# Load the data and create a tifffile object
data = xr.open_dataset('data.nc')
tiff = tifffile.TiffFile('output.tiff', mode='w')
# Extract the data and latitude and longitude coordinates from the xarray
data_array = data['data'].values
lats = data['lat'].values
lons = data['lon'].values
# Set the CRS
crs = 'EPSG:4326'
# Create the GeoTiff tags
tags = tifftools.create_geotiff_tags(data_array, crs, lats, lons)
# Add the tags to the tifffile object
tiff.append(data_array, tags)
# Close the tifffile object
tiff.close()
This script assumes that you have an xarray with data, latitude, and longitude coordinates stored in a file called 'data.nc' and that you want to save it as a geotiff with the CRS 'EPSG:4326'. It loads the xarray and extracts the data, latitude, and longitude coordinates. It then creates a tifffile object and uses the tifftools.create_geotiff_tags() function to create the GeoTiff tags for the data. The tags are then added to the tifffile object using the append() method, and the tifffile object is closed to save the geotiff.
Note that this script assumes that the latitude and longitude coordinates are stored as 1D arrays, but if they are stored as 2D arrays, you can use the create_geotiff_tags_from_grid() function instead of the create_geotiff_tags() function.
I haven't tested it out yet, But it's good to know. Obv I don't need the xarray stuff.
I have digged the source code of tifftools, no api or function named create_geotiff_tags
, and the only close one is write_tag_data
, but seems not expected.
def write_tag_data(dest, src, offsets, lengths, srclen):
"""
Copy data from a source tiff to a destination tiff, return a list of
offsets where data was written.
:param dest: the destination file, opened to the location to write.
:param src: the source file.
:param offsets: an array of offsets where data will be copied from.
:param lengths: an array of lengths to copy from each offset.
:param srclen: the length of the source file.
:return: the offsets in the destination file corresponding to the data
copied.
"""
Looks like this might be a major problem with chatGTP. That is; when the solution is not obvious, it will make up a solution based on features that don't exist.
Hi, previously, I have been using rasterio-based implementation of writing geotiffs (check https://github.com/rasterio/rasterio/blob/main/examples/reproject.py#L44-L55). If that's what you are looking for, maybe I can help in implementing the write geotiff files feature.
Writing data to a tiff file should be easy via the tifffile package
And writing the tags should also be straightforward using the tifftools package
The hard part is mapping the chosen CRS to the Geotiff tags correctly.