Accessing attributes for polygons imported from GeoJSON

Is it possible to access one or more attribute fields associated with polygons brought in to FNCMaps via a GeoJSON file? Ideally, I would like to be able to click on a polygon and be ablet bring up information for the chosen polygon. I currently use OpenStreetMap and Leaflet.

Alternatively, as a temporary work-around, can one create FNCMap labels ftom a GeoJSON file? This would not be ideal because I would have to calculate centroids for polygons and these are not always located within polygons for unusually shaped polygons on maps.

1 Like

Hi,

Currently only the Name attribute is exposed when loading GeoJSON files in TTMSFNCMaps.
Other attributes are currently not extracted. However this is a good suggestion and we'll investigate if this functionality can be added in a future version.

Example to retrieve the Name attribute of the first Polygon object in the GeoJSON file.

procedure TForm1.Button1Click(Sender: TObject);
var
  geo: TTMSFNCMapsGeoJSONRec;
begin
  geo := TMSFNCMaps1.LoadGeoJSONFromFile('samplefile.geojson', true, true);
  if Length(geo.Polygons) > 0 then
    TTMSFNCUtils.Log(geo.Polygons[0].Name);
end;

Hi Bart
thanks. I assume this expects that one has a field titled "Name" in the GeoJSON file, which I don't. My name field is called something else. Beyond this limitation, how does one identify which specific polygon in a long list of polygons the users has actually clicked on?

Thanks

Hi Bruce,

  • The previous example indeed expects that the GeoJSON file uses a field titled Name.

  • The AEventData.ID parameter of the OnPolyElementClick event contains the ID of the clicked polygon. You can find the polygon object by using the Polygons.ItemByID[AEventData.ID] call.

Example to change the stroke color of the clicked polygon:

procedure TForm1.TMSFNCMaps1PolyElementClick(Sender: TObject;
  AEventData: TTMSFNCMapsEventData);
begin
  TMSFNCMaps1.Polygons.ItemByID[AEventData.ID].StrokeColor := gcRed;
end;

I havew modified the GeoJSON file so that it contains a field called "Name" and load the GeoJSON into TMSFNCMAPS and a TTMSFNCMapsGeoJSONRec as you suggested in an earlier response. I have a TMSFNCMaps1PolyElementClick event and have confirmed that the event links to the correct GeoJSON dataset by changing the colour of the polygon poundary as you specified. I derive the polygon ID with the statement
geo_id := TMSFNCMaps1.Polygons.ItemByID[AEventData.ID];
where geo_id is defined as of type TTMSFNCMapsPolyElement

Then I try to get and show the "name" field contebt as follows
tmpStr := geo.Polygons[geo_id.Index].Name;

but this provides a blank string.

Do I somehow need to fill the "name" field in the record when loading the GeoJSON file. If so, how?

Thanks

This is the expected format of the GeoJSON file:

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [125.6, 10.1]
  },
  "properties": {
    "name": "Dinagat Islands"
  }
}

PlatePolygons_Continental_2025.txt (2.2 KB)
Bart, I have uploaded the first part of mt GeoJSON file as atxt file. The structure is very similar to what you indicate is necessary but there are some differences and I am not sure whether they influence the reading for asigning the value in the "Name" field. Please could you comment.

If the GeoJSON layout is okay, then I must be doing something wrong when loading the file. Beyond a straight GeoJSONLoadFromFile statement, is there anything else that needs to be done (other than assigning to a JSONrec)? My previous email indicated the process I have followed in terms of Delphi code.

Thanks

Bart
I think that I have found out why it would not work for me, after looking through the FNCMaps source code. The "name" field is case sensitive and I had it as "Name" instead of all lowercase.