-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Write Geotiff Files #25
Comments
Please refer to our package, it provide an alternative solution for making geotiff tags. 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. |
Please kindly check the documentation for more details. |
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 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.
The text was updated successfully, but these errors were encountered: