Python Forum
formatting string and returning as geojson
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
formatting string and returning as geojson
#4
something like this (note that I'm not able to test it)

#!/usr/bin/python
import psycopg2
from config import config
import re
import geojson
import tempfile

def parse_row(row):
    # extract lat, lon
    # you may need to round the float number to
    # correct number of decimal digits

    lon = float(row[3:15])
    lat = float(row[15:27])
    return (lon, lat)

 
def get_vendorfield():
    """ Connect to the PostgreSQL database server """


    # read connection parameters
    params = config()
    # connect to the PostgreSQL server
    print('Connecting to the PostgreSQL database...')
    with psycopg2.connect(**params) as conn:
        # create a cursor
        with conn.cursor() as cur:
            try:
                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, ) #this is used for the chunks - but the last chuck is missed because it is of unknown length
                geo_data = [parse_row(row[1]) for row in cur] # you may need to replace cur with cur.fetchall()
            except psycopg2.DatabaseError as db_error:
                print(error)
                geo_data = []

    print('Database connection closed.')
            
    if geo_data:
        multipoint_json = geojson.Multipoint(geo_data)
        with open('points.json', 'w') as outfile:
            geojson.dump(multipoint_json, outfile, indent=2)
My understanding was you want geojson.Multipoint file
there are number of things you need to consider, e.g. N/S E/W of the coordinates (usually it is represented with -) does it affect the length? also decimal precision - you may need to round the float number for lon/lat to correct precision)
Reply


Messages In This Thread
RE: formatting string and returning as geojson - by buran - Mar-05-2018, 08:04 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Formatting DateTime string and and converting it from AM/PM to 24 hours tester_V 2 127 Yesterday, 05:16 PM
Last Post: tester_V
  Formatting a date time string read from a csv file DosAtPython 5 1,590 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  String formatting (strptime) issues Henrio 2 940 Jan-06-2023, 06:57 PM
Last Post: deanhystad
  geojson to json --missing multiple row output yoshi 9 3,046 Mar-06-2022, 08:34 PM
Last Post: snippsat
  confused about string formatting barryjo 7 2,104 Mar-06-2022, 02:03 AM
Last Post: snippsat
  string formatting barryjo 7 2,205 Jan-02-2022, 02:08 AM
Last Post: snippsat
  Help with string formatting in classes brthurr 6 11,690 Dec-17-2021, 04:35 PM
Last Post: Jeff900
  Question on HTML formatting with set string in message Cknutson575 3 3,611 Mar-09-2021, 08:11 AM
Last Post: Cknutson575
  smtplib: string formatting not carrying over to email ClassicalSoul 1 2,750 Apr-22-2020, 09:58 PM
Last Post: bowlofred
  String formatting difficulties mmk1995 3 2,869 Aug-09-2019, 11:18 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020