Python script for text files to get GPS

4349
6
03-06-2014 03:43 AM
DouglasMinnigh
New Contributor
Hello,

I am trying to automate a process. I get an email that has a request number, GPS points, and an email to respond to.

I currently convert that email to a text file and put it on a shared drive. Then I map the GPS points from the text file. I create a polygon from those GPS points. I create a map titled by the request number. Zoom to appropriate scale. Export to PDF to the shared drive and email to the person that requested it.

Are there any ways to automate this process?

I was thinking if I had multiple text files, is there a way to extract the request number, GPS points (can range from 3-6 Lat/Long), and email associated with each text files and combine them all into a table.

Any help is appreciated,
Thanks

If the end result was like this that would be great!
Lat Long Request Email Address
40.2354 -79.54545 request 1 email 1
40.3624 -79.70275 request 1 email 1
40.4894 -79.86005 request 1 email 1
40.6164 -80.01735 request 1 email 1
40.7434 -80.17465 request 1 email 1
40.8704 -80.33195 request 1 email 1
40.9974 -80.48925 request 1 email 1
41.1244 -80.64655 request 1 email 1
41.2514 -80.80385 request 1 email 1
41.3784 -80.96115 request 1 email 1
41.5054 -81.11845 request 2 email 2
41.6324 -81.27575 request 2 email 2
41.7594 -81.43305 request 2 email 2
41.8864 -81.59035 request 2 email 2
42.0134 -81.74765 request 2 email 2
42.1404 -81.90495 request 2 email 2
42.2674 -82.06225 request 2 email 2
42.3944 -82.21955 request 2 email 2
42.5214 -82.37685 request 2 email 2
Tags (2)
0 Kudos
6 Replies
JoshuaChisholm
Occasional Contributor III
Hello Douglas,

We definitely should be able to script everything you said, it just depends how fancy you want to get. Do you want the whole process automated? As in someone emails you some lats and longs and then receives an automated message back with an attached PDF? This would involve a lot of scripting work.

If you just want to combine multiple text files into one long text file, that would be a much more simple script. If you just want this automated, could you provide the folder structure of the input text files and the format of the text files them selves?

Thank you,
~Josh
0 Kudos
DouglasMinnigh
New Contributor
Hello,

Thank you for getting back to me. Optimally, I want to automate the whole process but creating the table is a great first step. I have a folder with multiple text files like the one attached.

I need the following info :

SERIAL NUMBER (11 digit code)
All the GPS Coordinates
Email address

Thanks!
0 Kudos
JoshuaChisholm
Occasional Contributor III
Ok, so the whole process is a lengthy customized piece of work, I'm not sure if you'll be able to work out a full solution on this forum (but you can try). I think you would likely have to involve multiple tools (perhaps including visual basic in outlook or a Google form). You can send me a private message if you want me to help you out in this matter.

As for the merging of files you could try something like this (also attached):
import os
import sys

print "Starting Merge..."

outputTableName="MergedTable.csv"

scriptFile=sys.argv[0]
scriptName=os.path.basename(scriptFile)
homePath=os.path.dirname(scriptFile)

toCSV="Lat,Long,Request,EmailAddress\n"

for aFile in os.listdir(homePath):
    if aFile==scriptName:
        continue #ignore this script file
    if aFile==outputTableName:
        continue #ignore the merged table if it is already existing in the folder
    print "  Working on: "+aFile
    f=open(homePath+"\\"+aFile,"r")
    txt=f.read()
    f.close()
    #Get SERIAL NUMBER:
    serNum=os.path.splitext(aFile)[0]
    #Get Email address:
    email=txt.split("Email--")[1].split("\n")[0]
    #Get All the GPS Coordinates:
    latLongsTxt=txt.split("Mapped Lat/Lon--")[1].split("Map Graphic--")[0].strip()
    latLongsTxt=latLongsTxt.replace('[','').replace(']','')
    #Prepare text for output table:
    for i in latLongsTxt.split(","):
        latLong=i.replace("/",",").strip()
        if latLong=="":
            continue #skip null entries (for example if there is an extra comma at the end of the list)
        toCSV+=latLong+","+serNum+","+email+"\n"


#Create CSV
if os.path.exists(homePath+"\\"+outputTableName):
    os.remove(homePath+"\\"+outputTableName) #Remove file if it exists

f=open(homePath+"\\"+outputTableName,"a")
f.write(toCSV)
f.close()

print "Merge complete!\nNew table: "+outputTableName
raw_input("\nPress enter to exit.")

To run the script, just move the file to your folder containing the text emails (and nothing else) and run it (2x click should work). Note, this script is very dependent on the emails following the exact same format as the one provided. I would also take a copy of you folder containing text email before running on the main copy.
0 Kudos
DouglasMinnigh
New Contributor
That works just how I want it! Is there a way to point to a folder where the text files are or does the script have to be in the same folder?

Thank you! Extremely helpful
0 Kudos
JoshuaChisholm
Occasional Contributor III
Of course (just a few small changes):
import os

print "Starting Merge..."


outputTable=r"C:\Path\To\MergedTable.csv" #Change this to the desired location of your output table
emailFolder=r"C:\Path\To\TextEmailsFolder" #Change to the folder that contains your text emails

toCSV="Lat,Long,Request,EmailAddress\n"

for aFile in os.listdir(emailFolder):
    if aFile==os.path.basename(outputTable):
        continue #ignore the merged table if it is already existing in the folder
    print "  Working on: "+aFile
    f=open(emailFolder+"\\"+aFile,"r")
    txt=f.read()
    f.close()
    #Get SERIAL NUMBER:
    serNum=os.path.splitext(aFile)[0]
    #Get Email address:
    email=txt.split("Email--")[1].split("\n")[0]
    #Get All the GPS Coordinates:
    latLongsTxt=txt.split("Mapped Lat/Lon--")[1].split("Map Graphic--")[0].strip()
    latLongsTxt=latLongsTxt.replace('[','').replace(']','')
    #Prepare text for output table:
    for i in latLongsTxt.split(","):
        latLong=i.replace("/",",").strip()
        if latLong=="":
            continue #skip null entries (for example if there is an extra comma at the end of the list)
        toCSV+=latLong+","+serNum+","+email+"\n"


#Create CSV
if os.path.exists(outputTable):
    os.remove(outputTable) #Remove file if it exists

f=open(outputTable,"a")
f.write(toCSV)
f.close()

print "Merge complete!\nNew table: "+outputTable
raw_input("\nPress enter to exit.")


Just make you you edit the variables outputTable and emailFolder.
0 Kudos
DouglasMinnigh
New Contributor
Been a great help! Thank you so much. Now figuring out everything else will be an adventure
0 Kudos