Spatial Join. Is possible matching output fields ?

1247
20
Jump to solution
05-09-2022 03:41 AM
PasqualeLanera
New Contributor III

Hello I have ArcGIS 10.3.

I have 2 FC in a GDB, one is a polygon or regular cell of a grid (Grid), the other one is a point feature (PointHabitat). A poygon can contain one or more point. In Point Feature there are two field: AREA, a number that rapresent the surface, and HABITAT, the name of the habitat.

 

Case 3 point features in a cell 

If I do a Spatial Join between Target Feature (Grid) and Join Feature PointHabitat, and choose as Field of Join Features the field AREA Maximum, the output cell have the Field AREA with the value maximum between the 3 point; the Field HABITAT contains the first name of the habitats in alfabetical order.

Instead I want that the output of the Spatial Join will be a cell with the maximum value of the field AREA and for the field HABITAT the name of the habitat of the point feature that has the maximum Area. 

I hope it is clear. I'll prove all night but I don't found the solution. Maybe it's need a python script ?     

Help me, thanks !  

0 Kudos
20 Replies
PasqualeLanera
New Contributor III

Hello.

I askek you to send me  the model because it is not possible read the SQL script in the figure 7.

Thanks a lot

0 Kudos
KimGarbade
Occasional Contributor III

Rather then sending the data and the model and trying to get them to work on your system I think I can explain a little clearer.  

First your questions :

1) I added MyTestValue in PolygonTest to be the unique identifier of the polygon.  I am assuming each of your grid cells has a unique ID other than the ObjectID.  This is the equivalent of that in the test data.

2) That would be the case in my data if I had added two more polygons.  Your data should already have a unique ID assigned to each cell (that is not the ObjectID).

3) ObjectID is a unique Identifier, but Object IDs are not stable and can change when you run analysis (especially analysis that edits or changes the original data) or edit data.  Its better to use a stable, user defined, unique ID (like MyTestValue in this case). 

4) Here it is.  The append tool takes another feature layer as a "Target".  I created this as an empty Feature Class to accept the output of the append.  I simply created a point Feature Class with columns to accept the values I needed.

KimGarbade_0-1652364385353.png

5) "(2)" and "(3)" show the same dataset after it has been acted on by a different tool.  (3) contains the set of records selected by the first Select by Attribute and (2) contains the set of selected records after the second.

The data I used is simply the 13 points in the original post's image and 2 polygons those points fall within.  I created the data in a few minutes just to test the model.

Here is more detail on the model: FindMaxPnt_Image1.png

The code used in the first Select by Attribute tool (using "New Selection" as the Selection Type)

 

Value = (SELECT MAX(VALUE) FROM MaxTestPoints_SpatialJoin2 WHERE  MyTestValue = '%value%')

 

 

The code of the second Select By attribute (Using "Remove from current selection" as the Selection Type)

 

MyTestValue <> '%value%'

 

0 Kudos
PasqualeLanera
New Contributor III

Hello.

Your model RUNNNNNNN with your data!!!!!

Now I have my Feature Class with over 500,000 records.

How can generate values of the field MyTestValue in my Polygon Feature (over 500,000 of records) ?

Can I add the text "TestPoly" or another text to OBJECTID number or to a progressive number using the Field Calculator for my numerous records to obatin an unique ID ? Could you send me the scripts for the Field Calculator to generate this unique ID ?

Another simple solution could be:

copy the OBJECTID into field MyTestValue. Is a good idea ? 

 Thanks, Thanks Thanks my friend

 

0 Kudos
KimGarbade
Occasional Contributor III

Using the images of the model I created you can create your own model using Model Builder in ArcMap, but customized to user your own data.  The images were meant to provide a blue print and the model is not really "plug and play". 

You create a model in a tool box you create:

KimGarbade_2-1652370672503.png

In that tool box you create a new model and edit it:

KimGarbade_3-1652370783461.png

KimGarbade_5-1652370875971.png

 

 

The names of the tool in the model (for example "Select by Attribute") can be dragged into the model from the "Search" dialog:

KimGarbade_1-1652370573794.png

 

The iterator can be added like this:

KimGarbade_0-1652370363399.png

