Home > front end >  How to create a rectangle for azimuth direction for n kilimeter
How to create a rectangle for azimuth direction for n kilimeter

Time:09-27

Suppose I have three four columns like item,item_latitude,item_longitude and azimuth.

I am trying to create a rectangle for each lat and long in the azimuth direction for /-35 degree and 2 Kilometer.

Example . There is a azimuth of site x is 45 degree so 35 and -35 degree to 45 degree would be 15 degree and 80 degree and distance would be 2 KM

i am trying to get all polygon for that area.I have used jts.util.GeometricShapeFactory Library but it always create a circle like below .I have used scala with jts.util.GeometricShapeFactory Library.

def generatePolygoan(lat:String,long:String,diameterINKM:Double):Option[String] = {
    val latitude = lat.toDouble
    val longitude = long.toDouble
    val diameterInMeters = diameterINKM * 1000
    val shapeFactory = new GeometricShapeFactory()
    //shapeFactory.setNumPoints(64) // adjustable
    shapeFactory.setCentre(new Coordinate(latitude, longitude))
    // Length in meters of 1° of latitude = always 111.32 km
    shapeFactory.setWidth(diameterInMeters / 111320d)
    // Length in meters of 1° of longitude = 40075 km * cos( latitude ) / 360
    shapeFactory.setHeight(diameterInMeters / (40075000 * Math.cos(Math.toRadians(latitude)) / 360))
    val circle = shapeFactory.createCircle()
    Some(circle.toString)
  }

The above code create the circle around the lat and long, but i need to create the rectangle in the direction of azimuth only /- 35 degree.

Please help me i am new in Geometry. The code may be in any programming like JAVA/SCALA/PHP/PYTHON/MYSQL/POSTGRES/MONGODB or even mathematics pseudo code

CodePudding user response:

You need to create a function that compute a point from another point in an certain azimuth and at a certain distance (see geographic distance and azimuth chapter 1C]Then call the function :

  • loop from item point to azimuth - delta on azimuth at distance
  • loop from azimuth - delta azimuth to azimuth delta azimuth at distance
  • loop from azimuth delta at distance to item point :

Code :

import math

earthRadius = 6371.0 # earth radius in km

# utility functions working in degrees
def cos(d):
    return math.cos(math.radians(d))

def sin(d):
    return math.sin(math.radians(d))

def arccos(d):
    return math.degrees(math.acos(d))

def arcsin(d):
    return math.degrees(math.asin(d))

def printLatLon(lat,lon,prefix="",sep=" "):
    print(str.format("{}{:10.8}{}{:11.9}",prefix,lat,sep,lon))

# function to compute a point at some distance in some direction
def pointFrom(lat1, lon1, azimuth, distance):
    b = math.degrees(distance / earthRadius)
    a = arccos(cos(b)*cos(90 - lat1)   sin(90 - lat1)*sin(b)*cos(azimuth))
    B = arcsin(sin(b)*sin(azimuth)/sin(a))
    lat2 = 90 - a
    lon2 = B   lon1

# set initial point
p0Lat = 18.3965 # latitude in degrees 
p0Lon = 74.7274 # longitude in degrees 
azimuth = 20.0 # azumuth in degrees 
distance = 2.0 # distance in km (sane unit as earth radius)
deltaAzimuth = 35.0

pList = [[p0Lat,p0Lon]]

# conpute arc from point 0 to azimuth - 35
nbPoints = 5
deltaDistance = distance / nbPoints
print(str.format("delta distance {:.3} km",deltaDistance))
azi = azimuth - deltaAzimuth
for i in range(1,nbPoints 1):
    disti = deltaDistance * i
    #print("disti = " str(disti))
    piLat,piLon = pointFrom(p0Lat,p0Lon,azi,disti)
    printLatLon(piLat,piLon,prefix=str.format("{:.3} {:.3} : ",azi,disti))
    pList.append([piLat,piLon])

# compute n points point at distance from azimuth - 35 to aximuth   35
deltaAzPoint  = 5.0
nbPoints = int(2.0  * deltaAzimuth / deltaAzPoint)

print(str.format("delta azimuth {:.3} deg",deltaAzPoint))
az0 = azimuth - deltaAzimuth
for i in range(1,nbPoints 1):
    azi = az0   i*deltaAzPoint
    #print("az : " str(azi))
    piLat,piLon =  pointFrom(p0Lat,p0Lon,azi,distance)
printLatLon(piLat,piLon,prefix=str.format("{:.3} {:.3} : ",azi,distance))
    pList.append([piLat,piLon])

# compute points from distance to 0 (p0) for azimuth   35
nbPoints = 5
print(str.format("delta distance {:.3} km",deltaDistance))
azi = azimuth   deltaAzimuth
for i in range(nbPoints-1,0,-1):
    disti = deltaDistance * i
    piLat,piLon = pointFrom(p0Lat,p0Lon,azi,disti)
    printLatLon(piLat,piLon,prefix=str.format("{:.3} {:.3} : ",azi,disti))
    pList.append([piLat,piLon])

# return to origin
pList.append([p0Lat,p0Lon])

# print result
for latLon in pList:
    printLatLon(latLon[0],latLon[1])
  • Related