Thank you
\nANT_basins_shp.zip
\nLAND_MASK_CRI-JPL_180x360_conservative.nc.zip
Finally got it working.. I think the issue was with the shapefile data itself. The coordinates of the shapefile were in '3031' projection, and just switching the proj=3031 in clipping wasn't working, so I had to reproject the shapefiles before hand.
\nnot sure if it's the best solution, but it's working.
import pandas as pd\n# import geopandas as gpd\nimport numpy as np\nimport rioxarray\nimport xarray as xr\nfrom cartopy import crs as ccrs#, feature as cfeature\nfrom matplotlib import pyplot as plt\nimport matplotlib.path as mpath\n\nfrom pyproj import Transformer\nimport shapefile \n#%% Open 1 degree mask\npath='/Users/ANT_basins_shp/'\nifile='LAND_MASK_CRI-JPL_180x360_conservative.nc'\nds=xr.open_dataset(path+ifile)\nds\n\nxda = xr.DataArray(data=ds.mask, \n dims=[\"y\", \"x\"], \n coords=dict(x=([\"x\"], ds.lon),\n y=([\"y\"], ds.lat)))\n# Convert longitudes to -180 to 180\nxda = xda.assign_coords(x=(((xda.x + 180) % 360) - 180))\n# # Order longitudes \nxda = xda.sortby('x')\nxda.plot()\n#% %\n# Set the projection\nproj=\"EPSG:3031\" #EPSG:3031: WGS 84 / Antarctic Polar Stereographic\n# proj=\"EPSG:4326\"\nxda=xda.rio.write_crs(proj)\n# xda.rio.set_crs(\"EPSG:3031\")\n#%% open shapefile\nifile='ANT_Basins_IMBIE2_v1.6'\nsf = shapefile.Reader(path+ifile+'.shp')\nshapes = sf.shapes()\n#%% Transformation function\ninProj=3031#EPSG:3031: WGS 84 / Antarctic Polar Stereographic\noutProj =4326\ntransformer = Transformer.from_crs(inProj, outProj)\nfor i in range(len(shapes)):\n #shape[0] represents the 1st polygon of the shapefile\n # for point in shapes[0].points:\n # print (point)\n # Get coordinates of each polygon:\n coords=[point for point in shapes[i].points]\n #Transform the coordinates from 3031 to 4326\n points=[pt for pt in transformer.itransform(coords)]\n # lat,lon = transformer.transform((lat,lon))\n x=np.array([p[1] for p in points])\n y=np.array([p[0] for p in points])\n df2=pd.DataFrame(x,columns=['lon'])\n df2['lat']=y\n coords = df2.values.tolist()\n # Build the geometries object\n geometries = [{'type': 'Polygon','coordinates': [coords]}]\n clipped = xda.rio.clip(geometries)\n
The final result, after correcting a bit to include the center of the pole:
\n
-
Hi, This is what I got so far:
Thank you ANT_basins_shp.zip |
Beta Was this translation helpful? Give feedback.
-
#230 may be helpful |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Finally got it working.. I think the issue was with the shapefile data itself. The coordinates of the shapefile were in '3031' projection, and just switching the proj=3031 in clipping wasn't working, so I had to reproject the shapefiles before hand.
not sure if it's the best solution, but it's working.