<?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: Suggestions for an Algorithm for Assigning Values Based on Spatial Proximity in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/suggestions-for-an-algorithm-for-assigning-values/m-p/367324#M28958</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;What is the reason for the spatial assignment?&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 03 May 2018 19:47:59 GMT</pubDate>
    <dc:creator>DanPatterson_Retired</dc:creator>
    <dc:date>2018-05-03T19:47:59Z</dc:date>
    <item>
      <title>Suggestions for an Algorithm for Assigning Values Based on Spatial Proximity</title>
      <link>https://community.esri.com/t5/python-questions/suggestions-for-an-algorithm-for-assigning-values/m-p/367323#M28957</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am trying to come up with an algorithm that can handle the problem of assigning the values of an attribute of a set of polygon features in one feature class to the set of polygon features in another feature class based on proximity.&amp;nbsp; I can conceive of several ways to approach this problem using Python and/or Geoprocessing tools, but I need to optimize the final algorithm for performance.&amp;nbsp; I am hoping for python coding&amp;nbsp;or geoprocessing suggestions that could accomplish each step in the best order to achieve the greatest overall efficiency and speed.&amp;nbsp; The suggestions do not have to tackle the whole problem and can be focused on any part, but I am laying out the complete problem so each step can be seen&amp;nbsp;in light of the overall goal of the algorithm.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Assume I have two polygon feature classes named FC_A and FC_B which have two attribute fields called Case_Field and&amp;nbsp;Value_Field in both feature classes.&amp;nbsp; The following conditions must be met for the algorithm to reach a solution:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1. All conditions listed below&amp;nbsp;should only&amp;nbsp;consider and compare the set of&amp;nbsp;features that have a common Case_Field value in both&amp;nbsp;FC_A and FC_B and if the set of features is not limited to a single Case_Field value then the number of overlapping&amp;nbsp;features in both feature classes is massive.&lt;/P&gt;&lt;P&gt;2. A solution is only reached when every unique value in the Value_Field of FC_A&amp;nbsp;has been be assigned to&amp;nbsp;at least one feature in FC_B and all of the features in FC_B have been assigned a value in the Value_Field from the set of unique values in FC_A.&lt;/P&gt;&lt;P&gt;3. The total number of&amp;nbsp;unique values in Value_Field&amp;nbsp;in FC_A must be equal to or less than the number of features in FC_B to be analyzed by this algorithm, otherwise the&amp;nbsp;set of features associated with that Case_Field value will be ignored.&lt;/P&gt;&lt;P&gt;4. If a feature in FC_A does not overlap any of the features in FC_B the value in Value_Field of FC_A must be assigned to the closest feature in FC_B, provided that the FC_B feature has not previously been assigned a value from FC_A to satisfy this condition.&lt;/P&gt;&lt;P&gt;5. Any feature in FC_B that is overlapped partially or completely by only one feature in FC_A will be assigned the value from the FC_A&amp;nbsp; feature that overlaps it, provided that the FC_B feature has not already been assigned a value to satisfy condition 4 above.&lt;/P&gt;&lt;P&gt;6.&amp;nbsp;&lt;SPAN&gt;Any feature in FC_B that has portions&amp;nbsp;overlapped by&amp;nbsp;two or more features in FC_A will be assigned the value from the FC_A feature that overlaps its centroid or that is closest to its centroid if the centroid is not overlapped, provided that the FC_B feature has&amp;nbsp;&lt;SPAN&gt;not already been assigned a value to satisfy condition 4 above&lt;/SPAN&gt;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;An illustration of the problem is shown below for one of the sets of features in FC_A and FC_B that all share a common Case_Field value.&amp;nbsp; The polygons with a grey fill and a colored outline are in FC_A and are labeled with the value stored in the Value_Field.&amp;nbsp; The polygons that are colored light purple are in FC_B and have Null in the Value_Field.&amp;nbsp; (Note: some of the values in the Value_Field of FC_A&amp;nbsp;are actually&amp;nbsp;associated with many polygons, but they should all be treated as a single dissolved polygon&amp;nbsp;for the purposes of this algorithm.)&lt;/P&gt;&lt;P&gt;&lt;IMG alt="Input Polygons for a given Case Value in FC_A and FC_B" class="image-1 jive-image j-img-original" src="https://community.esri.com/legacyfs/online/405656_Value_Allocation_Input.png" /&gt;&lt;/P&gt;&lt;P&gt;The solution should look something like the output below.&amp;nbsp; The FC_B fill and the FC_A&amp;nbsp;outline have been assigned the same color for each value in the Value_Field&lt;/P&gt;&lt;P&gt;&lt;IMG alt="Output of FC_A and FC_B solution" class="image-2 jive-image j-img-original" src="https://community.esri.com/legacyfs/online/405657_Value_Allocation_Output.png" /&gt;&lt;/P&gt;&lt;P&gt;Notice that values from the polygons in FC_A that do not overlap any of the polygons in FC_B have still been assigned to one of the FC_B polygons.&amp;nbsp; I did this manually, so I may not have chosen the closest polygon to satisfy condition 4, but I would expect the algorithm to chose the closest polygon.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If it turns out that after fully optimizing the steps that evaluate&amp;nbsp;spatial proximity those steps&amp;nbsp;would take&amp;nbsp;5 or more minutes to reach the solution of the sample problem above, I would probably end up settling for a solution that only satisfies conditions 1&amp;nbsp;through 3 using the fastest python algorithm that can randomly&amp;nbsp;distribute&amp;nbsp;the values from FC_A into FC_B.&amp;nbsp; However, I prefer a solution that takes the spatial relationship into account.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 03 May 2018 19:37:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/suggestions-for-an-algorithm-for-assigning-values/m-p/367323#M28957</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2018-05-03T19:37:56Z</dc:date>
    </item>
    <item>
      <title>Re: Suggestions for an Algorithm for Assigning Values Based on Spatial Proximity</title>
      <link>https://community.esri.com/t5/python-questions/suggestions-for-an-algorithm-for-assigning-values/m-p/367324#M28958</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;What is the reason for the spatial assignment?&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 03 May 2018 19:47:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/suggestions-for-an-algorithm-for-assigning-values/m-p/367324#M28958</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2018-05-03T19:47:59Z</dc:date>
    </item>
    <item>
      <title>Re: Suggestions for an Algorithm for Assigning Values Based on Spatial Proximity</title>
      <link>https://community.esri.com/t5/python-questions/suggestions-for-an-algorithm-for-assigning-values/m-p/367325#M28959</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The particular set of data I am currently working with could disregard the spatial component, but the&amp;nbsp;value assignment makes much more sense as shown than using a random assignment.&amp;nbsp;&amp;nbsp;However, I have other problems I would love to tackle that I have never attempted, because&amp;nbsp;they are only worth tackling through&amp;nbsp;this kind of spatial algorithm.&amp;nbsp; The data in my example is current and historic parcels&amp;nbsp;and the output I am trying to create mimics parcel history.&amp;nbsp; I do not have access to parcel history data from my Assessor and if I did it&amp;nbsp;would only complicate the problem by adding another many to many relationship I would have to manage.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If the spatial problem was removed I would still be open to suggestions on how to approach the non-spatial portion of the problem, especially if they involved modules beyond native python and arcpy.&amp;nbsp; I have avoided learning numpy up to now, but could be persuaded to learn it if it has application to solving problems like this.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 03 May 2018 22:30:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/suggestions-for-an-algorithm-for-assigning-values/m-p/367325#M28959</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2018-05-03T22:30:05Z</dc:date>
    </item>
    <item>
      <title>Re: Suggestions for an Algorithm for Assigning Values Based on Spatial Proximity</title>
      <link>https://community.esri.com/t5/python-questions/suggestions-for-an-algorithm-for-assigning-values/m-p/367326#M28960</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have come up with an algorithm that can meet all of my criteria.&amp;nbsp; In 3 minutes and 13 seconds the code can find solutions for 27,542 records in FC_A and 64,985 records in FC_B that match up on a total of 2,773 unique case values, so the performance is excellent.&amp;nbsp; The code currently only solves the problem without writing an output, but now that the problem is solved I can extend the code to output the results in a variety of different ways, depending on my needs.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A portion of the code could be used to replace the Spatial Join tool when features need to be matched based on a shared case value to avoid unwanted joins due to feature overlap.&amp;nbsp; Additionally, the Spatial Join component of the code is similar to using the One To One option with a case field and the first option on another field in the summary options, but it is enhanced so that it will favor exact attribute matches on the second field over&amp;nbsp;any spatial relationship and otherwise favor overlap of the FC_B centroid with the FC_A polygon over merely being the first FC_A feature to touch the FC_B feature.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Beyond the spatial join, the code has additional logic for matching all unique values in the second field between FC_A and FC_B based on the closest feature when no exact attribute match or feature overlap exists, as I required.&amp;nbsp; So solving problems like this can be done very efficiently with arcpy.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 07 May 2018 21:14:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/suggestions-for-an-algorithm-for-assigning-values/m-p/367326#M28960</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2018-05-07T21:14:50Z</dc:date>
    </item>
  </channel>
</rss>