The code I sent for the tools will have to be customized for your data.

There are some good model builder videos online.

0 Kudos
PasqualeLanera
New Contributor III

If I want generate the value for the field MyTestValue I would like to use

PasqualeLanera_0-1652373194828.png

the Field Calculator from Table:

PasqualeLanera_1-1652373306882.png

How can write a script that make a union of a text "TestPoly" and the value of ObjectID or progressive numeber ?

TestPoly4

TestPoly5

...............

TestPoly495054

 

0 Kudos
KimGarbade
Occasional Contributor III

If your Grid Cells do not already have a unique ID other than Object ID I would give them one. 

Backup all of your feature classes first and work on copies of those. 

Then add a column to your Grid Cells polygon FC called something like GridID (String, 25 length).  This will be the equivalent to where I use "MyTestValue".

Use Field Calculator to calculate that field equal to the word "GRID-" with a cell number after it.  That will give them each an "unchangeable" ID.... ("Grid-1','Grid-2',.....'Grid-500000')

KimGarbade_0-1652376025947.png

You have to use the "Python" and "String" options (highlighted)

"Grid-"+ '!OBJECTID!'

 

This will give you a Grid cell feature class (I'm going to call it "GridCellPolys" here for reference) with a unique ID.  That maps to "PolygonTest" in my model.

Spatial join that polygon feature class to your Habitat points to get the equivalent of my MaxTestPoints_SpatialJoin2 feature class (call it something more appropriate like "HabitatPnts_SJ_GridPolys").  

Mapping to the model:

PolygonTest = GridCellPolys

MaxTestPoints_SpatialJoin2 = HabitatPnts_SJ_GridPolys

MyTestValue = GridID

Habitat = Habitat

Value = Area

NOTE: The model does not check for if no points fall within a cell.  This will likely cause it to fail.  Since I don't think Model Builder had an "If Then" condition in ArcMap 10.3 you'll have to do a select by attribute on you Grid Polygons that have at least one point in them and export that out as your new "PolygonTest" (in my model) feature class.

Good luck.

 

  

0 Kudos
PasqualeLanera
New Contributor III

Thanks a lot.

I have tested you model also with a polygon without point: no errrors, no fail.

So too work and process to create a point feature with only a point in a polygon: the point with max value and his habitat with ArcGIS 10.3 !!!

Are with ArcGIS PRO or with another platform more easy ?

Thanks very much for the support and excuse me for the time that you spent for me.

Good Bye 

 

0 Kudos
KimGarbade
Occasional Contributor III

Your welcome.  If my suggestion helped to solve the problem, would you mind also accepting one of my responses as a solution?  Thank you.

0 Kudos
PasqualeLanera
New Contributor III

Hello Kim.

Can you expalin two things:

1) Select Layer by attribute and  Select by attribute are the same ? If no why you use the first ?

2) In the query: Value = (SELECT MAX(Value) FROM MaxTestPoints_SpatialJoin2 WHERE MyTestValue = '%Value%') why there is '%Value%' and not only Value ? 

0 Kudos
KimGarbade
Occasional Contributor III

1) Its a little confusing, but the the first select by attribute selects all points in the HabitatPnts_SJ_GridPolys that have the same value for area as the highest value in the Grid cell you are processing.  For instance if the current Grid cell being processed is Grid-25 and the maximum habitat area with this cell is 50 hectares, the first query selects all records in the  HabitatPnts_SJ_GridPolys feature class with an Area of 50.  That selection set includes the one (or more) points you are interested in returning, plus all other points with an Area of 50.  The second select by attribute excludes all points not in Grid-25; leaving you with just the points you want (I.E. those points (one or more) that represent the highest habitat by area in that grid cell).  

It is confusing why you have to write two queries when SQL Server could so this no problem in just one, but I think the answer is that ArcMap is limited in its ability to do subqueries, so this was the only way I could get it to work.

2) %Value% is a variable in the model.  It is set each time the iterator loops.  It is the unique identifier of the polygon you are processing this time through the iterator.  It is vital that this be set correctly as the "Field" property of the iterator.

KimGarbade_0-1652468810616.png

 

0 Kudos