Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am working in reorganising our utility data. Two years ago, we have started collecting Private Service water line. We still have a lot of them to extract from old Record Drawings.

I was wondering if there was a way to create line that would join our building footprints to the WaterMain line?

I would like to use the the building vertex closest to the Water main as a starting point.

enter image description here

share|improve this question
What program are you using? Arcmap? version? – RyanDalton Dec 29 '11 at 22:15
I'm using ArcInfo 10 sp3. – Dom Dec 29 '11 at 22:47
up vote 9 down vote accepted

If you are looking for a solution that does not require developing a .NET tool, you can use the python script below to accomplish exactly what you are after. I had exactly the same need and wrote the following script as the solution. Configure it as an ArcCatalog tool with the 4 parameters, or comment out the parameters and uncomment the hardcoded variables and run it directly.

# Author: Jeff Berry
# Description: Creates a line between the nearest vertext on source features
# to the nearest feature in target feature class.
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy
from arcpy import env

# Local variables:
# 1. SourceFC - Feature Class 
# 2. TargetFC - Feature Class
# 3. Output_gdb - Geodatabase
# 4. Output_fc - String

SourceFC = arcpy.GetParameterAsText(0)
TargetFC = arcpy.GetParameterAsText(1)
Output_gdb = arcpy.GetParameterAsText(2)
Output_fc = arcpy.GetParameterAsText(3)

## Alternatively setup hardcoded variables    
##SourceFC = "Buildings"
##TargetFC = "WaterMains"
##Output_gdb = "D:\\New File Geodatabase.gdb"
##Output_fc = "lines_output"

SourceFeaturePoints = "SrcFtrPoints"
arcpy.env.workspace = Output_gdb

# Process: Feature Vertices To Points
arcpy.FeatureVerticesToPoints_management(SourceFC, SourceFeaturePoints, "ALL")

# Process: Near
arcpy.Near_analysis(SourceFeaturePoints, TargetFC, "1000 Feet", "LOCATION", "NO_ANGLE")

# Process: Create Feature Class...
#arcpy.CreateFeatureclass_management(Output_gdb, Output_fc, "POLYLINE", "", "DISABLED", "DISABLED", "", "", "0", "0", "0")
rows = arcpy.SearchCursor(SourceFeaturePoints)

lstIDs = []

for row in rows:

uniqueOBJIDS = set(lstIDs)
newLineList = []
shapeName = arcpy.Describe(SourceFeaturePoints).shapeFieldName

for objID in uniqueOBJIDS:
    rows = arcpy.SearchCursor(SourceFeaturePoints, "\"NEAR_DIST\" = (SELECT MIN( \"NEAR_DIST\") FROM SrcFtrPoints WHERE \"ORIG_FID\"  = " + str(objID) + ")")
    for row in rows:
        arrayLine = arcpy.Array()
        ftr = row.getValue(shapeName)
        pointStart = ftr.firstPoint
        pointEnd = arcpy.Point(row.NEAR_X, row.NEAR_Y)
        plyLine = arcpy.Polyline(arrayLine)

arcpy.CopyFeatures_management(newLineList, Output_fc)
arcpy.Delete_management(SourceFeaturePoints, "FeatureClass")

del rows
del row
del SourceFeaturePoints
del Output_fc
del Output_gdb
share|improve this answer
Thank you Jeff. – Dom Jan 4 '12 at 21:20

Look in to the "NearestFeature" method on IIndexQuery2.

You can use this to get the nearest water main feature to each building. I guess you would then somehow need to loop thru the vertices on each building to find which one is the closest distance to this feature, then build a new polyline using the vertices from the building and watermain as endpoints. The only time I have done this was using two point featureclasses, wish I could offer more than that off the top of my head.. :D

IFeatureCursor pDepthCursor = pDepthSoundings.Search(null, false);
IFeatureIndex2 pFtrInd = new FeatureIndexClass();
pFtrInd.FeatureClass = pDepthSoundings.FeatureClass;
pFtrInd.FeatureCursor = pDepthCursor;
pFtrInd.Index(null, pCombinedEnvelope);
IIndexQuery2 pIndQry = pFtrInd as IIndexQuery2;

int FtdID = 0;
double dDist2Ftr = 0;
pIndQry.NearestFeature(ppoint, out FtdID, out dDist2Ftr);

IFeature pCloseFeature = pDepthSoundings.FeatureClass.GetFeature(FtdID);
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.