0% found this document useful (0 votes)
3 views62 pages

Foss4g Python

The document outlines a course on using Python for geospatial applications, covering software and data loading, Python basics, and various Python packages for geospatial tasks. Participants will learn to automate geographic data processing and utilize Python's functionalities, including modules, data types, and spatial operations with PostGIS. The course includes practical examples and quizzes to reinforce learning and application of concepts.

Uploaded by

ashwinijoshi2026
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views62 pages

Foss4g Python

The document outlines a course on using Python for geospatial applications, covering software and data loading, Python basics, and various Python packages for geospatial tasks. Participants will learn to automate geographic data processing and utilize Python's functionalities, including modules, data types, and spatial operations with PostGIS. The course includes practical examples and quizzes to reinforce learning and application of concepts.

Uploaded by

ashwinijoshi2026
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 62

Learning the FOSS4g Stack: Python for

Geospatial

gisadvisor.com, LLC.
What we’ll cover
PART 1: LOADING SOFTWARE AND DATA
PART 2: OVERVIEW OF PYTHON
PART 3: PYTHON PACKAGES
PART 4: GEOSPATIAL ADDITIONS TO PYTHON

gisadvisor.com, LLC.
Course Goals
At the end of this course,
you will:
Understand what Python is.
Learn how to use Python to write
scripts for automating geographic
data.
Learn how to use Python packages
to add additional functionality to
your Python code.
gisadvisor.com, LLC.
Part I: Loading our
software and data

gisadvisor.com, LLC.
Part II: An overview of
Python

gisadvisor.com, LLC.
Using Python for Geospatial
Basic Python
Spatial and database tools
QGIS
Arcpy
Special Python packages for
geospatial
Postgres/PostGIS
Microsoft Excel
Geocoding
gisadvisor.com, LLC.
What is Python
Python is an interpreted, interactive, object-oriented
programming language. It incorporates modules,
exceptions, dynamic typing, very high level dynamic data
types, and classes. Python combines remarkable power
with very clear syntax. It has interfaces to many system
calls and libraries, as well as to various window systems,
and is extensible in C or C++. It is also usable as an
extension language for applications that need a
programmable interface. Finally, Python is portable: it runs
on many Unix variants, on the Mac, and on Windows 2000
A Tour
and later. –of Python
www.python.org

How to find what you want


How do I run a for loop?
How do I read from a file?

gisadvisor.com, LLC.
The many flavors of Python
Versions
2.6, 2.7, 2.8
3.0, 3.3, 3.6
Interpreters
IDLE
WinPy
Geany

The Locations – and why it is important!


Python directories
Arcpy directory
gisadvisor.com, LLC.
The Python Language

gisadvisor.com, LLC.
The Python Language

Python as a calculator
Variables
Structures
Statements
Expressions
Methods and Functions

gisadvisor.com, LLC.
Starting with IDLE

Working with the shell


Working with files

gisadvisor.com, LLC.
Python as a calculator
Just
enter some
numbers

gisadvisor.com, LLC.
Variables and Data Types
A variable is a name for a value. The computer
stores the value in memory. Depending upon what
the variable type is, certain operations can be done
with the variable.
 Some variable types include:
 Strings
 Numbers
 Integer

 Floating Point
 Lists
name = 'Art Lembo'
age = '54'
children = ['Emily','Arthur','Katie']
print age
print name
print children[1]
gisadvisor.com, LLC.
Variables and methods
String

len, split, find


replace,left/right([:],[-:])
Number

floor,round, / vs. // vs. float, round


List

append, len, max, min, sort

gisadvisor.com, LLC.
Lists
Python supports arrays in the form of lists.
These include a basic list:
mylist = ["art","bob","sue"]
mylist[0]
or, lists within lists:
mylist = [["art",52],["bob",19],["Sue",44]]
mylist[0]
mylist[0][0]

And, we can do things with lists:


sort, remove, append….

gisadvisor.com, LLC.
List
a = [66.25, 333, 333, 1, 1234.5]
a[1]
print a.reverse
a.insert(2,1.00)
a.remove(1.0)
a.pop(2)
sortlist = a; sortlist.sort(); sortlist

gisadvisor.com, LLC.
Dictionaries
A dictionary is a collection which is unordered, changeable and
indexed. In Python dictionaries are written with curly brackets, and
they have keys and values.

Dict = {'Name': 'Art', 'Age': 54, 'Area': 69 * 44}

print dict['Name']

