Home > Mobile >  Is there a way to plot a line that is a consistent length using matplotlib/cartopy?
Is there a way to plot a line that is a consistent length using matplotlib/cartopy?

Time:12-02

I am using matplotlib and cartopy to draw lines overlaid on maps in python. As of right now, I am just identifying the lat/lon of two points and plotting a line between them. Since I am taking cross sections across these lines, I would like to find a way to make the line the same length (say 300km long) no matter where I place it on the map. Is this possible without just using trial and error and setting the points until they are the desired length?

lat1, lat2, lon1, lon2 = [34.5, 36, -100, -97] x, y = [lon1, lon2], [lat1, lat2]

ax1.plot(x, y, color="black", marker="o", zorder=3, transform = ccrs.PlateCarree(), linewidth = 2.5)

Here are the relevant parts of the code that I am using now. This works, but I am looking for a way to hold the line length constant rather than changing the values for the endpoints at "lat1, lat2, lon1, lon2." I envision setting a line length, a mid-point (lat/lon), and an angle that would pivot around that point. I don't know if that's even possible, but that's how I'd imagine it'd have to work!

enter image description here

CodePudding user response:

the easiest way to do this would probably be to re-project your data into an equidistant projection, such as azimuthal equidistant, then buffer the point by 300km. You could do this

import cartopy.crs as ccrs
import geopandas as gpd

point = gpd.GeoDataFrame(
    geometry=gpd.points_from_xy([-100], [34.5], crs="epsg:4326")
)

crs = ccrs.AzimuthalEquidistant(-100, 34.5)

circle = point.to_crs(crs).buffer(300000).boundary.to_crs("epsg:4326")

This creates an ellipsoid of points in lat/lon space (a circle in actual distance):

In [17]: circle.iloc[0]
Out[17]: <shapely.geometry.linestring.LineString at 0x18c3db7c0>

In [18]: circle.iloc[0].xy
Out[18]:
(array('d', [-96.73458302693649, -96.76051210175493, -96.81721890848735, -96.90389145413285, -97.0194601113924, -97.16261553916054, -97.33182721184983, -97.52536229026433, -97.74130463130324, -97.97757379088794, -98.23194392302969, -98.5020625175967, -98.78546895046141, -99.07961284313612, -99.38187224585855, -99.68957166961148, -100.0, -100.31042833038852, -100.61812775414145, -100.92038715686388, -101.21453104953859, -101.4979374824033, -101.76805607697031, -102.02242620911204, -102.25869536869676, -102.47463770973567, -102.66817278815017, -102.83738446083946, -102.98053988860761, -103.09610854586715, -103.18278109151265, -103.23948789824507, -103.26541697306351, -103.26003093177158, -103.22308261845816, -103.15462889147989, -103.05504203609833, -102.92501821713451, -102.7655823598689, -102.57808885099398, -102.3642174900262, -102.12596419991539, -101.86562612586265, -101.58578091250277, -101.28926014666801, -100.97911717692438, -100.65858975917035, -100.33105821410338, -100.0, -99.66894178589662, -99.34141024082963, -99.02088282307564, -98.710739853332, -98.41421908749726, -98.13437387413735, -97.87403580008461, -97.63578250997378, -97.42191114900605, -97.23441764013111, -97.07498178286552, -96.94495796390169, -96.84537110852011, -96.77691738154184, -96.73996906822842, -96.73458302693649]),
 array('d', [34.45635448578617, 34.191923718769814, 33.930839345631036, 33.67556892797355, 33.42850443127362, 33.191942126948454, 32.96806390193457, 32.75892001047773, 32.566413289704464, 32.39228485200862, 32.23810126231687, 32.105243205881486, 31.99489565146612, 31.90803951485474, 31.845444827911535, 31.80766541851577, 31.795035106337213, 31.80766541851577, 31.845444827911535, 31.90803951485474, 31.99489565146612, 32.105243205881486, 32.23810126231687, 32.392284852008615, 32.566413289704464, 32.75892001047773, 32.96806390193457, 33.191942126948454, 33.42850443127362, 33.67556892797355, 33.93083934563104, 34.191923718769814, 34.45635448578617, 34.72160994100843, 34.985136962504626, 35.244374905489536, 35.49678051263328, 35.739853647811024, 35.97116361010222, 36.18837573214065, 36.38927791396905, 36.57180669376817, 36.73407241410168, 36.874383010755274, 36.99126593481258, 37.08348772070849, 37.15007073604399, 37.19030669398129, 37.20376657546522, 37.19030669398129, 37.15007073604399, 37.08348772070849, 36.99126593481258, 36.87438301075528, 36.73407241410169, 36.57180669376818, 36.38927791396906, 36.18837573214065, 35.97116361010222, 35.739853647811024, 35.496780512633286, 35.244374905489536, 34.98513696250464, 34.72160994100843, 34.45635448578617]))
  • Related