Draw circle with longitude, latitude and radius (km) in cartopy of python - python

#!/usr/bin/env python
import os, sys
import pandas as pd
import cartopy
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import shapely.geometry as sgeom
import numpy as np
from cartopy.geodesic import Geodesic
if __name__ == '__main__':
stn = pd.read_csv('obs_station.csv')
gd = Geodesic()
lcc = ccrs.LambertConformal(central_longitude=126., central_latitude=38.)
fig = plt.figure(figsize=(7,7))
ax = fig.add_subplot(111, projection=lcc)
ax.coastlines(resolution='50m')
geoms = []
for lon, lat in zip(stn['longitude'], stn['latitude']):
cp = gd.circle(lon=lon, lat=lat, radius=250000.)
geoms.append(sgeom.Polygon(cp))
ax.add_geometries(geoms, crs=lcc, edgecolor='r')
ax.set_extent([120., 133., 30., 43.])
plt.show()
The file 'obs_station.csv' contain several coordinates of longitudes and latitudes.
Using code above, I try to draw circles with specific radius (250 km).
But, nothing is on the map as below. Only show the map with coastlines.
I don't know what is the problem. Help please.
Result

You did not get the plots of the circles because of wrong coordinate transformation you specifies in .add_geometries() statement.
To get it right, suppose I use this data file:
'obs_station.csv':
longitude,latitude
127.603897,36.932988
126.505337,38.555939
And the modified code:-
#import os, sys
import pandas as pd
import cartopy
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import shapely.geometry as sgeom
import numpy as np
from cartopy.geodesic import Geodesic
if __name__ == '__main__':
stn = pd.read_csv('obs_station.csv')
gd = Geodesic()
# This is long-lat coordinate system for use in ..
# .. coordinate transformation options
src_crs = ccrs.PlateCarree()
lcc = ccrs.LambertConformal(central_longitude=126., central_latitude=38.)
fig = plt.figure(figsize=(7,7))
ax = fig.add_subplot(111, projection=lcc)
ax.coastlines(resolution='50m')
geoms = []
for lon, lat in zip(stn['longitude'], stn['latitude']):
cp = gd.circle(lon=lon, lat=lat, radius=250000.)
#x,y = lcc.transform_point(lon, lat, src_crs)
#cp = gd.circle(lon=x, lat=y, radius=250000.)
geoms.append(sgeom.Polygon(cp))
# Note the specification of coordinate transformation, using the
# .. correct parameter: crs=src_crs
ax.add_geometries(geoms, crs=src_crs, edgecolor='r', alpha=0.5)
ax.set_extent([120., 133., 30., 43.])
plt.show()
The output:
In conclusion, your line of code:
ax.add_geometries(geoms, crs=lcc, edgecolor='r')
needs correct CRS. And the correct CRS is
ccrs.PlateCarree()

Related

Inscribing a smaller domain onto a cartopy map in Python

