Skip to content

Instantly share code, notes, and snippets.

@adriantre
Created May 4, 2018 18:31
Show Gist options
  • Save adriantre/b7f971ea7b438e7094a119ae5630326d to your computer and use it in GitHub Desktop.
Save adriantre/b7f971ea7b438e7094a119ae5630326d to your computer and use it in GitHub Desktop.
Batch insert ais messages
def insertPositionData(cur, data_list, batch_size, received_by):
sql = '''
INSERT INTO ais_position
(timestamp, mmsi, geom, mgrs_name, accuracy,
course, speed, status, status_text, type, heading,
shiptype, shiptype_text, received_by, other)
VALUES %s
ON CONFLICT (mmsi, timestamp)
DO NOTHING
'''
template = '''
(%(timestamp)s, %(mmsi)s, ST_GeomFromEWKT(%(geom)s),
(ARRAY(SELECT name from mgrs where ST_Within(ST_GeomFromEWKT(%(geom)s), mgrs.geom))),
%(accuracy)s, %(course)s, %(speed)s, %(status)s,
%(status_text)s, %(type)s, %(heading)s, %(shiptype)s,
%(shiptype_text)s, %(received_by)s, %(other)s)
'''
execute_values(cur, sql, data_list, template, page_size = batch_size)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment