Skip to content

Instantly share code, notes, and snippets.

@aubreymoore
Last active March 14, 2021 01:06
Show Gist options
  • Save aubreymoore/7f3688e88556e08a8069623ecf48b68f to your computer and use it in GitHub Desktop.
Save aubreymoore/7f3688e88556e08a8069623ecf48b68f to your computer and use it in GitHub Desktop.
Create a KML file with equally spaced points at vertices of a triangular grid.
#!/usr/bin/python3
import pyproj
import simplekml
'''
Generates a KML file with equally spaced points at vertices of a triangular grid. The epsg argument is the code for the UTM
projection which converts lat/lon to meters. The epsg default value, EPSG:32655 is for Guam (zone 55n).
Example: This command line places points equally spaced at 10 m on the lawn in front of the Canadian Parliament Building:
$ python -c 'from triangular_grid import *; \
generate_equidistant_points(epsg="EPSG:32618", ll_longitude=-75.698873, ll_latitude=45.424078, meters_between_points=10)'
'''
def generate_equidistant_points(epsg='EPSG:32655', meters_between_points=100.0,
ll_longitude=144.71318, ll_latitude=13.26232, ncols=5, nrows=5,
kml_file='points.kml'):
wgs84=pyproj.Proj(init='EPSG:4326') # LatLon with WGS84 datum used by GPS units and Google Earth
utm=pyproj.Proj(init=epsg) # UTM coords (Cartesian coordinates in meters)
hdist = meters_between_points # horizontal distance between points
vdist = meters_between_points * (0.75**0.5) # vertical distance between points
x0, y0 = pyproj.transform(wgs84, utm, ll_longitude, ll_latitude) # convert lon lat to Cartesian coordinates in meters (UTM)
x = x0; y = y0; i = 0; kml = simplekml.Kml()
for row in range(nrows):
y = y0 + row * vdist
for col in range(ncols):
x = x0 + col * hdist
if row % 2 != 0: # Odd numbered rows are offset by dist/2
x = x + hdist / 2
lon, lat = pyproj.transform(utm, wgs84, x, y) # convert UTM back to lon lat
i = i + 1
kml.newpoint(name=str(i), coords=[(lon, lat)])
kml.save(kml_file)
return
@Alexsandro1997
Copy link

Thank you very much from Brazil. In Brazil is defaulf use coordinates UTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment