ThinkGeo.com    |     Documentation    |     Premium Support

How to find long/lat in the road?

Hello,


I have a problem.


If I input long/lat in the Map and I use GetFeaturesWithinDistance to find the closest road.


Then, I want to find which point (coordinate) be calculated on the road.


(I want to get the long/lat on the road).


 


 



Carol,


Thanks for your post. Try following API, hope it helps:

PointShape closetPointShape = lineShape.GetClosestPointTo(p, GeographyUnit.Meter);

 
Any more quesitons just feel free to let me know.
 
Thanks.
 
Yale

Thanks for your reply, Yale

But I'm sure how to use it,  could you give me some idea? my code as follows:


    Dim DistanceQueryResults As Collection(Of Feature) = Nothing

        Dim road_09007_Layer As ShapeFileFeatureLayer = New ShapeFileFeatureLayer("D:\road_09007.shp")


        road_09007_Layer.Open()

        While var_dis < 110

            DistanceQueryResults = road_09007_Layer.QueryTools.GetFeaturesWithinDistanceOf(lineFeature, GeographyUnit.DecimalDegree, DistanceUnit.Meter, var_dis, New String() {"ADR", "TNODE ", "FNODE"})


            If DistanceQueryResults.Count = 0 Then

                var_dis = var_dis + 20

                DistanceQueryResults = road_09007_Layer.QueryTools.GetFeaturesWithinDistanceOf(lineFeature, GeographyUnit.DecimalDegree, DistanceUnit.Meter, var_dis, New String() {"ADR", "TNODE ", "FNODE"})

            Else


            End If

        End While

        Dim closetPointShape As PointShape = road_09007_Layer.GetClosestPointTo(DistanceQueryResults, GeographyUnit.Meter)

        road_09007_Layer.Close()



Carol,


Thanks for your post.


I think you probabbly missed the targetPoint mentioned in your first post from which to find the closet point. Try the following logic:

            Dim targetPointShape As PointShape = New PointShape(10, 10)
            Dim DistanceQueryResults As Collection(Of Feature) = Nothing
            Dim var_dis As Double = 30
            Dim road_09007_Layer As ShapeFileFeatureLayer = New ShapeFileFeatureLayer("D:\road_09007.shp")
 
            road_09007_Layer.Open()
            While var_dis < 110
                DistanceQueryResults = road_09007_Layer.QueryTools.GetFeaturesWithinDistanceOf(targetPointShape, GeographyUnit.DecimalDegree, DistanceUnit.Meter, var_dis, New String() {"ADR", "TNODE ", "FNODE"})
 
                If DistanceQueryResults.Count = 0 Then
                    var_dis = var_dis + 20
                    DistanceQueryResults = road_09007_Layer.QueryTools.GetFeaturesWithinDistanceOf(targetPointShape, GeographyUnit.DecimalDegree, DistanceUnit.Meter, var_dis, New String() {"ADR", "TNODE ", "FNODE"})
                Else
 
                End If
            End While
            road_09007_Layer.Close()
 
            If (DistanceQueryResults.Count > 0) Then
                Dim resultPointShape As PointShape = DistanceQueryResults(0).GetShape().GetClosestPointTo(targetPointShape, GeographyUnit.Meter)
            End If

 
Any more questions just feel free to let me know.
 
Thanks.
 
Yale

Thank you very much, Yale 
 I modify my code, and it runs well. :) 
  
 sorry, I have another question… 
 Assume that I query a roadname called "ABC" and I use ExecuteQuery to find all recID of "ABC". 
 Then, could I get the coordinate of road.shp by recID?

Carol,


No problem.
 
Try following code to find the feature and then gets its shape:

Feature feature = featureLayer.QueryTools.GetFeatureById("123", ReturningColumnsType.AllColumns);
BaseShape baseShape = feature.GetShape();

 
While the only thing I am concerned about is that in ShapeFileFeatureLayer, its Id probablly only support the number-formatted string.
 
Any more questions just feel free to let me know.
 
Thanks.
 
Yale

Sorry, Yale 
  
 I come back again. 
 I think I describe wrong. 
 I’d like find a roadname contains String of “ABC” in road.shp 
  
 By sql statement like belows: 
  
  Dim inString As String = “ABC”  
  sSql = “select * from newRoad where roadname Like '%” & inString & “%’” 
  road_DT = road_Layer.QueryTools.ExecuteQuery(sSql) 
  
 Then, I want to get their coordinate… 
 Could you give some idea? 
  
 Or, I have to use as follows method to get coordinates. But, how to get its coordinates? 
 Dim feature As Collection(Of Feature) = road_Layer.QueryTools.GetFeaturesByColumnValue(“Roadname”, inString) 


Carol,


            Imports System.Collections.ObjectModel

            ShapeFileFeatureLayer.BuildRecordIdColumn("..\..\SampleData\Data\Countries02.shp", "RecID", BuildRecordIdMode.Rebuild)

            Dim inString As String = "ABC"
            Dim sSql As String = "select * from newRoad where roadname Like '%" & inString & "%'"
            Dim road_DT As DataTable = road_Layer.QueryTools.ExecuteQuery(sSql)
            Dim recIds As Collection(Of String) = New Collection(Of String)
            For Each rows As DataRow In road_DT.Rows
                recIds.Add(rows("RecID"))
            Next

            Dim features As Collection(Of Feature) = road_Layer.FeatureSource.GetFeaturesByIds(recIds, ReturningColumnsType.AllColumns)

            Dim feature As Collection(Of Feature) = road_Layer.QueryTools.GetFeaturesByColumnValue("Roadname", inString)
            ' your data represent road, so I think it's LineShape type, if not you can define another type but the code is similiar.
            Dim lineShape As LineShape = CType(feature(0).GetShape(), LineShape)
            Dim firstVertex As Vertex = lineShape.Vertices(0)
            Dim x As Double = firstVertex.X
            Dim y As Double = firstVertex.Y


Please let me know if you have more questions.
James

Thanks for your help, James 
 But it shows error in follows code 
 Dim lineShape As LineShape = CType(features(0).GetShape(), LineShape) 
  
 it can’t change type of  ‘ThinkGeo.MapSuite.Core.MultilineShape’  to type of  ‘ThinkGeo.MapSuite.Core.LineShape’。 
   
 but, if I change code as follows 
 Dim lineShape As MultilineShape = CType(features(0).GetShape(), MultilineShape)        
 Dim firstVertex As Vertex = lineShape.Vertices(0) 
 the error: Vertices isn’t the member of ‘ThinkGeo.MapSuite.Core.MultilineShape’  
  
 How to get coordinates from MultilineShape ?

Carol, 
  
 MultilineShape is a collection of lineShapes, you can get all lineshapes by Lines property. 
 If you get lineshape you can get all vertices of it. 
  
 By the way, what do you mean of coordinate? In my mind, the coordinate is a point which represent longitude and latitude. How do you think the coordinate of line? 
  
 James

Sorry, James 
 Could you give me a sample to get coordinate of MultilineShape? I still feel confused. :( 
  
 yes, coordintaes means that a point which represent represent longitude and latitude… 
 why I want to get coordinate of line? 
 because i want to search the road is located which county…i have to get coordinate and search by countyLayer. 
  
 line have two points at least (begin and end). so…could i get the coordinates of begin or end? or could i get the coordinates of middle of the line?

Sorry, James 
 Could you give me a sample to get coordinate of MultilineShape? I still feel confused. :( 
  
 yes, coordintaes means that a point which represent represent longitude and latitude… 
 why I want to get coordinate of line? 
 because i want to search the road is located which county…i have to get coordinate and search by countyLayer. 
  
 line have two points at least (begin and end). so…could i get the coordinates of begin or end? or could i get the coordinates of middle of the line?

Carol,


I have written a method to show how to get coordinate of MultilineShape, hope you can clear.


The begin is the first vertex of line, the end is the last vertex of line, the middle is the count / 2 vertex of line.


 


Private Sub ShowCoordinate(ByVal multilineShape As MultilineShape)
    Dim lineShapes As System.Collections.ObjectModel.Collection(Of LineShape) = multilineShape.Lines
    For Each lineShape As LineShape In lineShapes
        Dim verticesCount As Integer = lineShape.Vertices.Count
        Debug.WriteLine(("lineShape: " + lineShape.Id))
        Debug.WriteLine("the coordinates of begin x: " + lineShape.Vertices(0).X)
        Debug.WriteLine("the coordinates of begin y: " + lineShape.Vertices(0).Y)
        Debug.WriteLine("the coordinates of end x: " + lineShape.Vertices(verticesCount - 1).X)
        Debug.WriteLine("the coordinates of end y: " + lineShape.Vertices(verticesCount - 1).Y)
        Debug.WriteLine("the coordinates of middle x: " + lineShape.Vertices(verticesCount / 2).X)
        Debug.WriteLine("the coordinates of middle y: " + lineShape.Vertices(verticesCount / 2).Y)
    Next
End Sub

Please let me know if you have more questions


James