how to find nearest point in ArcGIS from another layer?

4117
11
11-05-2019 09:50 PM
Ahsanmohamed
New Contributor

Dear All,

I am very new for ArcGIS, can any one help me to finished this issue.

I have lots of point in different two layers and different location. i need to find nearest point and distance.

Is there any option to get this? 

I already tried "Generate Near Table" (ArcToolbox--> Analysis Tools--> Proximity --> Generate Near Table.) , But it's not giving right value.

Please guide me.

Thank You.

0 Kudos
11 Replies
Ahsanmohamed
New Contributor

Dear Mr.Johannes,

Thank you for your reply,

Unfortunately i can't delete all existing or all new data witch are matching,

I have compare matching data with another some data, from that only i can erase, because in existing data some are wrong and also some new data.

I need to take all matching data out and i'll compare with some other data and i'll erase.

Please help me.

Once again thank you for your valuable reply..

0 Kudos
JohnMay3
New Contributor III

This is a solution I developed to solve this issue. All you need is SQL access to your database. You should be able to copy and paste this into your SQL editor. It was developed for SQL Server. Slight variance would be needed for Oracle.

/*** Create a table in which to store the results. Table will have a shape field and query will create shape so it can be drawn in ArcGIS ***/
CREATE TABLE NearestNeighbor
(
Coordinates nvarchar(max),
ToLongitude float,
ToLatitude float,
ObjectID smallint,
TargetObjectID smallint,
FromLongitude float,
FromLatitude float,
LengthFeet float,
Shape geometry

);

/*** Update FeatureClass1 and FeatureClass2 to reflect the names of the two point coverages you want to use ***/
/*** Note this script assumes data is in WGS84 or a similar unprojected format. If your data is projected into units measured in feet or meters then you will need to update the geography line accordingly ***/
DECLARE @g geometry;
DECLARE @count smallint = 1
WHILE @count < (SELECT MAX(ObjectID) FROM FeatureClass1) + 1 -- Set to run through each record of the 'From Location' feature class
BEGIN

SET @g = (SELECT Shape FROM FeatureClass1 WHERE ObjectID = @count); -- Select a shape from the 'From Location' feature class

INSERT INTO NearestNeighbor

SELECT TOP(1) a.shape.ToString() as Coordinates -- Returns the coordinates of the closest feature to the 'From Location' feature
, a.shape.STX as ToLongitude
, a.shape.STY as ToLatitude
, @count as ObjectID
, objectid as TargetObjectID
, @g.STX as FromLongitude
, @g.STY as FromLatitude
, geography::STGeomFromText(geometry::STLineFromText('LINESTRING('+cast(@g.STX as varchar) +' '+cast(@g.STY as varchar) +','+cast(a.shape.STX as varchar) +' '+cast(a.shape.STY as varchar)+')', 4326).STAsText(),4326).STLength() * 3.28084 as LengthFeet
, geometry::STLineFromText('LINESTRING('+cast(@g.STX as varchar) +' '+cast(@g.STY as varchar) +','+cast(a.shape.STX as varchar) +' '+cast(a.shape.STY as varchar)+')', 4326) as Shape
FROM FeatureClass2 a -- 'To Location' feature class
WHERE a.shape.STDistance(@g) IS NOT NULL -- Search To Location feature class for nearest point to 'From Location' feature

ORDER BY a.shape.STDistance(@g) -- Select closest point

SET @count=@count+1 -- Set counter for next record in 'From Location' feature class

END

0 Kudos