Skip to main content
deleted 45 characters in body; edited tags; edited title; edited title
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

How to compute a Computing new attribute based on changes in another attribute using ArcGIS Desktop with Python?

I am attempting to classify a set of gps time-encoded point data into behaviors based on different attributes. 

I have created an attribute that is 0 for home and 1 for away based on location, and now want to number the trips away from home (a set of points 01111111111110 would be one trip because it started and ended at home). I have added the attribute field that will have the trip numbers, but don't know how to calculate the field so it is based on the home/away field.

UPDATE: theseThese are the bare bones of what Python code might look like (I'm not experienced with code).

UPDATE:
After After using matt wilkie's recommended script I've made some changes so that my first trip is number 1, my second is 2, etc etc. 

Here is the code modified from matt's:

Then I just select for home = 0 and calculate my trip field back to 0. Neatly ordered trips. Thanks a ton everyone!

How to compute a new attribute based on changes in another attribute?

I am attempting to classify a set of gps time-encoded point data into behaviors based on different attributes. I have created an attribute that is 0 for home and 1 for away based on location, and now want to number the trips away from home (a set of points 01111111111110 would be one trip because it started and ended at home). I have added the attribute field that will have the trip numbers, but don't know how to calculate the field so it is based on the home/away field.

UPDATE: these are the bare bones of what Python code might look like (I'm not experienced with code).

UPDATE:
After using matt wilkie's recommended script I've made some changes so that my first trip is number 1, my second is 2, etc etc. Here is the code modified from matt's:

Then I just select for home = 0 and calculate my trip field back to 0. Neatly ordered trips. Thanks a ton everyone!

Computing new attribute based on changes in another attribute using ArcGIS Desktop with Python?

I am attempting to classify a set of gps time-encoded point data into behaviors based on different attributes. 

I have created an attribute that is 0 for home and 1 for away based on location, and now want to number the trips away from home (a set of points 01111111111110 would be one trip because it started and ended at home). I have added the attribute field that will have the trip numbers, but don't know how to calculate the field so it is based on the home/away field.

These are the bare bones of what Python code might look like (I'm not experienced with code).

After using matt wilkie's recommended script I've made some changes so that my first trip is number 1, my second is 2, etc etc. 

Here is the code modified from matt's:

Then I just select for home = 0 and calculate my trip field back to 0. Neatly ordered trips.

edited body
Source Link
AlmaThom
  • 842
  • 9
  • 17
import arcpy
rows = arcpy.UpdateCursor("test2")

trip = 0
for row in rows:
    if row.home == 0:
        prev = row.home
        row.TRIP = trip
        rows.updateRow(row)

    elif row.home == 01 and prev == 10:
        trip += 1
        prev = row.home
        row.TRIP = trip
        rows.updateRow(row)
        rows.next()

    elif row.home == 01 and prev == 01:
        prev = row.home
        row.TRIP = trip
        rows.updateRow(row)
        rows.next()

    row.TRIP = trip
    rows.updateRow(row)


del row, rows
import arcpy
rows = arcpy.UpdateCursor("test2")

trip = 0
for row in rows:
    if row.home == 0:
        prev = row.home
        row.TRIP = trip
        rows.updateRow(row)

    elif row.home == 0 and prev == 1:
        trip += 1
        prev = row.home
        row.TRIP = trip
        rows.updateRow(row)
        rows.next()

    elif row.home == 0 and prev == 0:
        prev = row.home
        row.TRIP = trip
        rows.updateRow(row)
        rows.next()

    row.TRIP = trip
    rows.updateRow(row)


del row, rows
import arcpy
rows = arcpy.UpdateCursor("test2")

trip = 0
for row in rows:
    if row.home == 0:
        prev = row.home
        row.TRIP = trip
        rows.updateRow(row)

    elif row.home == 1 and prev == 0:
        trip += 1
        prev = row.home
        row.TRIP = trip
        rows.updateRow(row)
        rows.next()

    elif row.home == 1 and prev == 1:
        prev = row.home
        row.TRIP = trip
        rows.updateRow(row)
        rows.next()

    row.TRIP = trip
    rows.updateRow(row)


del row, rows
Updated script that is neater.
Source Link
AlmaThom
  • 842
  • 9
  • 17

UPDATE:
After using matt wilkie's recommended script I've made some changes so that my first trip is number 1, my second is 2, etc etc. Here is the code modified from matt's:

import arcpy
rows = arcpy.UpdateCursor("test2")

trip = 0
for row in rows:
    if row.home == 0:
        prev = row.home
        row.TRIP = trip
        rows.updateRow(row)

    elif row.home == 0 and prev == 1:
        trip += 1
        prev = row.home
        row.TRIP = trip
        rows.updateRow(row)
        rows.next()

    elif row.home == 0 and prev == 0:
        prev = row.home
        row.TRIP = trip
        rows.updateRow(row)
        rows.next()

    row.TRIP = trip
    rows.updateRow(row)


del row, rows

Then I just select for home = 0 and calculate my trip field back to 0. Neatly ordered trips. Thanks a ton everyone!

UPDATE:
After using matt wilkie's recommended script I've made some changes so that my first trip is number 1, my second is 2, etc etc. Here is the code modified from matt's:

import arcpy
rows = arcpy.UpdateCursor("test2")

trip = 0
for row in rows:
    if row.home == 0:
        prev = row.home
        row.TRIP = trip
        rows.updateRow(row)

    elif row.home == 0 and prev == 1:
        trip += 1
        prev = row.home
        row.TRIP = trip
        rows.updateRow(row)
        rows.next()

    elif row.home == 0 and prev == 0:
        prev = row.home
        row.TRIP = trip
        rows.updateRow(row)
        rows.next()

    row.TRIP = trip
    rows.updateRow(row)


del row, rows

Then I just select for home = 0 and calculate my trip field back to 0. Neatly ordered trips. Thanks a ton everyone!

edited body
Source Link
AlmaThom
  • 842
  • 9
  • 17
Loading
Tweeted twitter.com/#!/StackGIS/status/211088752909623296
Provided an example
Source Link
whuber
  • 70.2k
  • 16
  • 187
  • 282
Loading
improved formatting
Source Link
blah238
  • 35.9k
  • 8
  • 96
  • 200
Loading
edited tags
Link
AlmaThom
  • 842
  • 9
  • 17
Loading
added 339 characters in body
Source Link
AlmaThom
  • 842
  • 9
  • 17
Loading
Source Link
AlmaThom
  • 842
  • 9
  • 17
Loading