-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more layers and better functionaility for converting to geoJSON
- Loading branch information
1 parent
0d71e08
commit 8b1f8bd
Showing
30 changed files
with
208 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "Point", "coordinates": [-65.5, 43.5]}, "properties": {"Qsb_acc": null}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-64.5, 43.5]}, "properties": {"Qsb_acc": null}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-63.5, 43.5]}, "properties": {"Qsb_acc": null}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-62.5, 43.5]}, "properties": {"Qsb_acc": null}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-61.5, 43.5]}, "properties": {"Qsb_acc": null}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-60.5, 43.5]}, "properties": {"Qsb_acc": null}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-65.5, 44.5]}, "properties": {"Qsb_acc": 0.12185745686292648}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-64.5, 44.5]}, "properties": {"Qsb_acc": 0.12696601450443268}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-63.5, 44.5]}, "properties": {"Qsb_acc": null}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-62.5, 44.5]}, "properties": {"Qsb_acc": null}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-61.5, 44.5]}, "properties": {"Qsb_acc": null}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-60.5, 44.5]}, "properties": {"Qsb_acc": null}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-65.5, 45.5]}, "properties": {"Qsb_acc": 0.1550743579864502}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-64.5, 45.5]}, "properties": {"Qsb_acc": 0.1173614114522934}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-63.5, 45.5]}, "properties": {"Qsb_acc": 0.07875185459852219}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-62.5, 45.5]}, "properties": {"Qsb_acc": 0.09439173340797424}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-61.5, 45.5]}, "properties": {"Qsb_acc": 0.1454971432685852}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-60.5, 45.5]}, "properties": {"Qsb_acc": null}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-65.5, 46.5]}, "properties": {"Qsb_acc": 0.1303371787071228}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-64.5, 46.5]}, "properties": {"Qsb_acc": 0.1344454437494278}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-63.5, 46.5]}, "properties": {"Qsb_acc": null}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-62.5, 46.5]}, "properties": {"Qsb_acc": null}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-61.5, 46.5]}, "properties": {"Qsb_acc": null}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-60.5, 46.5]}, "properties": {"Qsb_acc": 0.1971033811569214}}]} |
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import xarray as xr | ||
import json | ||
import numpy as np | ||
|
||
# Load the .nc4 file | ||
data = xr.open_dataset("C:\\Users\\user\\Desktop\\Farmalytics\\RawData\\GLDAS_NOAH10_M.A202310.021.nc4.SUB.nc4") | ||
|
||
# Extract latitude, longitude, and the data of interest | ||
lat = data['lat'].values | ||
lon = data['lon'].values | ||
variable = data['Qsb_acc'].values # Replace 'Qsb_acc' with the actual variable name if different | ||
|
||
# Create a list of GeoJSON features | ||
features = [] | ||
|
||
for i in range(len(lat)): | ||
for j in range(len(lon)): | ||
value = variable[0, i, j] # Assuming time is the first dimension | ||
if np.isnan(value): | ||
value = None # Replace NaN with null | ||
else: | ||
value = float(value) # Convert float32 to standard Python float | ||
feature = { | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [float(lon[j]), float(lat[i])] | ||
}, | ||
"properties": { | ||
"Qsb_acc": value | ||
} | ||
} | ||
features.append(feature) | ||
|
||
# Create a GeoJSON FeatureCollection | ||
geojson_data = { | ||
"type": "FeatureCollection", | ||
"features": features | ||
} | ||
|
||
# Save the GeoJSON to a file | ||
with open("OutputData/output_data.geojson", "w") as f: | ||
json.dump(geojson_data, f) | ||
|
||
print("GeoJSON file created successfully.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import rasterio | ||
import json | ||
import numpy as np | ||
from rasterio.features import shapes | ||
from shapely.geometry import shape, mapping | ||
|
||
# Load a .tiff file and convert to GeoJSON | ||
def tiff_to_geojson(tiff_path, geojson_output_path): | ||
with rasterio.open(tiff_path) as src: | ||
# Read the first band of the raster | ||
band = src.read(1) | ||
mask = band != src.nodata | ||
|
||
# Extract shapes from the raster data | ||
results = ( | ||
{'properties': {'value': v}, 'geometry': s} | ||
for s, v in shapes(band, mask=mask, transform=src.transform) | ||
) | ||
|
||
# Convert the extracted shapes to GeoJSON format | ||
features = [] | ||
for result in results: | ||
geom = shape(result['geometry']) | ||
features.append({ | ||
'type': 'Feature', | ||
'geometry': mapping(geom), | ||
'properties': result['properties'] | ||
}) | ||
|
||
geojson = { | ||
'type': 'FeatureCollection', | ||
'features': features | ||
} | ||
|
||
# Write to GeoJSON file | ||
with open(geojson_output_path, 'w') as geojson_file: | ||
json.dump(geojson, geojson_file) | ||
|
||
print(f"GeoJSON file created successfully at: {geojson_output_path}") | ||
|
||
# Example usage | ||
tiff_to_geojson('RawData\GLDAS_NOAH025_3H.A20240629.0000.021.nc4.SUB.tif', 'OutputData/output_tiff_data.geojson') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters