1

I need to create a polygon and draw it on the map from GPS coordinates.
The GPS coordinates are taken from the android phone while the land leveler working the crop fields. enter image description here The results are not satisfactory.

import geojson    
import pandas as pd
from area import area
from geojson import Polygon

df = pd.read_csv('data.csv')

long_coords = df['Longitude'].tolist()
lat_coords = df['Latitude'].tolist()

data = []
for i in range(0, len(long_coords)):
    data.append( tuple([long_coords[i],lat_coords[i]]) )

data.append(data[0])
    
print("Total GPS Coordinates: {}".format(len(data)))
    
obj = Polygon([data])  
print("Total Area : {} ".format(area(obj)))
    
f = open("data.geojson", "w")
f.write(str(obj))
f.close()

The GeoJSON result is stored in data.geojson:

enter image description here

Not getting the results expected, I want a single polygon with no holes in it.

The result that I expect

enter image description here

The GPS Coordinates CSV File is: data.csv

4
  • Is using Python mandatory or is it possible to solve the problem in the QGIS desktop version? Commented May 28, 2021 at 5:04
  • @ComradeChe Yes Comrade, This must be done programmatically, the path and GPS coordinates will change. The GPS coordinates are taken from the Android phone while Land Leveler leveling the crop fields.
    – Khaalidi
    Commented May 28, 2021 at 5:11
  • @Taras Thanks Taras for the comment, let me see those answers to see if they can apply to the problem that I'm facing.
    – Khaalidi
    Commented May 28, 2021 at 5:28
  • @Taras alphashape is OK for few points, but I have about 3000 coordinates and it takes forever to create the Hull. 😢😢😢😢😢😢
    – Khaalidi
    Commented May 28, 2021 at 7:14

1 Answer 1

1

The thing you are looking for is called Alpha shape or Concave hull.

For Python exist a package Alpha Shape Toolbox.

The initial 'data.csv' file (has 2902 records) was used

input

Together with this piece of code:

import alphashape
import pandas as pd
from pyproj import Geod

file = "C://Downloads//data.csv"

df = pd.read_csv(file)

df['coords'] = tuple(zip(df['Longitude'], df['Latitude']))
coords = df['coords'].to_list()

alpha_shape = alphashape.alphashape(coords, alpha=1000)
print(alpha_shape)

geod = Geod(ellps="WGS84")
area = abs(geod.geometry_area_perimeter(alpha_shape)[0])
print('# Geodesic area: {:.3f} m²'.format(area))

In the above example the resulting alpha_shape should look like

result

and its area is # Geodesic area: 6732.531 m².

This alpha_shape is the corresponding polygon:

POLYGON ((71.6429771 29.38019, 71.6431286 29.3800933, 71.643156 29.3800313, 71.6431683 29.3799946, 71.6432204 29.3798594, 71.64319759999999 29.3797325, 71.6431948 29.3797276, 71.6431887 29.3797233, 71.6431855 29.3797212, 71.6431721 29.3797159, 71.6431616 29.3797123, 71.64286869999999 29.3795498, 71.64285529999999 29.3795391, 71.64262340000001 29.3793712, 71.642618 29.3793681, 71.64258940000001 29.3793526, 71.642512 29.3793798, 71.6424492 29.3794449, 71.6424363 29.3794624, 71.6424213 29.3794837, 71.6424044 29.3795077, 71.64180709999999 29.3799753, 71.6418553 29.3799919, 71.6418666 29.3799953, 71.64187819999999 29.3799967, 71.6418931 29.3799967, 71.6419102 29.3799957, 71.6419264 29.3799934, 71.6422322 29.3799957, 71.6422518 29.3799979, 71.6428269 29.3801475, 71.6428358 29.380153, 71.64287849999999 29.3801736, 71.64297310000001 29.3801921, 71.6429771 29.38019))

To get another more "sophisticated" result some adjustments to the alpha parameter alpha= in the alphashape() function should be done, e.g. alphashape.alphashape(coords, alpha=3000).


References:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.