<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: add time constraint to near tool in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708399#M54916</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;thanks for the reminder about overwriting problem.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Actually I get confused starting at # loop through houses! Is this loop&lt;/P&gt;&lt;P&gt;still consistent with what you posted earlier? For each house, find all&lt;/P&gt;&lt;P&gt;fires in the past 7 years based on house sale date. Then calculate distance&lt;/P&gt;&lt;P&gt;of each fire to the house and store the shorted one!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Chelsea&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 09 Apr 2015 17:14:52 GMT</pubDate>
    <dc:creator>QiuhuaMa</dc:creator>
    <dc:date>2015-04-09T17:14:52Z</dc:date>
    <item>
      <title>add time constraint to near tool</title>
      <link>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708390#M54907</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P style="color: #222222; font-family: arial, sans-serif; font-size: 12.8000001907349px;"&gt; Hi,&lt;/P&gt;&lt;P style="color: #222222; font-family: arial, sans-serif; font-size: 12.8000001907349px;"&gt;&lt;/P&gt;&lt;P style="color: #222222; font-family: arial, sans-serif; font-size: 12.8000001907349px;"&gt;I am working on my dissertation right now and need to use Arcgis to calculate the distance from the nearest fire to each house. &lt;/P&gt;&lt;P style="color: #222222; font-family: arial, sans-serif; font-size: 12.8000001907349px;"&gt;&lt;/P&gt;&lt;P style="color: #222222; font-family: arial, sans-serif; font-size: 12.8000001907349px;"&gt;Since fire is polygon and house is point, I just use near tool to calculate distance from the nearest fire to the house. However the fires I am interested in is the fire that burned within the past 7 years before the sale of the homes. So I need to exclude all fires that burned after the sale date of the home, and all fires that burned greater than seven years before the sale of the home.&lt;/P&gt;&lt;P style="color: #222222; font-family: arial, sans-serif; font-size: 12.8000001907349px;"&gt;&lt;/P&gt;&lt;P style="color: #222222; font-family: arial, sans-serif; font-size: 12.8000001907349px;"&gt;Is there anyway to add a time constraint to near tool using python?&amp;nbsp; &lt;/P&gt;&lt;P style="color: #222222; font-family: arial, sans-serif; font-size: 12.8000001907349px;"&gt;&lt;/P&gt;&lt;P style="color: #222222; font-family: arial, sans-serif; font-size: 12.8000001907349px;"&gt;thanks,&lt;/P&gt;&lt;P style="color: #222222; font-family: arial, sans-serif; font-size: 12.8000001907349px;"&gt;&lt;/P&gt;&lt;P style="color: #222222; font-family: arial, sans-serif; font-size: 12.8000001907349px;"&gt;chelsea&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 Apr 2015 15:30:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708390#M54907</guid>
      <dc:creator>QiuhuaMa</dc:creator>
      <dc:date>2015-04-08T15:30:32Z</dc:date>
    </item>
    <item>
      <title>Re: add time constraint to near tool</title>
      <link>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708391#M54908</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Can you just apply a definition query on the fire layer that meets your requirements then run your tool?&amp;nbsp; Or do you have a specific python script that needs modified to accomplish this?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 Apr 2015 15:37:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708391#M54908</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2015-04-08T15:37:45Z</dc:date>
    </item>
    <item>
      <title>Re: add time constraint to near tool</title>
      <link>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708392#M54909</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I assume that the sale of each house can have a different date, right? In that case the definition query on the fires per house would be different. In that case you don't really need to use the Near tool, but you could use the geometries to determine the distance. Do you have a maximum search distance defined?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This would require a loop through the houses, determine the date of sale, define the where clause based on the date to use in the search cursor for the fire polygons, calculate the distance, keep track of the shortest distance and store this as a result probably as an attribute for the current house.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If that is what you are looking for, and maybe if you can share a small part of your data, we can create a script to get that result.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 Apr 2015 17:23:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708392#M54909</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2015-04-08T17:23:19Z</dc:date>
    </item>
    <item>
      <title>Re: add time constraint to near tool</title>
      <link>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708393#M54910</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Xander, you are right! Each house has a different sale date. I didn't&lt;/P&gt;&lt;P&gt;define the maximum search distance.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What you said is exactly what I want to do! I tried to do this manually but&lt;/P&gt;&lt;P&gt;it took so much time!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I attached part of my house sale data and fire history data. Parcelid is&lt;/P&gt;&lt;P&gt;parcel id for each house. Ma_date_dt is sale date of each house.&lt;/P&gt;&lt;P&gt;For each fire, I have fire_day, fire_month and fire_year!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If a house is sold on 07/15/2013, I would like to find the nearest fire to&lt;/P&gt;&lt;P&gt;this house that burned between 2006 and 05/2013.&lt;/P&gt;&lt;P&gt;if a house is sold on 05/02/2011, I would like to find the nearest fire to&lt;/P&gt;&lt;P&gt;the house that burned between 2004 to 03/2011.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I would like to restrict the beginning time to year but the end time&lt;/P&gt;&lt;P&gt;to month/year.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thanks so much,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Chelsea&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 Apr 2015 18:21:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708393#M54910</guid>
      <dc:creator>QiuhuaMa</dc:creator>
      <dc:date>2015-04-08T18:21:21Z</dc:date>
    </item>
    <item>
      <title>Re: add time constraint to near tool</title>
      <link>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708394#M54911</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;the end time of fire need to be 2 month earlier than house sale time since&lt;/P&gt;&lt;P&gt;most sale process took longer than 2 month.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 Apr 2015 19:35:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708394#M54911</guid>
      <dc:creator>QiuhuaMa</dc:creator>
      <dc:date>2015-04-08T19:35:09Z</dc:date>
    </item>
    <item>
      <title>Re: add time constraint to near tool</title>
      <link>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708395#M54912</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;OK, I had some fun with the data and this is what I obtained as result (houses drawn on distance to nearest fire):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="Dist2Fire.png" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/83342_Dist2Fire.png" style="height: auto;" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Preparation:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;I projected the Fire polygons to the same spatial reference as the houses&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Change in the code below:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;paths to input data on lines 7 and 8&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
from datetime import datetime, date, timedelta
from dateutil.relativedelta import relativedelta

def main():
&amp;nbsp;&amp;nbsp;&amp;nbsp; # input fc's
&amp;nbsp;&amp;nbsp;&amp;nbsp; fc_house = r"C:\Forum\DistFireHouse\shp\housesaledata.shp"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fc_fire = r"C:\Forum\DistFireHouse\shp\firehistory_NAD_fips3002_feet.shp"

&amp;nbsp;&amp;nbsp;&amp;nbsp; # input field
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_date_sale = "ma_date_dt"

&amp;nbsp;&amp;nbsp;&amp;nbsp; # output fields
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_dist = "Dist2Fire"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_fire_oid = "FireOID"

&amp;nbsp;&amp;nbsp;&amp;nbsp; # add fields
&amp;nbsp;&amp;nbsp;&amp;nbsp; addField(fc_house, fld_dist, "DOUBLE")
&amp;nbsp;&amp;nbsp;&amp;nbsp; addField(fc_house, fld_fire_oid, "LONG")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # loop through houses
&amp;nbsp;&amp;nbsp;&amp;nbsp; cnt = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; flds = ("SHAPE@", fld_date_sale, fld_dist, fld_fire_oid)
&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.UpdateCursor(fc_house, flds) as curs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in curs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cnt += 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if cnt % 25 == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Processing house: {0}".format(cnt)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pnt = row[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; date_sale = row[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row[2], row[3] = getNearestFire(fc_fire, pnt, date_sale)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; curs.updateRow(row)


def addField(fc, fldname, fldtype):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(arcpy.ListFields(fc, wild_card=fldname)) == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(fc, fldname, fldtype)

def getNearestFire(fc, pnt, date_sale):
&amp;nbsp;&amp;nbsp;&amp;nbsp; # input fields
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_year = "FIRE_YEAR"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_month = "FIRE_MONTH"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_day = "FIRE_DAY"

&amp;nbsp;&amp;nbsp;&amp;nbsp; # defaults
&amp;nbsp;&amp;nbsp;&amp;nbsp; min_dist = 999999
&amp;nbsp;&amp;nbsp;&amp;nbsp; oid_fire = -1

&amp;nbsp;&amp;nbsp;&amp;nbsp; flds = ("SHAPE@", "OID@", fld_year, fld_month, fld_day)
&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(fc, flds) as curs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in curs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; polygon = row[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; oid = row[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; year = int(row[2])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; month = int(row[3])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; month = 6
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; day = int(row[4])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; day = 15

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if chkDate(date_sale, year, month, day):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dist = pnt.distanceTo(polygon)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if dist &amp;lt; min_dist:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; min_dist = dist
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; oid_fire = oid

&amp;nbsp;&amp;nbsp;&amp;nbsp; return min_dist, oid_fire

def chkDate(date_sale, year_fire, month_fire, day_fire):
&amp;nbsp;&amp;nbsp;&amp;nbsp; date_fire = datetime(year=year_fire, month=month_fire, day=day_fire)
&amp;nbsp;&amp;nbsp;&amp;nbsp; date_2monthsbefore_sale = date_sale + relativedelta(months=-2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; year_sale = date_sale.year
&amp;nbsp;&amp;nbsp;&amp;nbsp; test = (year_fire &amp;gt;= year_sale - 7) and (date_fire &amp;lt; date_2monthsbefore_sale)
##&amp;nbsp;&amp;nbsp;&amp;nbsp; if test:
##&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "date_sale: {0}".format(date_sale)
##&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "date_fire: {0}".format(date_fire)
##&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "date_2monthsbefore_sale: {0}".format(date_2monthsbefore_sale)
##&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "year_sale: {0}".format(year_sale)
##&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "{0}&amp;nbsp; &amp;lt;&amp;nbsp; {1}&amp;nbsp; &amp;lt;&amp;nbsp; {2}".format(year_sale-7, date_fire, date_2monthsbefore_sale)
&amp;nbsp;&amp;nbsp;&amp;nbsp; return test

if __name__ == '__main__':
&amp;nbsp;&amp;nbsp;&amp;nbsp; main()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:45:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708395#M54912</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-12T05:45:22Z</dc:date>
    </item>
    <item>
      <title>Re: add time constraint to near tool</title>
      <link>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708396#M54913</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Since it may be a little difficult to verify the result I threw in another piece of code to aid the verification process:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="Connections.png" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/83343_Connections.png" style="width: 620px; height: 312px;" /&gt;&lt;/P&gt;&lt;P&gt;These are lines that connect each house with the nearest fire and add the date info to the lines for verification.&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;FireDate is the date of the fire&lt;/LI&gt;&lt;LI&gt;ma_date_dt is the date when the house was sold&lt;/LI&gt;&lt;LI&gt;YrSaleMn7 is the year of sale minus 7 years&lt;/LI&gt;&lt;LI&gt;SaleMin2m is the date two months before the house was sold &lt;/LI&gt;&lt;LI&gt;The FireOID can be used to join additional data from the fire polygons to the lines (use the FID of the fire polygons)&lt;/LI&gt;&lt;LI&gt;The parcelid provides the link to the houses.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What you need to do is change these lines in the code below:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;reference the input shapefiles on line 10 and 11&lt;/LI&gt;&lt;LI&gt;define the output shapefile on line 23&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Run the code below:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
import os
from datetime import datetime, date, timedelta
from dateutil.relativedelta import relativedelta

def main():
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.overwriteOutput = True

&amp;nbsp;&amp;nbsp;&amp;nbsp; # input fc's
&amp;nbsp;&amp;nbsp;&amp;nbsp; fc_house = r"C:\Forum\DistFireHouse\shp\housesaledata.shp"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fc_fire = r"C:\Forum\DistFireHouse\shp\firehistory_NAD_fips3002_feet.shp"


&amp;nbsp;&amp;nbsp;&amp;nbsp; # input field house
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_fire_oid = "FireOID"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_parcel_id = "parcelid"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_date_sale = "ma_date_dt"

&amp;nbsp;&amp;nbsp;&amp;nbsp; # input fields fires
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_oid_fire = arcpy.Describe(fc_fire).OIDFieldName

&amp;nbsp;&amp;nbsp;&amp;nbsp; # output fc
&amp;nbsp;&amp;nbsp;&amp;nbsp; fc_out = r"C:\Forum\DistFireHouse\shp\lines01.shp"

&amp;nbsp;&amp;nbsp;&amp;nbsp; # output fields (parcelid, double)
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_sale_min7 = "YrSaleMin7"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_sale_2month = "SaleMin2m"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_fire_date = "FireDate"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_dist = "Dist2Fire"

&amp;nbsp;&amp;nbsp;&amp;nbsp; # create empty output fc
&amp;nbsp;&amp;nbsp;&amp;nbsp; sr = arcpy.Describe(fc_house).spatialReference
&amp;nbsp;&amp;nbsp;&amp;nbsp; shp_folder, shp_name = os.path.split(fc_out)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CreateFeatureclass_management(shp_folder, shp_name, "POLYLINE", spatial_reference=sr)

&amp;nbsp;&amp;nbsp;&amp;nbsp; # add output fields
&amp;nbsp;&amp;nbsp;&amp;nbsp; addField(fc_out, fld_parcel_id, "DOUBLE")
&amp;nbsp;&amp;nbsp;&amp;nbsp; addField(fc_out, fld_fire_oid, "LONG")
&amp;nbsp;&amp;nbsp;&amp;nbsp; addField(fc_out, fld_date_sale, "DATE")
&amp;nbsp;&amp;nbsp;&amp;nbsp; addField(fc_out, fld_fire_date, "DATE")
&amp;nbsp;&amp;nbsp;&amp;nbsp; addField(fc_out, fld_sale_min7, "INTEGER")
&amp;nbsp;&amp;nbsp;&amp;nbsp; addField(fc_out, fld_sale_2month, "DATE")
&amp;nbsp;&amp;nbsp;&amp;nbsp; addField(fc_out, fld_dist, "DOUBLE")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # loop through houses
&amp;nbsp;&amp;nbsp;&amp;nbsp; cnt = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; flds_out = ("SHAPE@", fld_parcel_id, fld_fire_oid, fld_date_sale,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_fire_date, fld_sale_min7, fld_sale_2month, fld_dist)
&amp;nbsp;&amp;nbsp;&amp;nbsp; flds_house = ("SHAPE@", fld_parcel_id, fld_date_sale, fld_fire_oid)
&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.InsertCursor(fc_out, flds_out) as curs_out:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(fc_house, flds_house) as curs_in:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row_in in curs_in:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cnt += 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if cnt % 25 == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Processing connection: {0}".format(cnt)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pnt1 = row_in[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; parcel_id = row_in[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; date_sale = row_in[2]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fire_oid = row_in[3]

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # get fire details
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pnt2, date_fire = getDetailsFirePolygon(fc_fire, fld_oid_fire, fire_oid, pnt1)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # create the lines
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; line = arcpy.Polyline(arcpy.Array([pnt1.firstPoint, pnt2.firstPoint]), sr)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; date_2monthsbefore_sale = date_sale + relativedelta(months=-2)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; year_salemin7 = date_sale.year - 7

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row_out = (line, parcel_id, fire_oid, date_sale, date_fire,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; year_salemin7, date_2monthsbefore_sale, line.length, )
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; curs_out.insertRow(row_out)


def getDetailsFirePolygon(fc_fire, fld_oid_fire, fire_oid, pnt):
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_year = "FIRE_YEAR"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_month = "FIRE_MONTH"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld_day = "FIRE_DAY"

&amp;nbsp;&amp;nbsp;&amp;nbsp; where = "{0} = {1}".format(arcpy.AddFieldDelimiters(fc_fire, fld_oid_fire), fire_oid)
&amp;nbsp;&amp;nbsp;&amp;nbsp; row = arcpy.da.SearchCursor(fc_fire, ("SHAPE@", fld_year, fld_month, fld_day), where_clause=where).next()
&amp;nbsp;&amp;nbsp;&amp;nbsp; polygon = row[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; year_fire = int(row[1])
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; month_fire = int(row[2])
&amp;nbsp;&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; month_fire = 6
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; day_fire = int(row[3])
&amp;nbsp;&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; day_fire = 15

&amp;nbsp;&amp;nbsp;&amp;nbsp; date_fire = datetime(year=year_fire, month=month_fire, day=day_fire)

&amp;nbsp;&amp;nbsp;&amp;nbsp; polyline = polygon.boundary()
&amp;nbsp;&amp;nbsp;&amp;nbsp; pnt2 = polyline.queryPointAndDistance(pnt, False)[0]

&amp;nbsp;&amp;nbsp;&amp;nbsp; del row
&amp;nbsp;&amp;nbsp;&amp;nbsp; return pnt2, date_fire

def addField(fc, fldname, fldtype):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(arcpy.ListFields(fc, wild_card=fldname)) == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(fc, fldname, fldtype)

if __name__ == '__main__':
&amp;nbsp;&amp;nbsp;&amp;nbsp; main()&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Add the result to ArcMap and define two relates on the lines featureclass:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;one linking the lines to the houses using the parcelid fields&lt;/LI&gt;&lt;LI&gt;the other linking the lines to the fires using the Fireoid and FID fields&lt;/LI&gt;&lt;/UL&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:45:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708396#M54913</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-12T05:45:25Z</dc:date>
    </item>
    <item>
      <title>Re: add time constraint to near tool</title>
      <link>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708397#M54914</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;thanks so much, Xander!&lt;/P&gt;&lt;P&gt;I am a Econ student and don't have any programming skills. I tried read&lt;/P&gt;&lt;P&gt;your script but can only understand a few.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I will run your code and compare it to results that I got earlier to see&lt;/P&gt;&lt;P&gt;whether they are consistent.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Except for fire history in the past 7 years, I also would like to do fire&lt;/P&gt;&lt;P&gt;history in the past 10, 15 and 20 years.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It seems to me that I only need to change the code in the following 2 lines:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def chkDate(date_sale, year_fire, month_fire, day_fire):&lt;/P&gt;&lt;P&gt;    date_fire = datetime(year=year_fire, month=month_fire, day=day_fire)&lt;/P&gt;&lt;P&gt;    date_2monthsbefore_sale = date_sale + relativedelta(months=-2)&lt;/P&gt;&lt;P&gt;    year_sale = date_sale.year&lt;/P&gt;&lt;P&gt;    test = (&lt;STRONG&gt;year_fire &amp;gt;= year_sale - 7)&lt;/STRONG&gt; and (date_fire &amp;lt;&lt;/P&gt;&lt;P&gt;date_2monthsbefore_sale)&lt;/P&gt;&lt;OL&gt;&lt;OL&gt;&lt;LI level="2" type="ol"&gt;&lt;P&gt;   if test:&lt;/P&gt;&lt;/LI&gt;&lt;LI level="2" type="ol"&gt;&lt;P&gt;       print "date_sale: &lt;SPAN __jive_macro_name="0"&gt;&lt;OL&gt;&lt;OL&gt;&lt;LI level="2" type="ol"&gt;       print "date_fire: &lt;/LI&gt;&lt;/OL&gt;&lt;/OL&gt;".format(date_sale)
&lt;/SPAN&gt;".format(date_fire)&lt;/P&gt;&lt;/LI&gt;&lt;LI level="2" type="ol"&gt;&lt;P&gt;       print "date_2monthsbefore_sale: &lt;SPAN __jive_macro_name="0"&gt;&lt;OL&gt;&lt;OL&gt;&lt;LI level="2" type="ol"&gt;       print "year_sale: &lt;/LI&gt;&lt;/OL&gt;&lt;/OL&gt;".format(date_2monthsbefore_sale)
&lt;/SPAN&gt;".format(year_sale)&lt;/P&gt;&lt;/LI&gt;&lt;LI level="2" type="ol"&gt;&lt;P&gt;       print "&lt;SPAN __jive_macro_name="0"&gt;&lt;/SPAN&gt;  &amp;lt;  &lt;SPAN __jive_macro_name="1"&gt;&lt;/SPAN&gt;  &amp;lt;  &lt;SPAN __jive_macro_name="2"&gt;&lt;/SPAN&gt;".format(&lt;STRONG&gt;year_sale-7,&lt;/STRONG&gt; date_fire,&lt;/P&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;/OL&gt;&lt;P&gt;date_2monthsbefore_sale)&lt;/P&gt;&lt;P&gt;    return test&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if __name__ == '__main__':&lt;/P&gt;&lt;P&gt;    main()&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Am I right? Are there any other changes that I need to make?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thanks,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Chelsea&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Apr 2015 16:41:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708397#M54914</guid>
      <dc:creator>QiuhuaMa</dc:creator>
      <dc:date>2015-04-09T16:41:51Z</dc:date>
    </item>
    <item>
      <title>Re: add time constraint to near tool</title>
      <link>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708398#M54915</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You are right. In fact the only change would be on line 76, where the number 7 should be replaced by the number of years you want to use. It is not necessary to change it on line 82 since it is switched of (commented), but if you uncomment the lines, you would have to adapt that number too.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="text-decoration: underline;"&gt;BUT&lt;/SPAN&gt;, also make sure that you point the result to different output fields or to a different copy of your input input houses. If you don't the new run will overwrite the data in the existing fields.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;If you consider that an answers has been helpful you can mark an answer as such using the link "Helpful Yes | No" below each post.&lt;/EM&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Apr 2015 16:51:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708398#M54915</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2015-04-09T16:51:26Z</dc:date>
    </item>
    <item>
      <title>Re: add time constraint to near tool</title>
      <link>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708399#M54916</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;thanks for the reminder about overwriting problem.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Actually I get confused starting at # loop through houses! Is this loop&lt;/P&gt;&lt;P&gt;still consistent with what you posted earlier? For each house, find all&lt;/P&gt;&lt;P&gt;fires in the past 7 years based on house sale date. Then calculate distance&lt;/P&gt;&lt;P&gt;of each fire to the house and store the shorted one!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Chelsea&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Apr 2015 17:14:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708399#M54916</guid>
      <dc:creator>QiuhuaMa</dc:creator>
      <dc:date>2015-04-09T17:14:52Z</dc:date>
    </item>
    <item>
      <title>Re: add time constraint to near tool</title>
      <link>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708400#M54917</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If you are interested in understanding the code, I can give you some pointers, but this example may not be the best way of learning python. But, anyway, here goes:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Lines 1 to 3 do all the import outside the functions (the "def"s) so that all the functions can make use of them&lt;/LI&gt;&lt;LI&gt;The code actually starts at the very last line (86) calling the main def on line 5&lt;/LI&gt;&lt;LI&gt;As stated before lines 7 and 8 hold references to the input shapefiles&lt;/LI&gt;&lt;LI&gt;Line 11 has the name of the input date field in the house shapefile&lt;/LI&gt;&lt;LI&gt;Lines 14 and 15 have the names for the output fields to be added to input house shapefile (with the result of the analysis)&lt;/LI&gt;&lt;LI&gt;Lines 17 and 18 call a custom function addField (defined on line 35) which checks if the field already exists and if not (length of the list of fields with the provided name is 0) the field is added to the shapefile&lt;/LI&gt;&lt;LI&gt;Line 22 defines a counter variable cnt that will be incremented for each row in the house shapefile&lt;UL&gt;&lt;LI&gt;Lines 26-28 provide some information for the user on the progress (every 25th record it will write a message "Processing house: #" where # is the counter&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;LI&gt;Line 24 starts the update cursor on the houses and it take the list of fields defined on line 23&lt;/LI&gt;&lt;LI&gt;Line 25 will go through each row in the cursor&lt;/LI&gt;&lt;LI&gt;Line 29 reads the geometry, [0] refers to the first index of the list of field, in this case "SHAPE@" &lt;/LI&gt;&lt;LI&gt;Line 30 read the input date of sale of the house&lt;/LI&gt;&lt;LI&gt;Line 31 triggers the analysis. It calls a user defined function called "getNearestFire" defined on line 39 which will return two variables: distance and oid of fire. These will be written to the fields of the row (field [2] and [3]).&lt;/LI&gt;&lt;LI&gt;Line 39 the does the analysis of determining which fire is closest with based on a given date&lt;/LI&gt;&lt;LI&gt;Line 41-43 define the input fields that hold the date of the fire&lt;/LI&gt;&lt;LI&gt;Line 46 and 47 set some initial values for the minimum distance found and the corresponding oid&lt;/LI&gt;&lt;LI&gt;Line 49-51 start the cursor on the fires&lt;/LI&gt;&lt;LI&gt;It extracts the polygon, the oid and the fields for the date of fire&lt;UL&gt;&lt;LI&gt;note that the try, except lines (55-62) is for those cases that the month and day fields are empty&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;LI&gt;Next the chkDate function is called which is defined on line 72 and hold the logic for determining of the fire is in the date range that we are looking for&lt;/LI&gt;&lt;LI&gt;The chkDate function on line 72:&lt;UL&gt;&lt;LI&gt;creates a date object for the fire&lt;/LI&gt;&lt;LI&gt;calculates the date two months before the house was sold&lt;/LI&gt;&lt;LI&gt;defined the year of the sale&lt;/LI&gt;&lt;LI&gt;and tests is the date is what we are looking for (returns True) or not (returns False)&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;LI&gt;On line 64 this result is used. If it is in the date range, the distance between the point and the polygon is calculated (this required the data to be in the same projection) on line 65&lt;/LI&gt;&lt;LI&gt;On line 66 it checks if the distance is less than the minimum distance, and if so it assigns it to the minimum distance and sets the oid to the oid_fire &lt;/LI&gt;&lt;LI&gt;On line 70 the minimum distance and oid of the corresponding fire is returned. which is read on line 31&lt;/LI&gt;&lt;LI&gt;On line 32, the row with the minimum distance and fire oid is stored.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;That is basically it...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Kind regards, Xander&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Apr 2015 18:54:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708400#M54917</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2015-04-09T18:54:54Z</dc:date>
    </item>
    <item>
      <title>Re: add time constraint to near tool</title>
      <link>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708401#M54918</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If you consider that a reply answered your question, please mark that reply as the correct answer. See: &lt;A href="https://community.esri.com/docs/DOC-2159"&gt;Mark a reply as the Correct Answer&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Apr 2015 11:36:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708401#M54918</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2015-04-10T11:36:35Z</dc:date>
    </item>
    <item>
      <title>Re: add time constraint to near tool</title>
      <link>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708402#M54919</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I tried this code with another house sale data. This data has different&lt;/P&gt;&lt;P&gt;fields from the one I posted, but still has house id and sale date.  I got&lt;/P&gt;&lt;P&gt;the following message:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Runtime error&lt;/P&gt;&lt;P&gt;Traceback (most recent call last):&lt;/P&gt;&lt;P&gt;  File "", line 36, in addField&lt;/P&gt;&lt;P&gt;  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\__init__.py",&lt;/P&gt;&lt;P&gt;line 1075, in ListFields&lt;/P&gt;&lt;P&gt;    return gp.listFields(dataset, wild_card, field_type)&lt;/P&gt;&lt;P&gt;  File "c:\program files&lt;/P&gt;&lt;P&gt;(x86)\arcgis\desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 344, in&lt;/P&gt;&lt;P&gt;listFields&lt;/P&gt;&lt;P&gt;    self._gp.ListFields(*gp_fixargs(args, True)))&lt;/P&gt;&lt;P&gt;IOError: "C:\Forum\DistFireHouse\shp\housesaledata.shp" does not exist&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;any idea what happened?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Chelsea&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Apr 2015 21:15:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708402#M54919</guid>
      <dc:creator>QiuhuaMa</dc:creator>
      <dc:date>2015-04-10T21:15:33Z</dc:date>
    </item>
    <item>
      <title>Re: add time constraint to near tool</title>
      <link>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708403#M54920</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I just corrected this error: IOError:&lt;/P&gt;&lt;P&gt;"C:\Forum\DistFireHouse\shp\housesaledata.shp"&lt;/P&gt;&lt;P&gt;does not exist&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;but still got the message:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Runtime error&lt;/P&gt;&lt;P&gt;Traceback (most recent call last):&lt;/P&gt;&lt;P&gt;  File "", line 74, in chkDate&lt;/P&gt;&lt;P&gt;  File&lt;/P&gt;&lt;P&gt;"C:\Python27\ArcGIS10.1\lib\site-packages\dateutil\relativedelta.py", line&lt;/P&gt;&lt;P&gt;247, in __radd__&lt;/P&gt;&lt;P&gt;    raise TypeError, "unsupported type for add operation"&lt;/P&gt;&lt;P&gt;TypeError: unsupported type for add operation&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Apr 2015 21:20:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708403#M54920</guid>
      <dc:creator>QiuhuaMa</dc:creator>
      <dc:date>2015-04-10T21:20:21Z</dc:date>
    </item>
    <item>
      <title>Re: add time constraint to near tool</title>
      <link>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708404#M54921</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Glad to hear that you solved to error referring to the shapefile that was being referenced.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The other error on line 74 in the chkDate function refers to this line:&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="_jivemacro_uid_14287072172119763 jive_macro_code jive_text_macro" data-renderedposition="71_8_912_16" jivemacro_uid="_14287072172119763"&gt;&lt;P&gt;date_2monthsbefore_sale = date_sale + relativedelta(months=-2) &lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The only variable that could influence the error is the date_sale. Are you sure that the date field in the houses is of type Date? Could you check that all records have valid values?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Apr 2015 23:09:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708404#M54921</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2015-04-10T23:09:20Z</dc:date>
    </item>
    <item>
      <title>Re: add time constraint to near tool</title>
      <link>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708405#M54922</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yeah, the problem is some houses don't have a valid sale date! After&lt;/P&gt;&lt;P&gt;deleting these data, it works.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thanks,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Chelsea&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Apr 2015 23:20:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708405#M54922</guid>
      <dc:creator>QiuhuaMa</dc:creator>
      <dc:date>2015-04-10T23:20:57Z</dc:date>
    </item>
    <item>
      <title>Re: add time constraint to near tool</title>
      <link>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708406#M54923</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Glad it works. Better code would include methods to properly handle those cases...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Apr 2015 23:24:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708406#M54923</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2015-04-10T23:24:25Z</dc:date>
    </item>
    <item>
      <title>Re: add time constraint to near tool</title>
      <link>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708407#M54924</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;another way to examine the effect of fire on house price is to check number&lt;/P&gt;&lt;P&gt;and size of fire within a certain distance in the last 7 years.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I would like to count number of fires within 5km of a house in the past 7&lt;/P&gt;&lt;P&gt;years, then calculate their&lt;/P&gt;&lt;P&gt;average size!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Can this be done using python?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;THANKS SO MUCH,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;chelsea&lt;/P&gt;&lt;P&gt;2015年4月10日 下午5:25于 "Xander Bakker" &amp;lt;geonet@esri.com&amp;gt;写道：&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;BLOCKQUOTE level="1"&gt;&lt;P&gt;      GeoNet&lt;/P&gt;&lt;P&gt;&amp;lt;http://jiveon.jivesoftware.com/wf/click?upn=Dg1s4x8le7Lmxv8KWGaqo8h7SGfRSMkw-2FpvHGF9-2FW3rK-2Bvs1kL9-2FnG6jjf2NZhrLDz0M-2BrY-2By9IaziQEKVk3Hg-3D-3D_cklnSfGWc0xtrUWz22hpfRmL-2BltIn2gVqEZxa2WpZ5WxnKfgSyyLU2hOb5I9HulWredg8bzBclHT-2Bssd3ileiOvLFQa-2FmBCtTaSqDGUHKu4XnPDXbl4Ut-2FMsay6tDfBzbVyLltMUwOlSIVVR-2BV5NMQcQfabq6bnsJ3h2e1w1qxNeHuvAFqXMl-2Fgn3Jq-2FWu3P1S-2FUEe4CHuJjCedZY-2BH76WvpBn6acAcWT4wYCAVAPnSBQ3MG2e01gVAgY-2FP54joxFx3SrBDvBPCJclBcl-2B3JuMZQtE3dW34Cm4Y7KaLDVIyvFkRba7LQTlzH6ZiELIuu&amp;gt;     add&lt;/P&gt;&lt;P&gt;time constraint to near tool&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;reply from Xander Bakker&lt;/P&gt;&lt;P&gt;&amp;lt;http://jiveon.jivesoftware.com/wf/click?upn=Dg1s4x8le7Lmxv8KWGaqo7A4BXwO9PY1WvQ5cXCtK4-2FJIS3CK9JWidf-2FyI744iulp2nFUojYk8SjUF04OfNOXam6B1F7Qh0-2B9QYrDAA5RTM-3D_cklnSfGWc0xtrUWz22hpfRmL-2BltIn2gVqEZxa2WpZ5WxnKfgSyyLU2hOb5I9HulWredg8bzBclHT-2Bssd3ileiOvLFQa-2FmBCtTaSqDGUHKu4XnPDXbl4Ut-2FMsay6tDfBzYMfGzF0tzxGItU0XBAVRvIFK1Crre-2BQMAzijTGJvPtjjVYP9Y2zQdaiYRPZQVTXdLWrglYoGLlp7tj9o4J8kB32EMk-2BdykdvaaHciSVPu0coHhC9yF25LKcHvkBr1y2gAnZn7Nufsz3QlcJ-2Fmb-2BV9l9nGGoVsfujd2BJdCilzosqp8OjRNjyb4CuZDTW9M7-2F&amp;gt;&lt;/P&gt;&lt;P&gt;in &lt;STRONG&gt;Python&lt;/STRONG&gt; - View the full discussion&lt;/P&gt;&lt;P&gt;&amp;lt;http://jiveon.jivesoftware.com/wf/click?upn=Dg1s4x8le7Lmxv8KWGaqo0VQjeJBYpG9HGC8QZBT-2FEx1rLkR8-2Fx-2FWdOrqbWRfYSj-2Bj-2F8IDaEgj29-2BmPV8A-2By0H03mtjLHTpxxgI2CO1cdwQ-3D_cklnSfGWc0xtrUWz22hpfRmL-2BltIn2gVqEZxa2WpZ5WxnKfgSyyLU2hOb5I9HulWredg8bzBclHT-2Bssd3ileiOvLFQa-2FmBCtTaSqDGUHKu4XnPDXbl4Ut-2FMsay6tDfBzmKBKWVY715nrdJfp-2FROf-2BiJjyGh0EpaBnitt8dOcfj8-2FIpvsZkweBZlgU8P46bbtoaUsygjnX-2Bj83goL3QHpLVW5CqZCUL-2Fv0eXXfNhrRuQ5TpEhowkCZPUIt9zUhX6lNio2ms1y3ULw3lXOtWDOOXPhz7pltNlXOR9jdHaCaabEZ8BTMt899G3wLZUdw7oA&amp;gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Apr 2015 20:15:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708407#M54924</guid>
      <dc:creator>QiuhuaMa</dc:creator>
      <dc:date>2015-04-13T20:15:42Z</dc:date>
    </item>
    <item>
      <title>Re: add time constraint to near tool</title>
      <link>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708408#M54925</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes, this can be done. The logic should go in the lines 65 - 70. Instead of checking the distance you could determine the number of fire, min, max and mean size and pass this back. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You would need to change to the way the result is read on line 31 and add the appropriate fields to store the results. Lines 13 to 23. It may be good to open a new discussion and add a link to this discussion, since this one is getting a little long...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Apr 2015 21:54:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-time-constraint-to-near-tool/m-p/708408#M54925</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2015-04-13T21:54:37Z</dc:date>
    </item>
  </channel>
</rss>

