How to calculate area of a polygon with latitude and longitude? - python

I have the following method that checks whether is points are polygon
from shapely import wkt
def validate_polygon(points):
try:
wkt.loads("POLYGON((%s))" % points)
except Exception as ex:
raise WrongRequestDataError("Incorrect points format. " + str(ex))
How can I calculate area of the polygon and throw exception if it's greater than 400 square miles?
I have tried to check this:
polygon = wkt.loads("POLYGON((%s))" % "34.093523 -118.274893,34.091414 -118.275887,34.092082 -118.278062,34.093867 -118.276609,34.093523 -118.274893")
print(polygon.area)
4.406979500001112e-06
but it seems like the incorrect answer or what type of units it uses for this value and how to translate it to square miles or km^2?

typically you would compute the area like this:
from shapely.geometry import Polygon
points = [(34.093523, -118.274893), (34.091414, -118.275887), (34.092082, -118.278062), (34.093867, -118.276609), (34.093523, -118.274893)]
polygon = Polygon(points)
# the area in square degrees
area_sdeg = polygon.area
note: for area calculation in square meters, you have to use projections as described in https://gist.github.com/robinkraft/c6de2f988c9d3f01af3c

This is the best solution that works perfectly for me
def compute_polygon_area(points):
coordinates = (tuple(map(float, x.split())) for x in points.split(', '))
xy_coordinates = switch_to_xy_coordinates(coordinates)
return Polygon(xy_coordinates).area
def switch_to_xy_coordinates(coordinates):
earth_radius = 6371 # in km
lat_dist = pi * earth_radius / 180.0
latitudes, longitudes = zip(*coordinates)
y = (lat * lat_dist for lat in latitudes)
x = (lon * lat_dist * cos(radians(lat))
for lat, lon in zip(latitudes, longitudes))
return list(zip(x, y))

Related

I have a question related to add a point to a GPS point

I had a question that I have a GPS point (lat long) and I'd like to add noise that gaussian distribution with radius 10m. How should I write a python code?
Generate random bearing and gaussian distribution of distance. Then calculate Destination point given distance and bearing from start point as described here
def gauss_spread(lat, lon, radius):
angle = random.random()*2.0*math.pi
arcdist = random.gauss(0, radius) / 6371000
rlat = math.radians(lat)
newlat = math.asin(math.sin(rlat)*math.cos(arcdist) + math.cos(rlat)*math.sin(arcdist)*math.cos(angle))
newlon = lon + math.degrees(math.atan2(math.sin(angle)*math.sin(arcdist)*math.cos(rlat),math.cos(arcdist)-math.sin(rlat)*math.sin(newlat)))
return math.degrees(newlat), newlon
For small radii you can try the next approach. Generate random angle direction and gaussian distribution of distance (in degrees of earth arc). Then make correction by latitude factor.
import random, math
def gauss_spread_approx(lat, lon, radius):
angle = random.random()*2.0*math.pi
arcdist = random.gauss(0, radius * 180.0 / 20000000.0)
return lat + arcdist * math.sin(angle), \
lon + arcdist * math.cos(angle) / math.cos(math.radians(lat))
print(gauss_spread_approx(45, 0, 111111))

How to calculate size in m2 of each lat/long grid square

I have estimates for data in units m2 at gridsquare resolution. I need to calculate the number of m2 in each latitude / longitude grid cell?
Cell sizes are much smaller near the poles than at the equator so this is important.
I would like a netcdf file or array of the number of square meters in each grid square.
In case anyone would like a netcdf of the number of square meters in each lat long grid cell.
This is probably not the cleanest solution, but will create a netcdf (earth_m2.nc) of m2 in each grid using xarray.
The gridsize() function is adapted from another stack overflow question.
We can then make a dummy array and create a earth field of m2s using the longitude distances at each location.
"""
This will create a global grid of the approximate size of each grid square.
"""
import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
def gridsize(lat1):
#https://en.wikipedia.org/wiki/Haversine_formula
#https://stackoverflow.com/questions/639695/how-to-convert-latitude-or-longitude-to-meters/11172685#11172685
lon1=200
import math
lat2=lat1
lon2=lon1+1
R = 6378.137 # // Radius of earth in km
dLat = lat2 * np.pi / 180 - lat1 * np.pi / 180
dLon = lon2 * np.pi / 180 - lon1 * np.pi / 180
a = np.sin(dLat/2) * np.sin(dLat/2) + np.cos(lat1 * np.pi / 180) * np.cos(lat2 * np.pi / 180) * np.sin(dLon/2) * np.sin(dLon/2)
c = 2 * np.arctan2(np.sqrt(a), np.sqrt(1-a))
d = R * c
return d * 1000 #; // meters
boxlo,boxla=np.array(np.meshgrid(np.arange(-179.5,179.5,1),np.arange(-89.5,89.5,1)))
sizes=np.ones(boxlo.shape)
grid=gridsize(boxla)
grid_nc=xr.DataArray(grid,coords={'lat':boxla[:,1],'lon':boxlo[1,:]},dims=['lat','lon'])
lat_size=110567 #in m
grid_nc['m2']=grid_nc*lat_size
grid_nc=grid_nc['m2']
grid_nc.to_netcdf('earth_m2.nc')
plt.pcolormesh(boxlo[1,:],boxla[:,1],grid_nc)
plt.colorbar()
plt.show()
By spherical trigonometry, the surface area of a triangle on a sphere of radius R with 1 vertex at the North pole (latitude: π/2) and 2 vertices at the same latitude -π/2 < x < π/2 separated (longitudinally) by d radians is
S(x, d) = (cos⁻¹((cos(b) - cos²(a))/sin²(a)) + 2cos⁻¹((cos(a) - cos(a)cos(b))/(sin(a)sin(b))) - π)R² where a = R(π/2 - x) and b = Rd
So, the surface area of a grid rectangle on a sphere of radius R between lines of longitude separated by d radians and latitudes x₁ > x₂ is
S(x₂, d) - S(x₁, d)
One option is to transform your cells into a coordinate reference system (CRS) that has units in, say, meters rather than degrees. Then the area calculation is simple.
I assume your coordinates are in WGS84.
For the target CRS there are choices especially if you know the locality of the points, but a common collection of global CRSs like this are Universal Transverse Mercator (UTM), or near the poles Universal Polar Stereographic
For example, for UTM, assuming a list of points of the form [lon, lat] where the last point is equal to the first:
import pyproj
from shapely.geometry import Polygon
from shapely.ops import transform
def utm_epsg(lon: float, lat: float) -> int:
"""
Return the UTM EPSG code for the given lon-lat.
"""
offset = int(round((183 + lon) / 6.0))
return 32600 + offset if lat > 0 else 32700 + offset
for lat in range(-79, 83):
for lon in range(-179, 179):
polygon = Polygon([
[lon, lat],
[lon+1, lat],
[lon+1, lat+1],
[lon, lat+1],
[lon, lat],
])
src_crs = pyproj.CRS.from_epsg(4326)
tgt_crs = pyproj.CRS.from_epsg(utm_epsg(polygon.centroid.x, polygon.centroid.y))
project = pyproj.Transformer.from_crs(src_crs, tgt_crs, always_xy=True).transform
utm_polygon = transform(project, polygon)
# aggregate into some result. Here just printed to stdout.
print(polygon.centroid, utm_polygon.area)
It's worth noting that UTM isn't defined south of 80°S and north of 84°N.

Skyfield visible area underneath EarthSatellite

How would I calculate the area below an EarthSatellite so that I can plot the swath of land covered as the satellite passes over?
Is there anything in Skyfield that would facilitate that?
Edit: Just thought I'd clarify what I mean by area below the satellite. I need to plot the maximum area below the satellite possible to observe given that the Earth is a spheroid. I know how to plot the satellite path, but now I need to plot some lines to represent the area visible by that satellite as it flies over the earth.
Your edit made it clear what you want. The visible area from a satellite can be easily calculated (when the earth is seen as a sphere). A good source to get some background on the visible portion can be found here. To calculate the visible area when the earth is seen as an oblate spheroid will be a lot harder (and maybe even impossible). I think it's better to reform that part of the question and post it on Mathematics.
If you want to calculate the visible area when the earth is seen as a sphere we need to make some adjustments in Skyfield. With a satellite loaded using the TLE api you can easily get a sub point with the position on earth. The library is calling this the Geocentric position, but actually it's the Geodetic position (where the earth is seen as an oblate spheroid). To correct this we need to adjust subpoint of the Geocentric class to use the calculation for the Geocentric position and not the Geodetic position. Due to a bug and missing information in the reverse_terra function we also need to replace that function. And we need to be able to retrieve the earth radius. This results in the following:
from skyfield import api
from skyfield.positionlib import ICRF, Geocentric
from skyfield.constants import (AU_M, ERAD, DEG2RAD,
IERS_2010_INVERSE_EARTH_FLATTENING, tau)
from skyfield.units import Angle
from numpy import einsum, sqrt, arctan2, pi, cos, sin
def reverse_terra(xyz_au, gast, iterations=3):
"""Convert a geocentric (x,y,z) at time `t` to latitude and longitude.
Returns a tuple of latitude, longitude, and elevation whose units
are radians and meters. Based on Dr. T.S. Kelso's quite helpful
article "Orbital Coordinate Systems, Part III":
https://www.celestrak.com/columns/v02n03/
"""
x, y, z = xyz_au
R = sqrt(x*x + y*y)
lon = (arctan2(y, x) - 15 * DEG2RAD * gast - pi) % tau - pi
lat = arctan2(z, R)
a = ERAD / AU_M
f = 1.0 / IERS_2010_INVERSE_EARTH_FLATTENING
e2 = 2.0*f - f*f
i = 0
C = 1.0
while i < iterations:
i += 1
C = 1.0 / sqrt(1.0 - e2 * (sin(lat) ** 2.0))
lat = arctan2(z + a * C * e2 * sin(lat), R)
elevation_m = ((R / cos(lat)) - a * C) * AU_M
earth_R = (a*C)*AU_M
return lat, lon, elevation_m, earth_R
def subpoint(self, iterations):
"""Return the latitude an longitude directly beneath this position.
Returns a :class:`~skyfield.toposlib.Topos` whose ``longitude``
and ``latitude`` are those of the point on the Earth's surface
directly beneath this position (according to the center of the
earth), and whose ``elevation`` is the height of this position
above the Earth's center.
"""
if self.center != 399: # TODO: should an __init__() check this?
raise ValueError("you can only ask for the geographic subpoint"
" of a position measured from Earth's center")
t = self.t
xyz_au = einsum('ij...,j...->i...', t.M, self.position.au)
lat, lon, elevation_m, self.earth_R = reverse_terra(xyz_au, t.gast, iterations)
from skyfield.toposlib import Topos
return Topos(latitude=Angle(radians=lat),
longitude=Angle(radians=lon),
elevation_m=elevation_m)
def earth_radius(self):
return self.earth_R
def satellite_visiable_area(earth_radius, satellite_elevation):
"""Returns the visible area from a satellite in square meters.
Formula is in the form is 2piR^2h/R+h where:
R = earth radius
h = satellite elevation from center of earth
"""
return ((2 * pi * ( earth_radius ** 2 ) *
( earth_radius + satellite_elevation)) /
(earth_radius + earth_radius + satellite_elevation))
stations_url = 'http://celestrak.com/NORAD/elements/stations.txt'
satellites = api.load.tle(stations_url)
satellite = satellites['ISS (ZARYA)']
print(satellite)
ts = api.load.timescale()
t = ts.now()
geocentric = satellite.at(t)
geocentric.subpoint = subpoint.__get__(geocentric, Geocentric)
geocentric.earth_radius = earth_radius.__get__(geocentric, Geocentric)
geodetic_sub = geocentric.subpoint(3)
print('Geodetic latitude:', geodetic_sub.latitude)
print('Geodetic longitude:', geodetic_sub.longitude)
print('Geodetic elevation (m)', int(geodetic_sub.elevation.m))
print('Geodetic earth radius (m)', int(geocentric.earth_radius()))
geocentric_sub = geocentric.subpoint(0)
print('Geocentric latitude:', geocentric_sub.latitude)
print('Geocentric longitude:', geocentric_sub.longitude)
print('Geocentric elevation (m)', int(geocentric_sub.elevation.m))
print('Geocentric earth radius (m)', int(geocentric.earth_radius()))
print('Visible area (m^2)', satellite_visiable_area(geocentric.earth_radius(),
geocentric_sub.elevation.m))

Sectors representing and intersections in shapely

I am trying to use shapely to identify the area that intersact between sectors and rectangle.
So , my question is divide to two sections:
How to define (create, representing) sector as shapely object ( also triangle is sufficient), my input is coordinate x,y , start angle, end angle , radius.
How to calculate the area that intersact between list of sectors and polygon (rectangle)
Thanks
You can create a sector as a shapely object with the following function:
from shapely.geometry import Point, Polygon
import math
def sector(center, start_angle, end_angle, radius, steps=200):
def polar_point(origin_point, angle, distance):
return [origin_point.x + math.sin(math.radians(angle)) * distance, origin_point.y + math.cos(math.radians(angle)) * distance]
if start_angle > end_angle:
start_angle = start_angle - 360
else:
pass
step_angle_width = (end_angle-start_angle) / steps
sector_width = (end_angle-start_angle)
segment_vertices = []
segment_vertices.append(polar_point(center, 0,0))
segment_vertices.append(polar_point(center, start_angle,radius))
for z in range(1, steps):
segment_vertices.append((polar_point(center, start_angle + z * step_angle_width,radius)))
segment_vertices.append(polar_point(center, start_angle+sector_width,radius))
segment_vertices.append(polar_point(center, 0,0))
return Polygon(segment_vertices)
The center is a shapely point object and steps define the resolution of the curve.
So you can create a sector in this way:
center = Point(0,0)
sect = sector(center, 10, 60, 20)
To calculate the area of the intersection first you calculate the shape of the intersection in this way:
square = Polygon([(0,0), (0,10),(10,10), (10,0)])
intersection = sect.intersection(square)
at this point you obtain the area in this way:
calculated_area = intersection.area
The sector function is deliberately inspired by https://gis.stackexchange.com/questions/67478/how-to-create-a-circle-vector-layer-with-12-sectors-with-python-pyqgis

