Skip to content
\n

Thank you

\n

ANT_basins_shp.zip
\nLAND_MASK_CRI-JPL_180x360_conservative.nc.zip

","upvoteCount":1,"answerCount":2,"acceptedAnswer":{"@type":"Answer","text":"

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.

\n
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
\n

The final result, after correcting a bit to include the center of the pole:
\n\"image\"

","upvoteCount":1,"url":"https://github.com/corteva/rioxarray/discussions/278#discussioncomment-534724"}}}

Clipping in Antarctica #278

Answered by carocamargo
carocamargo asked this question in Q&A
Discussion options

You must be logged in to vote

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.

import pandas as pd
# import geopandas as gpd
import numpy as np
import rioxarray
import xarray as xr
from cartopy import crs as ccrs#, feature as cfeature
from matplotlib import pyplot as plt
import matplotlib.path as mpath

from pyproj import Transformer
import shapefile 
#%% Open 1 degree mask
path='/Users/ANT_basins_shp/'
ifile='LAND_MASK_CRI-JPL_180x360_conservative.nc'
ds=xr.open_data…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@carocamargo
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by carocamargo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants