Mar-04-2018, 07:29 PM
I have records like this (I cannot alter the origional dataset). The Primary key is always 64 characters, followed by a 3 letter code, three sets of WGS84 coordinates (lat,long,altitutde), then a site name, in this case De Vrijheid.
('b30e845d00993f2258900c4052471a6947c6b3f1a375438b6f4cd206397186a5', 'geo52.7642852014.9228121084000000000000De Vrijheid')
First I try to format this:
['b30e845d00993f2258900c4052471a6947c6b3f1a375438b6f4cd206397186a5', 'geo', '52.764285201', '4.9228121084', '000000000000']
I get geojson this way.
('b30e845d00993f2258900c4052471a6947c6b3f1a375438b6f4cd206397186a5', 'geo52.7642852014.9228121084000000000000De Vrijheid')
First I try to format this:
cur.execute( "SELECT t.id, t.\"vendorField\" FROM transactions t WHERE t.\"vendorField\" LIKE 'geo%'") print("Rows returned: ", cur.rowcount) dashmap=(66, 3, 12, 12, 12, ) row = cur.fetchone() while row is not None: #formatting each row data = row data = str(data).encode('latin1').decode('unicode_escape').replace("', '","") data = re.findall(''.join('(\S{{{}}})'.format(l) for l in dashmap), data) data = str(data) data = data.replace(r'"', '|').replace(r'|(', '').replace(r'|', '').replace(r'(', '').replace(r')', '').replace(r'[', '').replace(r']', '').replace(r"'", "").replace(" ","").split(",")This returns
['b30e845d00993f2258900c4052471a6947c6b3f1a375438b6f4cd206397186a5', 'geo', '52.764285201', '4.9228121084', '000000000000']
I get geojson this way.
geodata = geojson.Point((float(data[3]), float(data[2])))And write to file
with open('points.json', 'a') as outfile: geojson.dump(geodata, outfile, indent=2) outfile.close()And finish of
cur.close() except (Exception, psycopg2.DatabaseError) as error: print(error) # execute a statement print('PostgreSQL database version:') cur.execute('SELECT version()') # display the PostgreSQL database server version db_version = cur.fetchone() print(db_version) # close the communication with the PostgreSQL cur.close() except (Exception, psycopg2.DatabaseError) as error: print(error) finally: if conn is not None: conn.close() print('Database connection closed.')This gives a geojson file formatted like this, which is not geojson, it seems like it is writing one line at a time as an induvidual point not a multipoint file. I'm a noob to python, so any amendments/alternative solutions are welcome.
{ "type": "Point", "coordinates": [ 45.506527899, 12.081455199 ] }{ "type": "Point", "coordinates": [ 25.4094136, 11.9158859 ] }