A slight modification of the answer linked in the comments would be to do a Summary Statistics to get the SUM of the Shape.Length
field of the intersect feature class, using the FID field of the polygons a case field, and then Join Field that back to the polygon feature class.
In the ArcMap 10.1 Python window, these commands worked for me:
intersection = arcpy.Intersect_analysis("RoadCenterline;SchoolTaxDistrict", r"in_memory\intersect")
arcpy.CalculateField_management(intersection, "SHAPE_Leng", "!shape.length!", "PYTHON_9.3")
summary = arcpy.Statistics_analysis(intersection, r"in_memory\statistics", "SHAPE_Leng SUM", "FID_SchoolTaxDistrict")
arcpy.JoinField_management("SchoolTaxDistrict", "FID", summary, "FID_SchoolTaxDistrict")
ST_ClosestPoint
will find the closest point on a linestring. For example, locating a point on a road that is closest to a point-of-interest off to one side:
SELECT ST_AsText(ST_ClosestPoint(road, poi))
FROM (SELECT
'LINESTRING (10 10, 20 20)'::geometry AS road,
'POINT (14 10)'::geometry AS poi
) AS f;
st_astext
--------------
POINT(12 12)
(1 row)
There are theoretically an infinite number of points that are on a line, and this location is exactly on the road linestring:
SELECT ST_Intersects('LINESTRING (10 10, 20 20)'::geometry, 'POINT (12 12)'::geometry);
st_intersects
---------------
t
(1 row)
If the objective is to get the closest vertex or coordinate from the linestring, the road needs to be converted to a MultiPoint geometry. Here's a function that does that:
CREATE OR REPLACE FUNCTION ST_AsMultiPoint(geometry) RETURNS geometry AS
'SELECT ST_Union((d).geom) FROM ST_DumpPoints($1) AS d;'
LANGUAGE sql IMMUTABLE STRICT COST 10;
Then use it similar to before:
SELECT ST_AsText(ST_ClosestPoint(ST_AsMultiPoint(road), poi))
FROM (SELECT
'LINESTRING (10 10, 20 20)'::geometry AS road,
'POINT (14 10)'::geometry AS poi
) AS f;
st_astext
--------------
POINT(10 10)
(1 row)
Best Answer
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.