Saturday, 10 November 2018

Perform a spatial join in C# with ArcObjects


I feel like this has to be on the internet somewhere, but I haven't been able to find it.


I'm attempting to perform a spatial join on a polygon and point layer. My goal is to push the polygon's ID to each point that it contains. In PostGIS, it might look close to this: SELECT a.*, b.* FROM a, b WHERE ST_CONTAINS(a, b).



I have:



  1. A polygon IFeatureClass, from a geodatabase

  2. A point IFeatureClass, from a geodatabase

  3. The name of the polygon field


I would like to turn this into a point IFeatureClass with an extra field.


My question: How do I do this? Bonus points for links to relevant documentation.



Answer



It's possible to do it with the Geoprocessor in ArcObjects like this:



public static void SpatialJoiner(IFeatureClass sourceFC, IFeatureClass joinFC)
{
Geoprocessor GP = new Geoprocessor();
ESRI.ArcGIS.AnalysisTools.SpatialJoin spatialJoin = new ESRI.ArcGIS.AnalysisTools.SpatialJoin();
spatialJoin.join_features = joinFC;
spatialJoin.target_features = sourceFC;
spatialJoin.join_type = "KEEP_COMMON";
spatialJoin.match_option = "WITHIN";
spatialJoin.fieldmapping = fieldMapping;
spatialJoin.out_feature_class = @"c:\yourPath\your.gdb\outfile";

GP.Execute(spatialJoin,null);
}

Spatial Join class documentation: http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/Overview/00470000328n000000/


Note that the "fieldmMapping" object needs to be constructed and populated with the IGPFieldMapping interface:


IGPFieldMapping fieldMapping = new GPFieldMappingClass();

Field Mapping Documentation: http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#//004800000194000000


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...