Blog
All Blog Posts | Next Post | Previous PostDetect if a point is in a coordinate based polygon in Delphi
Wednesday, March 23, 2022
In this blog post, we explained how to measure the area of a polygon defined by coordinates. TMS FNC Maps v3.0.3.0 adds a way to detect a point (coordinate) inside a polygon. In this short blog post, we want to show you how to work with this new functionality.
Load GeoJSON
First of all, we are going to load our GeoJSON file with coordinates that define the surface area of Germany.
TMSFNCMaps1.BeginUpdate; TMSFNCMaps1.LoadGeoJSONFromFile('germany.geojson'); TMSFNCMaps1.EndUpdate;
Then, we style the polygons loaded by the LoadGeoJSONFromFile method. The complete code to load and style polygons is demonstrated below.
procedure LoadGeoJSON; var p: TTMSFNCMapsPolygon; I: Integer; begin TMSFNCMaps1.BeginUpdate; TMSFNCMaps1.LoadGeoJSONFromFile('germany.geojson'); for I := 0 to TMSFNCMaps1.Polygons.Count - 1 do begin p := TMSFNCMaps1.Polygons[I]; p.FillOpacity := 0.4; p.FillColor := gcGreenyellow; p.StrokeOpacity := 0.4; p.StrokeColor := gcGreen; end; TMSFNCMaps1.EndUpdate; end;
Detecting a coordinate inside a polygon
To detect whether or not a coordinate lies inside a polygon, the polygon collection item class exposes a ContainsPoint function. For the purpose of this sample, we used the non visual TTMSFNCGeocoding component and retrieved the coordinates for cities that lie inside and outside of Germany.
procedure DetectPoints; var c1, c2, c3, c4: TTMSFNCMapsCoordinateRec; const v = '<span style="color:green">Coordinate inside polygon</span>'; n = '<span style="color:red">Coordinate outside polygon</span>'; begin c1 := TMSFNCGeocoding1.GetGeocodingSync('Groningen'); c2 := TMSFNCGeocoding1.GetGeocodingSync('Dresden'); c3 := TMSFNCGeocoding1.GetGeocodingSync('Munich'); c4 := TMSFNCGeocoding1.GetGeocodingSync('Augustenborg'); if TMSFNCMaps1.Polygons[0].ContainsPoint(c1) then TMSFNCMaps1.ShowPopup(c1, v) else TMSFNCMaps1.ShowPopup(c1, n); if TMSFNCMaps1.Polygons[0].ContainsPoint(c2) then TMSFNCMaps1.ShowPopup(c2, v) else TMSFNCMaps1.ShowPopup(c2, n); if TMSFNCMaps1.Polygons[0].ContainsPoint(c3) then TMSFNCMaps1.ShowPopup(c3, v) else TMSFNCMaps1.ShowPopup(c3, n); if TMSFNCMaps1.Polygons[0].ContainsPoint(c4) then TMSFNCMaps1.ShowPopup(c4, v) else TMSFNCMaps1.ShowPopup(c4, n); end;
Alternative detection
The ContainsPoint function at polygon collection item level internally calls the IsPointInArea, which is defined in the *.TMSFNCMapsCommonTypes.pas unit. To use this function, you need to define an array of coordinates. This function can be used without the need to define a polygon inside the Polygons collection.
function IsPointInArea(ALatitude: Double; ALongitude: Double; ACoordinatesArray: TTMSFNCMapsCoordinateRecArray): Boolean;
Explore!
Go ahead and download TMS FNC Maps. Explore the features and demos and feel free to leave a comment on this blog.
Pieter Scheldeman
This blog post has not received any comments yet.
All Blog Posts | Next Post | Previous Post