gisadvisor.com, LLC.
states = {
'Oregon': 'OR',
'Florida': 'FL',
'California': 'CA',
'New York': 'NY',
'Michigan': 'MI'
}

cities = {
'CA': 'San Francisco',
'MI': 'Detroit',
'FL': 'Jacksonville'
}

cities['NY'] = 'New York'


cities['OR'] = 'Portland‘

print "NY State has: ", cities['NY']

gisadvisor.com, LLC.
Methods and Functions
f = open('c:/training/python/addresses.txt')
for line in f:
print line
Files
Read a file
Write a file f = open('c:/training/python/addresses.txt')
o = open('c:/training/python/addout.txt','w')
for line in f:
o.write(line)
o.close()

gisadvisor.com, LLC.
Quiz

Read the file ‘benchmarks.csv’


Write the file out to ‘benchmarksnew.csv’

gisadvisor.com, LLC.
Quiz
How many letters are in
supercalifragilisticexpialidocious?
What is the 9th letter in the word?
Use split to create a list of the Beatles:
‘John’,’George’,’Paul’,’Ringo’
Create two variables:
Firstname = ‘John’
Lastname = ‘Lennon’
Append the two names into a new
variable called FullName
gisadvisor.com, LLC.
Python as a module
Add code array = [12, 9, 17, 16]
Save sumtotal = 0
for i in array:
Run the module sumtotal = i +
sumtotal
print sumtotal
print sum(array)
Quiz
Instead of the sum, calculate the sum of the
squares (use the pow function)
Print out the sum of the squares and the
average sum of the squares

gisadvisor.com, LLC.
E
C
T
P
rr
o
ro
e d
o
g
re
r
s a
C
W
m
t
o
rr
rD
ie
i e
t
c
s
n ti
i
in
g
g o
g
n
n

L
o
g
i
c

C
o
r
r
e
c
t
i
o
n

gisadvisor.com, LLC.
Algorithm - a finite list of well-defined
instructions for accomplishing some
task
 Algorithms are essential to the way computers process
information
 Algorithms must be rigorously defined:
 specified in the way it applies in all possible circumstances that could
arise.
 conditional steps must be systematically dealt with, case-by-case;
 the criteria for each case must be clear (and computable).
array = [2,4,6,3]
Have variable for
largest=0
-for largest
i in array:
if i>largest:
Look up in Help:
largest=i
print largest
- list
- for loop
- if..then..else
gisadvisor.com, LLC.
Statements and Control

if
while
for

gisadvisor.com, LLC.
Quiz - Temperature program

Now use the input statement

gisadvisor.com, LLC.
Python modules (packages)
A module is a file containing Python
definitions and statements.
import math
math.cos(34)
There are many, many “standard” modules
built in Python by default
There are many, many, many, MANY other
packages you can install

gisadvisor.com, LLC.
Default modules

Modules for Python 2.7


time
math
HTMLParser
urllib
sqlite3

gisadvisor.com, LLC.
SQLite Example (not spatial,
yet)
import sqlite3
conn = sqlite3.Connection('c:/training/python/tompkins.sqlite')
answer = conn.execute('SELECT * FROM parcels LIMIT 3')
for row in answer:
print row

Quiz
Findthe average ASMT value for
each Propclass in parcels

gisadvisor.com, LLC.
import os
os.chdir("c:/program files/qgis 3.8/bin")
import sqlite3
##pkey = input('enter your parcel key: ')
conn = sqlite3.Connection('c:/training/python/tompkins.sqlite')
conn.enable_load_extension(True)
conn.execute('SELECT load_extension("mod_spatialite")')

answer = conn.execute('''SELECT sum(asmt) AS sumasmt, propclass


FROM parcels, floodzones
WHERE
ST_Intersects(parcels.geometry,floodzones.geom)
GROUP BY propclass ORDER BY sumasmt''')
for row in answer:
print( "Property Class ",row[1], " $", row[0])

gisadvisor.com, LLC.
import os
os.chdir("c:/program files/qgis 3.8/bin")
import sqlite3
pkey = input('enter your parcel key: ')
conn = sqlite3.Connection('c:/training/python/tompkins.sqlite')
conn.enable_load_extension(True)
conn.execute('SELECT load_extension("c:/program files/QGIS
3.8/bin/mod_spatialite")')

answer = conn.execute('''SELECT
min(st_distance(parcels.geometry,floodzones.geom)) as dist
FROM parcels, floodzones
WHERE parcels.parcelkey = ''' + '\'' + pkey + '\'')
for row in answer:
if row[0] == 0.0:
print('parcel intersects the floodzone')
else:
print(row[0])
gisadvisor.com, LLC.
pip – the magic of Python

Run pip to install third party modules:


pip install numpy
pip install googlemaps
pip install pygal
pip is a package management system used to
pip install geocoder install and manage software packages written
in Python. Many packages can be found in
the Python Package Index
(PyPI). Python 2.7.9 and later (on the python2
series), and Python 3.4 and later
include pip (pip3 for Python 3) by default.

gisadvisor.com, LLC.
Creating an external function
for linear algebra
Pycall.py Ols.py

from pyclass import ols_function, numpy import numpy

A = numpy.array([[1,2,3],[4,5,6],[7,8,9]]) def ols_function(A,L):


L = numpy.array([[3],[2],[1]]) AT = numpy.transpose(A)
myresult = ols_function(A,L) ATA = numpy.matmul(AT,A)
print(myresult) ATAi = numpy.linalg.inv(ATA)
ATAiAT = numpy.matmul(ATAi,AT)
ols = numpy.matmul(ATAiAT,L)
return ols

gisadvisor.com, LLC.
import psycopg2

PostGIS
conn = psycopg2.connect(dbname =
'tompkins',host='127.0.0.1',user='postgres',password='postgres',
port=5433)
cur = conn.cursor()
thesql = ('''SELECT tcparcel.parcelkey, zone
FROM floodzones, tcparcel
WHERE ST_Intersects(tcparcel.geom,floodzones.geom)''')
cur.execute(thesql)
theresult = cur.fetchall()
for j in theresult:
print ('Park ' + j[0] + 'Intersects parcel ' + j[1])

import psycopg2
conn = psycopg2.connect(dbname =
'tompkins',host='127.0.0.1',user='postgres',password='postg
res', port=5433)
cur = conn.cursor()
thesql = input('enter sql: ')
print (thesql)
cur.execute(thesql)
theresult = cur.fetchall()
for j in theresult:
gisadvisor.com, LLC. print(j)
Microsoft Excel

gisadvisor.com, LLC.
Quick and dirty Excel
pip install pypiwin32

import win32com.client
excel = win32com.client.Dispatch("Excel.Application")
mylist = [10,20,30,40,50,60,70,80,90]
myAvg= excel.WorksheetFunction.Average([mylist])
print(myAvg)

gisadvisor.com, LLC.
Advanced Excel
import psycopg2, win32com.client, numpy as np
conn = psycopg2.connect(dbname = 'tompkins',host='127.0.0.1',user='postgres',password='postgres', port='5433')
excel = win32com.client.Dispatch("Excel.Application")
cur = conn.cursor()
thesql = ('SELECT asmt FROM tcparcel')
cur.execute(thesql)
theresult = cur.fetchall()

# Here we will issue an Excel function to determine the standard deviation from the
# selected values
thestdev = excel.WorksheetFunction.StDev(theresult)
print ('The Standard Deviation is: ' + str(thestdev))

# Now we will add the confidence interval, as calculated by Excel


conf = excel.WorksheetFunction.Confidence(.45,thestdev,len(theresult))

print ('Average: ' + str(np.average(theresult)) + ' Confidence Interval: ' + str(conf))


print(' ')
print (' ')
print (str(np.average(theresult) - conf) + ' ' + str(np.average(theresult)) + ' ' +
str(np.average(theresult) + conf))
conn.close()

gisadvisor.com, LLC.
GEOCODING: GEOCODER, CENSUSGEOCODER,
GOOGLE
SPATIAL OPERATIONS: POSTGIS

Now for the spatial


SPATIAL DATABASE: POSTGRES

stuff

gisadvisor.com, LLC.
gisadvisor.com, LLC.
Geocoding
from googleapi import googleapikey

requests.packages.urllib3.disable_warnings()

import geocoder
apikey = googleapikey
f = open('c:/training/python/addresses.csv','r')
googleout = open('c:/temp/googleoutaddress.csv','w')
googleout.write("\"address\",\"lon\",\"lat\",\"matchcode\" \n")

for line in f:
gc = geocoder.google(line,key=apikey)
googleout.write(gc.address.replace(',','') + ' , ' + str(gc.lng) + ',' +
str(gc.lat) + ',' + gc.accuracy + '\n')
print (line," has an accuracy of ", gc.accuracy)
googleout.close()
f.close()

gisadvisor.com, LLC.
Geocoding
import censusgeocode
cg = censusgeocode.CensusGeocode()
census_gc = cg.onelineaddress('16 Bush Lane Ithaca NY')
Print( census_gc)

import censusgeocode
cg = censusgeocode.CensusGeocode()
f = open('c:/training/python/addresses.csv', 'r')
out = open('c:/temp/outaddress1.csv','w')
out.write('addr, longitude, latitude, accuracy '+'\n')

for line in f:
census_gc = cg.onelineaddress(line)
if len(census_gc) > 0:
out.write(str(line) + ',' + str(census_gc[0]['coordinates']['x']) + ',' +
str(census_gc[0]['coordinates']['y'])+'\n')

gisadvisor.com, LLC.
import psycopg2, geocoder
from googleapi import googleapikey

##requests.packages.urllib3.disable_warnings()

conn = psycopg2.connect(dbname = 'tompkins',host='127.0.0.1',user='postgres',password='postgres', port=5433)


cur = conn.cursor()

apikey = googleapikey
f = open('c:/training/python/addresses.csv','r')

thesql = 'DROP TABLE IF EXISTS newpoints'


cur.execute(thesql)
thesql = 'CREATE TABLE newpoints (id SERIAL PRIMARY KEY, address VARCHAR, geom geometry(point,4326))'
print( thesql)
cur.execute(thesql)

for line in f:
gc = geocoder.google(line,key=apikey)
if gc.accuracy == 'ROOFTOP':
thesql = 'INSERT INTO newpoints(address,geom) VALUES (\'' + gc.location + '\',ST_SetSRID(ST_Point(' +
str(gc.lng) + ',' + str(gc.lat) + '),4326))'
cur.execute(thesql)
print(thesql)
conn.commit()
f.close()

gisadvisor.com, LLC.
Excel quiz

Create another list called mylist 2:


[10,8,19,40,50,15,122,33,44]
What is the standard deviation of the list?
Create another list called mylist 2:

[16,22,30,45,58,69,77,88,99]
What is the Pearson correlation coefficient?

gisadvisor.com, LLC.
Arcpy Examples

gisadvisor.com, LLC.
Working with Arcpy in ArcGIS
Fire up ArcMap
Click Python window
Start typing code
Look up help files – they are
excellent!

gisadvisor.com, LLC.
Things you do most of the GIS Useage
time…
Load layers
Select layers by attributes
Select layers by some spatial
operation Load Layers Attribute Qry Spatial Qry Other actions

Perform some kind of spatial


analysis

gisadvisor.com, LLC.
Buffer watershed and clip
parcels
import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.workspace = "c:/training/python/tompkins.gdb"
wstemp = arcpy.MakeFeatureLayer_management("watersheds","wsclip","watershed = 'Cascadilla
Creek'")
##testvar = input("How far do you want the buffer in meters")
testvar = 500
wsbuffer = arcpy.Buffer_analysis(wstemp,"wsbuffer",testvar)

wsclip = arcpy.Clip_analysis("parcels","wsbuffer","parclip")
arcpy.Delete_management(wsbuffer)
arcpy.Delete_management(wsclip)

gisadvisor.com, LLC.
Total value of land in the AE
floodzone by land use
import arcpy
import numpy
arcpy.env.workspace = "c:/training/python/tompkins.gdb"
parceltemp = arcpy.MakeFeatureLayer_management("parcels")
floodtemp = arcpy.MakeFeatureLayer_management("floodzones","zone = 'AE'")

LUs = [row[0] for row in arcpy.da.SearchCursor(parceltemp,"propclass")]


LUs = list(set(LUs))
for lu in LUs:
parsel = arcpy.SelectLayerByLocation_management(parceltemp,"INTERSECT",floodtemp)
parsubsel = [row[0] for row in arcpy.da.FeatureClassToNumPyArray(parsel,"asmt", "propclass = '" + lu +
"'")]
totalvalue = numpy.sum(parsubsel)
avgvalue = numpy.average(parsubsel)
stdvalue = numpy.std(parsubsel)
print "total value of " + lu + "= " + str(totalvalue) #+ " and the CV is " + str(stdvalue / avgvalue)

gisadvisor.com, LLC.
QGIS Examples

gisadvisor.com, LLC.
Working with Qpy in QGIS
Fire up QGIS
Click Python window
Start typing code
Look up help files – not so
excellent 

gisadvisor.com, LLC.
Some QGIS documentation

https://
docs.qgis.org/testing/en/docs/user_manual/pro
cessing/console.html

Let’s try it:


processing.algorithmHelp("native:buffer")
Start typing code

gisadvisor.com, LLC.
Buffer watershed and clip
parcels
import qgis
fz = QgsProject.instance().mapLayersByName('floodzones')[0]
fz.selectByExpression('zone = \'AE\'',QgsVectorLayer.SetSelection)
parcels = QgsProject.instance().mapLayersByName('tcparcel')[0]

##testvar = input("How far do you want the buffer in meters")


testvar = 500
mybuff = processing.run("native:buffer",{'INPUT': fz,'DISTANCE':100,
'OUTPUT':"c:/temp/fzbuff.shp"})
parclip = processing.run("native:clip",{'INPUT': parcels,'OVERLAY':'c:/temp/fzbuff.shp',
'OUTPUT':'c:/temp/parclip.shp'})
cliplayer = QgsVectorLayer('c:/temp/parclip.shp','parclip','ogr')
QgsProject.instance().addMapLayer(cliplayer)

gisadvisor.com, LLC.
import qgis

Buffer watershed and clip


floodzones = iface.addVectorLayer('c:/training/python/shapefiles/Floodzones.shp', "", "ogr")
floodzones.setName('floodz')
parcels = iface.addVectorLayer('c:/training/python/tompkins.gdb|layername=parcels', "parcels",
parcels
"ogr")
firestations = iface.addVectorLayer('c:/training/python/shapefiles/FireStations.shp',
"firestations", "ogr")
firebuf = processing.runAndLoadResults("native:buffer",
{'INPUT':firestations,'DISTANCE':100,'OUTPUT':'memory:'})

firesp = processing.runAndLoadResults('qgis:reprojectlayer',{'INPUT': firestations,


'TARGET_CRS': 'EPSG:32618', 'OUTPUT': 'memory:firereproj'})
firebuf = processing.runAndLoadResults("native:buffer",
{'INPUT':'Reprojected','DISTANCE':400,'OUTPUT':'memory:'})

selectedfz = processing.run("native:extractbyexpression", {'INPUT':'floodz',


'EXPRESSION':'ZONE = \'AE\'','OUTPUT':'memory:'})

selectedfz = processing.runAndLoadResults("native:extractbyexpression", {'INPUT':'floodz',


'EXPRESSION':'ZONE = \'AE\'','OUTPUT':'memory:'})

selectedfz.featureCount()

selpar = processing.runAndLoadResults("native:extractbylocation",
{'INPUT':'parcels','PREDICATE':[0],'INTERSECT':"Matching features",'OUTPUT':'memory:'})
gisadvisor.com,
['OUTPUT'] LLC.
Total value of land in the AE
floodzone by land use
import numpy
sp = QgsProject.instance().mapLayersByName('Extracted (location)')[0]
selected_features = sp.selectAll()
features = sp.getFeatures()
myidx = features.fields().indexFromName('asmt')
myarray = []
for i in features:
myarray.append(i[19])
print(numpy.sum(myarray))

gisadvisor.com, LLC.
Total value of land in the AE
floodzone by land use
import qgis, numpy
floodzones = iface.addVectorLayer('c:/training/python/shapefiles/Floodzones.shp', "", "ogr")
floodzones.setName('floodz')
parcels = iface.addVectorLayer('c:/training/python/tompkins.gdb|layername=parcels', "parcels", "ogr")
selectedfz = processing.runAndLoadResults("native:extractbyexpression", {'INPUT':'floodz', 'EXPRESSION':'ZONE
= \'AE\'','OUTPUT':'memory:'})
selpar = processing.runAndLoadResults("native:extractbylocation",{'INPUT':'parcels','PREDICATE':
[0],'INTERSECT':"Matching features",'OUTPUT':'memory:'})['OUTPUT']

sp = QgsProject.instance().mapLayersByName('Extracted (location)')[0]
selected_features = sp.selectAll()
features = sp.getFeatures()
myidx = parcels.fields().indexFromName('asmt')
myarray = []
for i in features:
myarray.append(i[myidx])
print(numpy.sum(myarray))

gisadvisor.com, LLC.
gisadvisor.com, LLC.
Municipal Risk
Exploration of vector functions in Arcpy
For this exercise, you will explore the use of vector analysis to
determine at-risk properties in certain flood zones, and categorize the
data. Let’s assume our stakeholders are interested in:
• The total value of all land within the ‘AE’ flood zone.

• The total value of residential properties in the ‘AE’ flood zone.

• The amount of residential land in the ‘AE’ flood zone.

gisadvisor.com, LLC.
Steps – in ArcGIS
import arcpy
myconn =
arcpy.CreateDatabaseConnection_management("c:/temp/","tompkins","POSTGRESQL","127.0.0
.1,5433","DATABASE_AUTH","postgres","postgres","SAVE_USERNAME","tompkins")
arcpy.SelectLayerByAttribute_management("tompkins.public.floodzones","NEW_SELECTION",
"zone='AE'")
arcpy.SelectLayerByLocation_management("tompkins.public.tcparcel","INTERSECT","tompki
ns.public.floodzones",0,"NEW_SELECTION")
arcpy.Statistics_analysis("tompkins.public.tcparcel","statout",
[["asmt","SUM"]],"propclass")

gisadvisor.com, LLC.
Steps – separate script
import arcpy
arcpy.env.overwriteOutput = True

if arcpy.Exists(r'c:\temp\tompkins.sde'):
arcpy.env.workspace = 'c:/temp/tompkins.sde'
myconn = arcpy.env.workspace
else:
myconn =
arcpy.CreateDatabaseConnection_management("c:/temp/","tompkins","POSTGRESQL","127.0.0.1,5433","DAT
ABASE_AUTH","postgres","postgres","SAVE_USERNAME","tompkins")

sdeconn = str(myconn)+"\\"
floodfeat = arcpy.MakeFeatureLayer_management(sdeconn+"tompkins.public.floodzones")
parcelfeat = arcpy.MakeFeatureLayer_management(sdeconn+"tompkins.public.tcparcel")

aeflood = arcpy.SelectLayerByAttribute_management(floodfeat,"NEW_SELECTION","zone='AE'")
parflood =
arcpy.SelectLayerByLocation_management(parcelfeat,"INTERSECT",aeflood,0,"NEW_SELECTION")
##arcpy.Statistics_analysis(parflood,"c:/temp/statoutpf/statoutpf",[["asmt","SUM"]],"propclass")

stattable = arcpy.Statistics_analysis(parflood,"c:/temp/statoutpf.dbf",
[["asmt","SUM"]],"propclass")
for row in arcpy.SearchCursor(stattable):
print row.propclass + " $" + str(row.SUM_asmt)
gisadvisor.com, LLC.
The total value of Residential properties
within the ‘X’ flood zone.
import arcpy
arcpy.env.workspace = r'C:\training\python\tompkins.gdb'
arcpy.MakeFeatureLayer_management('parcels', 'parcel_layer')
arcpy.MakeFeatureLayer_management('floodzones', 'firm_layer')
arcpy.SelectLayerByLocation_management('parcel_layer', 'intersect',
'firm_layer')
arcpy.SelectLayerByAttribute_management('parcel_layer','SUBSET_SELECTION',
"propclass = 'Residential'")

gisadvisor.com, LLC.
import googlemaps

gmaps = googlemaps.Client(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
startAddress = "1440 East Sandy Acres Drive, Salisbury MD"
endAddresses = ["6 Briaroot Drive, Smithtown, NY","7 Hickory Road, Bayville NY",
"16 Bush Lane, Ithaca, NY"]
for endAddress in endAddresses:
directions = gmaps.directions(startAddress, endAddress)
print "time to " + endAddress + ": " + directions[0]['legs'][0]['duration']
['text']

gisadvisor.com, LLC.
import arcpy, math
fc = arcpy.GetParameterAsText(0)

fields = arcpy.ListFields(fc, "bivariate")


if len(fields) != 1:
arcpy.AddField_management(fc, "bivariate", "text", 3)
f1 = arcpy.GetParameterAsText(1)
f2 = arcpy.GetParameterAsText(2)
fields = ['bivariate',f1,f2]
var1 = [row[0] for row in arcpy.da.SearchCursor(fc, fields[1])]
var2 = [row[0] for row in arcpy.da.SearchCursor(fc, fields[2])]
bivariate1 = max(var1)
bivariate2 = max(var2)
print str(bivariate1)
with arcpy.da.UpdateCursor(fc,fields) as cursor:
for row in cursor:
first = math.ceil((float(row[1]) / (float(bivariate1) +.001))*3)
second = math.ceil((float(row[2]) / (float(bivariate2) +.001))*3)
row[0] = str(int(first)) + '.' + str(int(second))
cursor.updateRow(row)

gisadvisor.com, LLC.

You might also like