Compute off-nadir angle of vector from satellite to ground

I'd like to write Python code to specify the orbit of a satellite with Keplerian elements, specify a point on the Earth with latitude, longitude, and altitude, specify a time, and compute the angle between two vectors:
- the vector from the satellite to the specified point on the Earth
- the vector from the satellite to the center of the Earth.
I know I can use poliastro to define the orbit and propagate it to the specified time. The hard part is representing the satellite and the Earth point in the same coordinate system.
poliastro currently doesn't specify a coordinate system. Someone in their chat room told me that Earth orbits are in GCRS. astropy can convert GCRS to ITRS, which is an Earth-centered Earth-fixed frame:
import math
import numpy as np
from astropy import units as u
from astropy.time import Time
from poliastro.bodies import Earth
from poliastro.twobody import Orbit
from astropy.coordinates import SkyCoord
def lla2ecef(lat, lon, alt):
""" Convert lat/lon/alt to cartesian position in ECEF frame.
Origin is center of Earth. +x axis goes through lat/lon (0, 0).
+y axis goes through lat/lon (0, 90). +z axis goes through North Pole.
lat: number, geodetic latitude in degrees
lon: number, longitude in degrees
alt: number, altitude above WGS84 ellipsoid, in km
Returns: tuple (x, y, z) coordinates, in km.
Source: "Department of Defense World Geodetic System 1984"
Page 4-4
National Imagery and Mapping Agency
Last updated June, 2004
NIMA TR8350.2
"""
lon = lon * math.pi/180.0 # Convert to radians
lat = lat * math.pi/180.0 # Convert to radians
# WGS84 ellipsoid constants:
a = 6378.137 #equatorial radius, in km
e = 8.1819190842622e-2
# intermediate calculation: prime vertical radius of curvature
N = a/math.sqrt(1 - e**2*math.sin(lat)**2)
#results
x = (N + alt)*math.cos(lat)*math.cos(lon)
y = (N + alt)*math.cos(lat)*math.sin(lon)
z = ((1 - e**2)*N + alt)*math.sin(lat)
return (x, y, z)
epoch = Time(2018, format='decimalyear', scale='tai') #01-01-2018 00:00:00, in TAI
propagation_time = 9000 #seconds
semi_major_axis = 10000 #km
eccentricity = 0.1
inclination = 50 #deg
raan = 70 #deg
arg_perigee = 60 #deg
true_anomaly = 80 #deg
orbit = Orbit.from_classical(
Earth,
semi_major_axis*u.km,
eccentricity*u.one,
inclination*u.deg, raan*u.deg,
arg_perigee*u.deg,
true_anomaly*u.deg,
epoch)
propagated_orbit = orbit.propagate(propagation_time*u.s)
pos_gcrs = propagated_orbit.state.r
sky_gcrs = SkyCoord(
representation_type='cartesian',
x=pos_gcrs[0], y=pos_gcrs[1], z=pos_gcrs[2],
frame='gcrs',
obstime=(epoch + propagation_time*u.s))
pos_ecef = sky_gcrs.transform_to('itrs')
pos_s = np.array((pos_ecef.x.to(u.km).value,
pos_ecef.y.to(u.km).value,
pos_ecef.z.to(u.km).value))
lat = 40 #deg
lon = 50 #deg
alt = 0.06 #km
pos_t = np.array(lla2ecef(lat, lon, alt))
#Compute angle at satellite between target and center of earth
v1 = pos_t - pos_s
v2 = -pos_s
angle = math.acos(np.dot(v1, v2)/(np.linalg.norm(v1)*np.linalg.norm(v2)))
#convert to degrees
angle = angle*180.0/math.pi

Categories