Skip to content

Instantly share code, notes, and snippets.

@nitori
Created March 7, 2024 00:02
Show Gist options
  • Save nitori/69b3bf6712416ef0cb424abbd54c198b to your computer and use it in GitHub Desktop.
Save nitori/69b3bf6712416ef0cb424abbd54c198b to your computer and use it in GitHub Desktop.
import numpy as np
def main():
coord_and_names = np.genfromtxt('coordinates.txt',
dtype=[('lat', 'float'), ('lng', 'float'), ('name', 'U20')],
delimiter=',', skip_header=True)
# make lat and lng into 2D array
coords = np.column_stack((coord_and_names['lat'], coord_and_names['lng']))
uniques, indexes, counts = np.unique(coords, axis=0, return_index=True, return_counts=True)
# get mask for all rows in coords not in indexes[counts == 1]
# meaning, we're creating a mask matching coord_and_names (or coords), that
# is True for all duplicated coordinates.
duplication_mask = np.ones(coords.shape[0], dtype=bool)
duplication_mask[indexes[counts == 1]] = False
# this will now only affect the duplicates
coords[duplication_mask] += np.random.uniform(-0.001, 0.001, coords[duplication_mask].shape)
# save the new coordinates
# https://stackoverflow.com/a/26019097/3717467
from numpy.lib.recfunctions import append_fields
latlng = np.rec.array(coords, dtype=[('lat', 'float'), ('lng', 'float')])
mixed_coords = append_fields(latlng, 'name', coord_and_names['name'], usemask=False)
# alternatively, only store the jittered coordinates:
# latlng = np.rec.array(coords[duplication_mask], dtype=[('lat', 'float'), ('lng', 'float')])
# mixed_coords = append_fields(latlng, 'name', coord_and_names['name'][duplication_mask], usemask=False)
np.savetxt('jittered_coordinates.txt', mixed_coords,
delimiter=',', fmt=['%.9f', '%.9f', '%s'],
header='lat,lng,name', comments='')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment