Plotting schools Port Elizabeth using shell scripting

From Christiaan008 Wiki
Revision as of 10:21, 27 September 2016 by Christiaan008 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
#Script written on 3 Feb 2016
#Purpose: retrieving coordinates for schools Port Elizabeth

#While reading file 'schools1.csv' per line, split by ; character into variable name,tel,phase,suburb,special
while IFS=";" read -r name tel phase suburb special
do

        #Take the name of a school, replace white spaces with a + sign  and 'To Be Updated and 'Port Elizabeth'  with nothing (remove)
        #Afterwards place after school name +Port+Elizabeth
        poi=$(echo $name | sed "s| |+|g" | sed "s|To Be Updated||g" | sed "s|Port Elizabeth||g" | sed "s|$|+Port+Elizabeth|")

        #Do a webrequest to the Google Geocoding API
        request=$(curl --silent "https://maps.googleapis.com/maps/api/place/textsearch/xml?query="$poi"&key=insertyourownkey")

        #Take the output of the request and only use the information between <formatted_address> and </formatted_address>
        formatted=$(echo $request | grep -o -P '(?<=<formatted_address>).*(?=</formatted_address>)')

        #Take the output of the request and only use the information between <lat> and </lat>
        lat=$(echo $request | grep -o -P '(?<=<lat>).*(?=</lat>)')

        #Take the output of the request and only use the information between <lng> and </lng>
        long=$(echo $request |  grep -o -P '(?<=<lng>).*(?=</lng>)')

        #Ouput name, address and coordinates to the screen
        echo "School: $name"
        echo "Address: $formatted"
        echo "Coordinates: $lat , $long"

        #Go to sleep for 5 seconds (do nothing for 5 seconds)
        sleep 5

        #Write the school name, Address and coordinates to the file 'schools_w_coordinates.csv'
        echo $name";"$formatted";"$lat";"$long >> schools_w_coordinates.csv

#End the while loop
done < "schools1.csv"