I've been working to make a visual for a poster regarding the physical domain that I am studying. I'm working with a nested domain, so I have 1 smaller domain inside a larger outer domain. I'm trying to create a cartopy plot that shows both the outer domain and inner domain. Ideally, the result would look something like this:
I'm really struggling with trying to get my smaller domain inscribed onto my map. I've attempted to make a Shapely LinearRing to show the inner domain, but it is not working. Here's the code I have created so far:
# Imports
import numpy as np
import sys, os
import matplotlib.pyplot as plt
%matplotlib inline
import netCDF4
from netCDF4 import Dataset
from matplotlib.cm import get_cmap
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.feature import NaturalEarthFeature, COLORS
import metpy as mp
import metpy.calc as mpcalc
from metpy.calc import divergence, smooth_gaussian
from metpy.units import units
import xarray as xr
from wrf import getvar, interplevel, to_np, latlon_coords, get_cartopy, cartopy_xlim, cartopy_ylim, ALL_TIMES
from shapely.geometry.polygon import LinearRing
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
from shapely import geometry
# Get 1km lats/lons
lats1km = getvar(ds1, 'lat')
lons1km = getvar(ds1, 'lon')
lat1km_max = to_np(np.max(lats1km))
lat1km_min = to_np(np.min(lats1km))
lon1km_max = to_np(np.max(lons1km))
lon1km_min = to_np(np.min(lons1km))
# Get 3km lats/lons
lats3km = getvar(ds3, 'lat')
lons3km = getvar(ds3, 'lon')
lat3km_max = to_np(np.max(lats3km))
lat3km_min = to_np(np.min(lats3km))
lon3km_max = to_np(np.max(lons3km))
lon3km_min = to_np(np.min(lons3km))
domain = [lon3km_min, lat3km_min, lon3km_max, lat3km_max]
lons = [lat1km_min, lat1km_min, lat1km_max, lat1km_max]
lats = [lon1km_min, lon1km_max, lon1km_max, lon1km_min]
ring = LinearRing(list(zip(lons, lats)))
geom = geometry.box(minx=lon1km_min, miny=lat1km_min, maxx=lon1km_max, maxy=lat1km_max)
# Grab CRS
crs = get_cartopy(wrfin=ds1)
# Create figure and axes
fig = plt.figure(figsize=(20,10))
ax0 = fig.add_subplot(1, 1, 1, projection=crs)
ax0.set_extent([lon3km_min, lon3km_max, lat3km_min, lat3km_max])
ax0.add_geometries([ring], crs=crs, facecolor='blue', edgecolor='black')
ax0.add_geometries([geom], crs=crs, alpha=0.3)
plot_background(ax0)
This yields my outer domain, but not my inner domain:
What am I doing wrong, and what can I do to get my inner domain shown on the map? Thank you for the help! I really appreciate it!
NOTE: I have already attempted the solution in this link. I am still unable to visualize my polygon.
Have a look at EOmaps ! (I'm the dev) it provides simple functions to add static (or interactive) indicators such as projected rectangles or ellipses to cartopy plots in 1 line!
from eomaps import Maps
m = Maps()
m.add_coastlines()
props = dict(xy=(10, 45), xy_crs=4326, radius_crs=4326, shape="rectangles")
m.add_marker(**props, radius=3, fc=(0,1,0,.5), ec="r", lw=2)
m.add_marker(**props, radius=5, fc="none", ec="k")
m.add_marker(**props, radius=(15, 10), fc="none", ec="m", ls="--", lw=2)
m.figure.ax.set_extent((-15., 65., -5., 75.))

why plt.savefig() doesn't work? When save figure, latitude does not show while longitude show correctly

I am plotting global distribution of precipitation. So my figure should have longitude on the x-axis and latitude on the y-axis.
The figure show correctly inside jupyter notebook, with latitude and longitude shown correctly as desired.
However, when saving the figure using plt.savefigure(), and check the saved figure, the latitude labels disappear...
Does anyone know the reason?
complete code is attached here for your reference.
import xarray as xr
import numpy as np
import glob
import copy
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.gridspec as gridspec
from matplotlib.ticker import (MultipleLocator, AutoMinorLocator)
import matplotlib.ticker as mticker
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import pandas as pd
import cartopy.crs as ccrs
%matplotlib inline
dpi = 100
fig = plt.figure(figsize=(1600/dpi, 1600/dpi), dpi=dpi)
spec = gridspec.GridSpec(ncols=2, nrows=4,figure=fig,left=0.15,bottom=0.1,right=0.7,top=0.9)
spec.update(wspace=0.2,hspace=0.3)
ax = []
for ii in range(8):
if ii <= 1:
ax.append(fig.add_subplot(spec[0, ii],projection=ccrs.PlateCarree(central_longitude=180.0))) #,frameon=False)
elif (ii >=2) and (ii <=3):
ax.append(fig.add_subplot(spec[1, ii-2],projection=ccrs.PlateCarree(central_longitude=180.0))) #,frameon=False)
elif (ii >=4) and (ii <=5):
ax.append(fig.add_subplot(spec[2, ii-4],projection=ccrs.PlateCarree(central_longitude=180.0))) #,frameon=False)
elif ii>=6:
ax.append(fig.add_subplot(spec[3, ii-6],projection=ccrs.PlateCarree(central_longitude=180.0))) #,frameon=False)
ax[-1].coastlines(resolution='110m',linewidth=0.8,color='black',linestyle='solid',alpha=0.7)
ax[-1].set_global()
ax[-1].set_title('',loc='left')
ax[-1].set_extent([0.,357.5,-90,90],crs=ccrs.PlateCarree())
gl = ax[-1].gridlines(crs=ccrs.PlateCarree(), draw_labels=True,linewidth=2, color='gray', alpha=1.0, linestyle='--')
gl.top_labels = False
gl.left_labels= True
gl.right_labels=False
gl.bottom_labels=True
gl.xlines = False
gl.ylines = False
gl.xlocator = mticker.FixedLocator([-120,-60,180,120,60])
gl.ylocator = mticker.FixedLocator([-90,-60,-30,0,30,60,90])
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
plt.savefig("***.jpg",bbox_inches='tight',dpi=300,pad_inches=0.15)
Thanks a lot in advance!

OSMNX and Shapely polygon cut

I wanted to spilt the polygon into a numbered grid consisting of many small squares/shapes but the result is a polygon split into four sections instead is there anything wrong with the code I have been trying to solve this for hours but not sure what to do:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import geopandas as gpd
import osmnx as ox
from descartes import PolygonPatch
from shapely.geometry import Polygon
city=ox.geocode_to_gdf("Paris,France")
geometry = city['geometry'].iloc[0]
geometry_split =
ox.utils_geo._quadrat_cut_geometry(geometry,
quadrat_width = 750)
polylist = [p for p in geometry_split]
west, south, east, north = city.unary_union.bounds
fig, ax = plt.subplots(figsize=(30,30))
for polygon, n in zip(geometry_split,
np.arange(len(polylist))):
p = polygon.representative_point().coords[:][0]
patch =
PolygonPatch(polygon,ec='#100000',alpha=0.5,
zorder=2)
ax.add_patch(patch)
plt.annotate(text=n, xy=p,
horizontalalignment='center', size=15)
ax.set_xlim(west, east)
ax.set_ylim(south, north)
ax.axis('off')
plt.show()
The result is shown below:

Python netcdf cartopy - Plotting a selection of data

I have a netcdf file ('test.nc'). The variables of the netcdf file are the following:
variables(dimensions): float64 lon(lon), float64 lat(lat), int32 crs(), int16 Band1(lat,lon)
I am interested in the ´Band1´ variable.
Using cartopy, I could plot the data using the following code:
import numpy as np
import pandas as pd
import gzip
from netCDF4 import Dataset,num2date
import time
import matplotlib.pyplot as plt
import os
import matplotlib as mplt
#mplt.use('Agg')
import cartopy.crs as ccrs
import cartopy.feature as cfea
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
projection=ccrs.PlateCarree()
bbox=[-180,180,-60,85];creg='glob'
mplt.rc('xtick', labelsize=9)
mplt.rc('ytick', labelsize=9)
nc = Dataset('test.nc','r')
lat = nc.variables['lat'][:]
lon = nc.variables['lon'][:]
kopi= (nc.variables['Band1'][:,:])
nc.close()
fig=plt.figure(figsize=(11,5))
ax=fig.add_subplot(1,1,1,projection=projection)
ax.set_extent(bbox,projection)
ax.add_feature(cfea.COASTLINE,lw=.5)
ax.add_feature(cfea.RIVERS,lw=.5)
ax.add_feature(cfea.BORDERS, linewidth=0.6, edgecolor='dimgray')
ax.background_patch.set_facecolor('.9')
levels=[1,4,8,11,14,17,21,25,29]
cmap=plt.cm.BrBG
norm=mplt.colors.BoundaryNorm(levels,cmap.N)
ddlalo=.25
pc=ax.contourf(lon,lat,kopi,levels=levels,transform=projection,cmap=cmap,norm=norm,extend='both')
divider = make_axes_locatable(ax)
ax_cb = divider.new_horizontal(size="3%", pad=0.1, axes_class=plt.Axes)
fig.colorbar(pc,extend='both', cax=ax_cb)
fig.add_axes(ax_cb)
fig.colorbar(pc,extend='both', cax=ax_cb)
ttitle='Jony'
ax.set_title(ttitle,loc='left',fontsize=9)
plt.show()
However, I would like just to plot a selection of values inside the variable ´Band1´. I thought I could use the following code:
kopi= (nc.variables['Band1'][:,:])<=3
However it does not work and instead of plotting the area corresponding to the value selection it selected the all map.
How could I select and plot a desired range of values inside the variables ´Band1´?
Just mask the values with np.nan
kopi[kopi <=3] = np.nan
This should yield to white pixels in your plot.
Please provide test data in the future.

How to use numpy to build a 3D-model?

Original(2018.11.01)
I have 3 numpy:x、y、z,created by my laser scanner(40 degree / 1 step).
I want to used them to build a 3D model.
I think it must should be use matplotlib.tri
But I have no idea to decide triangulated data
Here is my data :https://www.dropbox.com/s/d9p62kv9jcq9bwh/xyz.zip?dl=0
And Original model:https://i.imgur.com/XSyONff.jpg
Code:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.tri as mtri
x_all=np.load("x.npy")
y_all=np.load("y.npy")
z_all=np.load("z.npy")
tri = #I have no idea...
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_trisurf(x_all,y_all,z_all,triangles=tri.triangles)
Thank so much.
Update(2018.11.02)
I try this way to decide triangulated data
Delaunay Triangulation of points from 2D surface in 3D with python?
code:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.tri as mtri
from stl import mesh
x_all=np.load("x.npy")
y_all=np.load("y.npy")
z_all=np.load("z.npy")
model=np.vstack((x_all,y_all,z_all))
model=np.transpose(model)
model -= model.mean(axis=0)
rad = np.linalg.norm(model, axis=1)
zen = np.arccos(model[:,-1] / rad)
azi = np.arctan2(model[:,1], model[:,0])
tris = mtri.Triangulation(zen, azi)
plt.show()
And my model looks like:
https://i.stack.imgur.com/KVPHP.png
https://i.stack.imgur.com/LLQsQ.png
https://i.stack.imgur.com/HdzFm.png
Even though it has better surface on it,but there is a big hole over my model.Any idea to fixs it?
Assuming you want to reduce the complexity, i.e find triangles in your files to reduce the complexity. You may look into fitting a convex hull to your points, see here fore more info
Based on the file you provided this produces a surf plot of the object.
from numpy import load, stack
from matplotlib.pyplot import subplots
from mpl_toolkits.mplot3d import Axes3D
from scipy import spatial
x = load("x.npy")
y = load("y.npy")
z = load("z.npy")
points = stack((x,y,z), axis = -1)
v = spatial.ConvexHull(points)
fig, ax = subplots(subplot_kw = dict(projection = '3d'))
ax.plot_trisurf(*v.points.T, triangles = v.simplices.T)
fig.show()

Categories