Skip to content

Instantly share code, notes, and snippets.

@ragnarheidar
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ragnarheidar/ff0af4d0ea439acb6415 to your computer and use it in GitHub Desktop.
Save ragnarheidar/ff0af4d0ea439acb6415 to your computer and use it in GitHub Desktop.
GEP 662 Final project - Python on a Bike
"""
-------------------------------------------------------------------
LEHMAN COLLEGE
Geographic Information Science Program
Course: GEP 662 Introduction to Programming for GISc
Semester: Spring 2014
Instructor: Prof. Jennifer Brisbane
Student: Ragnar Heidar Thrastarson
Final project - Python on a Bike
Description:
This script will perform the following tasks
-Ask the user for CSV table that is a Citi Bike System data table
-Make changes to timestamps in the file
-Export the changes to a new CSV file
-Ask the user to supply a name for a new File GDB
-Import the converted CSV table into the new File GDB
-Perform multiple tasks and create both tables and feature classes
-Add a feature class with Citi Bike stations to an existing MXD file
-Export a JPG map with the Citi Bike stations from the MXD file
-------------------------------------------------------------------
"""
from tempfile import NamedTemporaryFile # Import tempfile module to create temporary files
import shutil # Import shutil module for high level operations (copy/paste)
import csv # Import the CSV module
import arcpy
from arcpy import env
import os
current_path = os.getcwd() + "\\" # check the current path and add a single backslash
writefile = current_path + "trips_imported.csv" # name for the exported CSV table
def askforinput(): # prompt user and get name of input table
print "Please specify input filename including CSV extension"
print "(assuming file is located in " + current_path + ")"
print ">",
user_file = raw_input() # if nothing is entered and enter is pressed, the code will throw an error! need to fix
converttrips(user_file) # pass the input to the converttrips function
def converttrips (user_file):
check_file = os.path.exists(current_path + user_file) # check if the input table exists
check_file_whitespace = len(user_file) == 0 # check for white-space
if check_file == True and check_file_whitespace != True : # if true, run what follows
print '-' * 10
print "Converting timestamps..."
readfile = user_file # input CSV file
writefile = current_path + "trips_imported.csv" # output file, created if not there, overwritten if it's already there.
tempfile = NamedTemporaryFile(delete=False) # a temporary file
with open(readfile, 'rb') as csvfilein, tempfile: # open CSV files
reader = csv.reader(csvfilein, delimiter=',', quotechar='"') # Create a CSV reader object
writer = csv.writer(tempfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL) # Create a CSV writer object
# add the header row into the writefile
writer.writerow(["tripduration","starttime","stoptime","start station id","start station name","start station latitude","start station longitude","end station id","end station name","end station latitude","end station longitude","bikeid","usertype","birth year","gender"])
next(reader) # skip the header row for the readfile
for row in reader: # for each row, split row into variables
tripduration = row[0] # trip time in seconds
starttime = row[1] # time stamp for trip start
stoptime = row[2] # time stamp from trip end
ssid = row[3] # id number start station
ssname = row [4] # name of start station
sslat = row [5] # latitude of start station
sslon= row [6] # longitude of start station
esid = row [7] # id number of end station
esname = row [8] # name of end station
eslat = row[9] # latitude of end station
eslon = row[10] # longitude of end station
bikeid= row[11] # id number for bike
usertype = row[12] # subscriber or non-subscriber
birth = row[13] # birth year of rider, only for subscribers
gender = row[14] # gender of rider, only for subscribers
# split the start timestamp into day, month and year+time
s_month, s_day, s_yeartime = starttime.split('/')
# split the year+time into year and time
s_year, s_time = s_yeartime.split(' ')
# merge start timestamp into new format
s_timestamp = s_year + "-" + s_month + "-" + s_day + " " + s_time
# split the end timestamp into day, month and year+time
e_month, e_day, e_yeartime = stoptime.split('/')
# split the year+time into year and time
e_year, e_time = e_yeartime.split(' ')
# merge start timestamp into new format
e_timestamp = e_year + "-" + e_month + "-" + e_day + " " + e_time
# write each row into a tempfile with new timestamp formats
writer.writerow([tripduration, s_timestamp, e_timestamp, ssid, ssname, sslat, sslon, esid, esname, eslat, eslon, bikeid, usertype, birth, gender])
# write contents of tempfile into a new file
shutil.move(tempfile.name, writefile)
print "CSV output file saved here: " + writefile
else: # # if false, run what fallows
print '-' * 28
print "| This file was not found! |" # notify the user that input file in not found
print '-' * 28
askforinput() # go back to the askforinput function and get a new input table
def askforfgdb():
print "Please specify name for File Geodatabase"
print ">",
user_filegdb = raw_input() # ask for user input
user_filegdb.replace(" ", "") # if there are spaces in the name, remove
user_filegdb = user_filegdb + ".gdb" # add the extension for the file geodatabase
gisanalysis(user_filegdb) # pass the name for the new file geodatabase to the gisanalysis function
def gisanalysis (user_filegdb):
check_for_gdb = os.path.isdir(user_filegdb) # check if the file geodatabase exists
if check_for_gdb == False: # if file geodatabase does NOT exist, run the following
env.overwriteOutput = True #allow overwrites
arcpy.CreateFileGDB_management(current_path, user_filegdb, "CURRENT") # create file geodatabase
print "File Geodatabase created"
env.workspace = current_path + user_filegdb #define workspace
print "Importing table..."
# importin CSV table into file geodatabase
arcpy.TableToGeodatabase_conversion(writefile, env.workspace)
# count the number of records imported
count_resault = arcpy.GetCount_management("trips_imported")
count_trips = int(count_resault.getOutput(0))
print "Total trips imported: " + str(count_trips)
print "Summarizing..."
# create summary tables for the fields gender and stationsIDs
arcpy.Statistics_analysis("trips_imported", "summary_gender", "gender COUNT", "gender")
arcpy.Statistics_analysis("trips_imported", "summary_stations", "start_station_latitude FIRST;start_station_longitude FIRST;start_station_id COUNT;end_station_id COUNT", "start_station_id")
print "Summary tables created"
print "Creating feature class..."
# create an event layer from station summary table
arcpy.MakeXYEventLayer_management("summary_stations", "FIRST_start_station_longitude", "FIRST_start_station_latitude", "summary_stations_Layer", "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 10000;8.98315284119522E-09;0.001;0.001;IsHighPrecision", "")
print "XY Event layer created"
# Copy event layer and create a new feature class - Citi Bike Stations
arcpy.CopyFeatures_management("summary_stations_Layer", "bike_stations_wgs84", "", "0", "0", "0")
print "XY Event Layer saved to new feature class"
# project the Citi Bike Stations feature class from WGS84 to State Plane-NY-LI-feet
arcpy.Project_management("bike_stations_wgs84", "bike_stations", "PROJCS['NAD_1983_StatePlane_New_York_Long_Island_FIPS_3104_Feet',GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Lambert_Conformal_Conic'],PARAMETER['False_Easting',984250.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-74.0],PARAMETER['Standard_Parallel_1',40.66666666666666],PARAMETER['Standard_Parallel_2',41.03333333333333],PARAMETER['Latitude_Of_Origin',40.16666666666666],UNIT['Foot_US',0.3048006096012192]]", "WGS_1984_(ITRF00)_To_NAD_1983", "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]")
print "Bike stations projected from WGS-84 to New York Long Island State Plane"
print "Adding fields..."
# add a field for the total number of trips
arcpy.AddField_management("bike_stations", "total_trips", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
# populate the total number of trips field
arcpy.CalculateField_management("bike_stations", "total_trips", "[COUNT_start_station_id] + [COUNT_end_station_id]", "VB", "")
# add Citi Bike Stations feature class to an MXD file
bike_stations_lyr = arcpy.MakeFeatureLayer_management("bike_stations", "bike_stations_lyr")
mxd = arcpy.mapping.MapDocument(current_path + "Citibikemap.mxd") # this MXD needs to exists and be in the same folder as script
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] # add to data frame that is called layers
addLayer = arcpy.mapping.Layer("bike_stations_lyr") # add the bike station layer to the data frame
arcpy.mapping.AddLayer(df, addLayer, "AUTO_ARRANGE") # zoom to extent
arcpy.mapping.ExportToJPEG(mxd, current_path + "NewMap.jpg") # export JPG map
print "Map exported"
del mxd, addLayer # clean up and delete these two variables
print "Done!"
else: # if file geodatabase does exist, run the following
print '-' * 39
print "|This File Geodatabase already exists!|" # notify the user that the file geodatabase does exist
print '-' * 39
askforfgdb() # call the askforfgdb again to get a new name for a file geodatabase
askforinput() # run function askforinput
askforfgdb() # run function askforfgdb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment