Lazarus

Programming => General => Topic started by: amir.eng on January 10, 2022, 12:55:48 pm

Title: GIS Library for Lazarus
Post by: amir.eng on January 10, 2022, 12:55:48 pm
Hello guys,
First of all, thanks to everyone who created and is suportting this nice IDE.
I'm really interested to know if it's possible to read/write GIS Shape File via Lazarus ??

Already, I have done with C# and this library :
https://github.com/rosspickard/ArcShapeFile

It's great if I will do it with Lazarus like write Point or Polygon shapefile or read Polygon on form.
Title: Re: GIS Library for Lazarus
Post by: MarkMLl on January 10, 2022, 01:17:04 pm
If you're specifically talking about the files then I think the search term you need is ESRI.

There's various links in https://forum.lazarus.freepascal.org/index.php?topic=39392.0 which might be worth following up.

In general, I'd expect most libraries written in C to be fairly easily accessible.

Also https://sourceforge.net/directory/os:linux/?q=delphi+esri+shape where I see at least one interesting reference to Object Pascal.

MarkMLl
Title: Re: GIS Library for Lazarus
Post by: amir.eng on January 11, 2022, 08:56:42 am
I had seen the topics before I posted. All the topics are old and I couldn't find any useful information. I just wanted to know if someone has ever experienced it in practical.
Title: Re: GIS Library for Lazarus
Post by: stab on January 12, 2022, 06:52:25 pm
Hi,

I have a component TShapeObj that can read/write shapefiles, I can attach it, if you are interested.
As I live in Sweden, comments are moustly in swedish, but I guess you can figure out how to use it.
Maybe there is dependencies that I've not been aware of, but a starting point I think you can use it. :)
Title: Re: GIS Library for Lazarus
Post by: amir.eng on January 13, 2022, 03:06:52 pm
Thank you so much. Yes I'm really interested to have it, please attach the component. I will try to figute out how to use it.

I also want to know, can I show shapefile and the attribute of the shapefile on the form with the component?
Title: Re: GIS Library for Lazarus
Post by: wp on January 13, 2022, 03:58:03 pm
My corona (https://github.com/wp-xyz/corona) application contains a mode in which data are plotted in a map with countries colored according to their infection level. At first, I wanted to read shape files directly, but then I decided against it because of the many interrelated files. I found the kml format to be more appropriate because it has everything in a plain xml structure. For those cases where I did not find kml files I used the QGIS application to convert shp to kml. And, if I remember correctly, this program also helped me to reduce the number of the polygon points of the country boundary down to some decent level (forgot how I did this - this is a non-trivial task...).

For drawing the country shapes I used TAChart. The version in laz/main contains a new TPolygonSeries which was intended to become the ancestor of the country plots. But since I wanted to be able to compile the application also in older Lazarus versions I copied this code into the corona project (unit cPolygonSeries). There is another unit cGeoMap which interfaces to GIS data files, extracts the country boundary polygons and feeds them into a polygon series (a TcGeoMapSeries, to be precise, because it needs some ID to identify the countries). There's also a variety of projections to project the spherical coordinates onto the screen plane (Mercator, Miller, orthographic plus some more). A separate reader unit is specialized to get the data from the kml files (cGeoMapReaderKML). Basically the structure is intended to be independent of kml. So, when you write a reader for SHP according to what the KML reader does the program should be able to display SHP files as well.

The entire code probably is not general enough for all-purpose GIS plotting, but you may get an idea how a standard TChart could be used for this purpose.
Title: Re: GIS Library for Lazarus
Post by: stab on January 13, 2022, 04:08:45 pm
Hi again,

Here it comes together with two other components:
worldviewportroutines - can be used to display world-coordinates in a display
WGS84                      - transformation between WGS84 and coordinates in
                                   national coordinate-system.
                                   constants in the beginning is for the national system
                                   in Sweden, but can be reassigned.
                           
Salutes
stab :)
Title: Re: GIS Library for Lazarus
Post by: stab on January 13, 2022, 04:15:38 pm
Forgot to answer your question:

Yes you can draw shapefiles in different colors on a canvas and have access to attributes

/stab
Title: Re: GIS Library for Lazarus
Post by: amir.eng on January 14, 2022, 11:16:25 pm
Hi again,

Here it comes together with two other components:
worldviewportroutines - can be used to display world-coordinates in a display
WGS84                      - transformation between WGS84 and coordinates in
                                   national coordinate-system.
                                   constants in the beginning is for the national system
                                   in Sweden, but can be reassigned.
                           
Salutes
stab :)

I took a look at the components, ShapeObj component is incredible !!, it also has some Arcmap's tools such as Merge or Split shapefiles , add or delete field or getting centroid of a polygon.
I was also looking for a component to convert UTM coordinates to LatLong, now I have it. Many thanks for your favor.

Could you please give me a simple example to create Poylgon shapefile for these coordinates ?? :

1) x = 1569.84, y = 1430.57
2) x = 1018.21, y = 1103.42
3) x = 958.40, y = 1404

My next question, is it possible to use Select By Attribute with the component similar Arcmap ?? For example I want to use this select : Select * from BuildingShape where village= 'Kayakoy'

* Also thanks to wp for replying. I downloaded Corona software , it's a good idea but I needed all-purpose GIS component.

 
Title: Re: GIS Library for Lazarus
Post by: stab on January 15, 2022, 01:53:24 pm
Hi again,

I'm somewhat busy right now, but I'll be back next weeks with description an how to create, read/write,  select and display shapefiles.
Have written TShapeObj according shapefile format and it has served my purposes well.  It is not optimized and when some new need has showed up it has been expanded. Anyone is welcomed to improve it.
As for your second question: It is possible to make the kind of selection your are asking for!
Title: Re: GIS Library for Lazarus
Post by: amir.eng on January 15, 2022, 09:25:57 pm
Thanks a lot, I'm looking forward to your description or sample Lazarus project.
I hope to add Clip tool to the component. I saw Split function in the component but I don't know if that's the same Clip tool.

Split tool in Arcmap : Exporting shape files based on attribute field.
Clip tool in Arcmap : Exporting shape file based on another shape file (polygon shape).
Title: Re: GIS Library for Lazarus
Post by: stab on January 17, 2022, 11:36:02 pm
Hi,

Here comes a program showing a little about how to
use ShapeObj together with an instruction.

Both will be extended.

Regarding your thoughts about CLIP I would suggest that you look at Angus Johnson's  clipper.pas.
Look at attached unit: ClipperRoutines in which I've used clipper.pas to buffer a shapefile.

Found a bug in ShapeObj. A new version attached

 
Title: Re: GIS Library for Lazarus
Post by: amir.eng on January 18, 2022, 06:24:20 am
Hi
Thank you for replying,
What is this error while compiling ?? :


Title: Re: GIS Library for Lazarus
Post by: stab on January 18, 2022, 12:06:21 pm
Excuse me, FastGeo is a library I've thought to perhaps use sometime.
The reference can be removed, because it is not currently used.

Following is from the description on the net:
FastGEO is a library that contains a wide range of highly optimized computational geometry algorithms and routines for many different types of geometrical operations such as geometrical primitives and predicates, hull construction, triangulation, clipping, rotations and projections.

That could also be something to consider when planning for a CLIP option.
Title: Re: GIS Library for Lazarus
Post by: stab on January 19, 2022, 01:07:33 am
Updated version of ShapeObjDemo with extended instructions.

Look at Split that have 2 variants
  One variant divides a shape with a straight line segment
  Another variant divides a shape with another shape.
  The second variant might correspond to Clip
  Note: not sufficiently tested

When selecting with a string field in attribute table the string
must be surrounded with ".

ShapeObj updated with comments in english
Title: Re: GIS Library for Lazarus
Post by: amir.eng on January 19, 2022, 06:31:42 am
Great, I check it out and also tools.

Few issues:

-Parcels border are not clear when Outline mode is diabled (Please take a look at the attached image, the left shape file is in Arcmap and the right one is the same shape file in the Application)
-Is it possible to add Zoom In and Zoom Out to the main shape view window?
-Is it possible to select feature by clicking on the main shape veiw window and showing only the feature's attributes on the shapetable ?
Title: Re: GIS Library for Lazarus
Post by: zamtmn on January 19, 2022, 08:55:09 am
There is also such an variant, it does not directly supports SHP, but a lot has been done in it for fast processing of vector graphics
Title: Re: GIS Library for Lazarus
Post by: stab on January 20, 2022, 12:04:08 am
Regarding your question:
Is it possible to select feature by clicking on the main shape veiw window and showing only the feature's attributes on the shapetable ?

Should I understand this so:
 you would like that only attributes for selected shape is displayed?

Regarding border not clear:
  Have to look at that little more

Working on zoomin, zoomout and pan
Title: Re: GIS Library for Lazarus
Post by: loaded on January 20, 2022, 08:18:58 am
-Parcels border are not clear when Outline mode is diabled (Please take a look at the attached image, the left shape file is in Arcmap and the right one is the same shape file in the Application)
Regarding border not clear:
  Have to look at that little more

By the way, I can say with the experience I gained from a CAD application that I designed in Android Studio, the backbone of which I wrote with Lazarus+LAMW;
Defining the brush and pen colors separately will solve the problem.

Related application usage test video;  (https://youtu.be/gBS0AMrJ50s)
Title: Re: GIS Library for Lazarus
Post by: amir.eng on January 20, 2022, 11:56:55 am
Regarding your question:
Is it possible to select feature by clicking on the main shape veiw window and showing only the feature's attributes on the shapetable ?

Should I understand this so:
 you would like that only attributes for selected shape is displayed?

Regarding border not clear:
  Have to look at that little more

Working on zoomin, zoomout and pan
Yes I mean the table shows only attribute for selected shape like Arcmap.
Could you please add zoomin, zoomout,pad, selecting feature by mouse ??
Title: Re: GIS Library for Lazarus
Post by: stab on January 20, 2022, 12:43:05 pm
Regarding selecting feature by mouse, you have it already, just check HitTest and click in a shape. In the version you have right now, all features will show up, but current attribute is marked. I've a new version where just attributes for the selected is shown.
Working on  zoomin, zoomout,pan and your problem with somewhat jagged borders.
Title: Re: GIS Library for Lazarus
Post by: stab on January 26, 2022, 06:48:04 pm
Have been testing quite a bit to achieve fast drawing, am not satisfied but
think someone smarter than me have to fix that.

You now have zoom, pan and antialiasing.

Included in the zip file is a new component ShapeObBGRA that draws on a
TBGRAGraphicControl that handles antialiasing and removes jagged borders.

If you or someone else improves the component I'd be glad to have a copy.
Title: Re: GIS Library for Lazarus
Post by: winni on January 26, 2022, 07:43:20 pm
@stab

Hi!

You forgot the dir auxiliary with the file shapeobjbgra.pas  in your zip file.

Winni
Title: Re: GIS Library for Lazarus
Post by: stab on January 26, 2022, 10:04:23 pm
Sorry, new zip file included test shapefile :o
Title: Re: GIS Library for Lazarus
Post by: winni on January 27, 2022, 12:04:00 am
Hi!

First shot:

Code: Text  [Select][+][-]
  1. ShapeObj.pas(5595,58) Error: Call by var for arg no. 5 has to match exactly: Got "Double" expected "Extended"
  2.  

On Linux64

Winni
Title: Re: GIS Library for Lazarus
Post by: stab on January 28, 2022, 02:03:05 pm
As I wrote earlier:
Have written TShapeObj according the shapefile format description and it has served my purposes well. It is not optimized and when some new need has showed up it has been expanded.
Anyone is welcome to improve it. If so, I'm interested in getting a copy.
Title: Re: GIS Library for Lazarus
Post by: Mike.Cornflake on October 08, 2022, 02:32:49 pm
G'day,

Sorry for bumping an old post, but this seems the most appropriate location.

Many thanks for this library.  Honestly, I couldn't have solved some issues on this job without it.  My use is with the WGS84 unit.  I'm not a surveyor, but in my industry we use Eastings, Northings extensively.  I've been tasked with sending a device a GPS signal, so I needed to convert from EN to LatLon (cause I only know GPS commands that contain Lat Lon).

I've got everything working, and the conversion in WGS84 checks out against empirical data.  So chances are, my question here is about convention.

I've noticed two oddities when using WGS84.pas.   
1.  In the comments for WGS84.pas, UTM.X is Easting, and UTM.Y is northing.  And that makes sense.  In my code though, I only get everything working if I use UTM.Y as Easting and UTM.X and northing.

2.  Northing is coming out with a negative sign.  Sure, I'm in the southern hemisphere, so this kind of makes sense.  Except I've never seen negative northings in use in any field (I work in subsea oil and gas).  And there's already a UTM.southernhemi boolean which I thought would take care of this.

My question is: Am I using WGS84.pas incorrectly, and I misunderstanding survey coordinates?, or is there an issue in WGS84.pas?

In the test code that follows, I ask the user to enter a Lat Lon.  This is then used to determine the UTM Zone (UTM.Fuseau) and determine if we're in the SouthernHemisphere.
I then convert this LAt Lon to Easting, Northing, and display that in my UI
I then convert back to Lat Lon (as a test) and display that.

I have a support unit (GSPSupport.pas) really so I can simplify code elsewhere by using a singleton for TWGS84.

Here's relevant snippets of my code:

MainForm
Code: Pascal  [Select][+][-]
  1.  
  2.   InitialiseGPS(edtLatitude.Value, edtLongitude.Value);
  3.  
  4.   LatLonToEN(edtLatitude.Value, edtLongitude.Value, dEast{%H-}, dNor{%H-});
  5.   ENToLatLon(dEast, dNor, dLat{%H-}, dLon{%H-});
  6.  
  7.   memGPS.Lines.Add('Original Lat:%.6f, Long:%.6f', [edtLatitude.Value, edtLongitude.Value]);
  8.   memGPS.Lines.Add('E:%.2f, N:%.2f', [dEast, dNor]);
  9.   memGPS.Lines.Add('Converted Lat:%.6f, Long:%.6f', [dLat, dLon]);  
  10.  

GPSSupport.pas
Code: Pascal  [Select][+][-]
  1. Var
  2.   FWGS84: TWGS84;
  3.   FFuseau: Integer;
  4.   FSouthernHemi: Boolean;
  5.  
  6. Function GetWGS88: TWGS84;
  7. Begin
  8.   If Not Assigned(FWGS84) Then
  9.     FWGS84 := TWGS84.Create;
  10.  
  11.   Result := FWGS84;
  12. End;  
  13.  
  14. Procedure InitialiseGPS(ALat, ALon: Double);
  15. Begin
  16.   FFuseau := floor((ALon + 180) / 6) + 1;
  17.   FSouthernHemi := (ALat < 0);
  18. End;  
  19.  
  20. Procedure LatLonToEN(ALat, ALon: Double; Var AEast, ANorth: Double);
  21. Var
  22.   oLatLon: TrecLatLon;
  23.   oUTM: TrecUTM;
  24.   oWGS84: TWGS84;
  25. Begin
  26.   oLatLon.Lat := ALat;
  27.   oLatLon.Lon := ALon;
  28.  
  29.   oWGS84 := GetWGS88;
  30.   oWGS84.WGS84ToUTM(oLatLon, oUTM{%H-});
  31.  
  32.   AEast := oUTM.Y;
  33.   ANorth := oUTM.X;
  34. End;
  35.  
  36. Procedure ENToLatLon(AEast, ANorth: Double; Var ALat, ALon: Double);
  37. Var
  38.   oLatLon: TrecLatLon;
  39.   oUTM: TrecUTM;
  40.   oWGS84: TWGS84;
  41. Begin
  42.   If FFuseau = -1 Then
  43.     Raise Exception.Create('Fuseau (UTM Zone) not defined.  Call InitialiseGPS first.');
  44.  
  45.   oUTM.X := ANorth;
  46.   oUTM.Y := AEast;
  47.  
  48.   oUTM.southhemi := FSouthernHemi;
  49.   oUTM.fuseau := FFuseau;
  50.  
  51.   oWGS84 := GetWGS88;
  52.   oWGS84.UTMToWGS84(oUTM, oLatLon{%H-});
  53.  
  54.   ALat := oLatLon.Lat;
  55.   ALon := oLatLon.Lon;
  56. End;      
  57.  
  58. Initialization
  59.   FWGS84 := nil;
  60.   FFuseau := -1;
  61.   FSouthernHemi := True;
  62.  
  63. Finalization
  64.   FreeAndNil(FWGS84);        

And finally, here's the output from the above:
Quote
Original Lat:-19.809287, Long:114.608388
E:249468.54, N:-7807850.06
Converted Lat:-19.809287, Long:114.608388

I haven't checked if the Easting/Northing is mm accurate, I only need accuracy to metre.   Other than the minus sign, the E N checks out.
As you'll see, in Procedures ENToLatLon & LatLontoEN I had to switch X & Y around for Easting & Northing
Title: Re: GIS Library for Lazarus
Post by: loaded on October 08, 2022, 08:41:26 pm
I haven't checked if the Easting/Northing is mm accurate, I only need accuracy to metre.   Other than the minus sign, the E N checks out.
As you'll see, in Procedures ENToLatLon & LatLontoEN I had to switch X & Y around for Easting & Northing

You can control the conversions between units from the relevant site.
https://twcc.fr/ (https://twcc.fr/)
Title: Re: GIS Library for Lazarus
Post by: Mike.Cornflake on October 08, 2022, 08:51:43 pm
You can control the conversions between units from the relevant site.
https://twcc.fr/ (https://twcc.fr/)

Thanks.  But that appears to be a web based solution.  I've already confirmed the coordinates are correct. 

My issue is that in order to generate the correct coordinates, I had to cross the use of X and Y when passing to WGS84.pas.

This worked
Code: Pascal  [Select][+][-]
  1.   oUTM.X := ANorth;
  2.   oUTM.Y := AEast;

This didn't
Code: Pascal  [Select][+][-]
  1.   oUTM.X := AEast;
  2.   oUTM.Y := ANorth;

So, is this an issue with WGS84.pas, or am I misunderstanding something about GIS coordinates?  I have no survey training, so this latter is a real possibility.
Title: Re: GIS Library for Lazarus
Post by: loaded on October 08, 2022, 09:54:04 pm
It seems to me that it is necessary to check the formulas for the use of x and y.
The related site is using proj4 for conversion. This is what the conversion values look like.
It seems like there is some difference.

Code: Pascal  [Select][+][-]
  1. GPS (WGS84) (deg) to GPS (WGS84) (UTM)
  2. Lat =-19.809287°N      Lng =114.608388°E
  3. Hemisphere =South       Zone =50        X =     249468.543 m    Y =7807850.062 m
  4.  

Code: Pascal  [Select][+][-]
  1. GPS (WGS84) (UTM)  to GPS (WGS84) (deg)
  2. Hemisphere =South       Zone =50        X =249468.543 m         Y =-7807850.062 m
  3. Lat =-19.772537982827114°N     Lng =-60.60893696769726°E

I have no survey training, so this latter is a real possibility.

I don't have surveyor training either.
Title: Re: GIS Library for Lazarus
Post by: MarkMLl on October 08, 2022, 10:26:48 pm
So, is this an issue with WGS84.pas, or am I misunderstanding something about GIS coordinates?  I have no survey training, so this latter is a real possibility.

In the interest of resolving this relatively quickly, I would suggest that a question on gis.stackexchange.com phrased like "I am using a software library which appears to use this sign convention: is this compliant with WGS84?" is in order.

MarkMLl
Title: Re: GIS Library for Lazarus
Post by: Mike.Cornflake on October 08, 2022, 11:21:27 pm
It seems to me that it is necessary to check the formulas for the use of x and y.

Nice.  I see what you're saying now.  Thanks

OK, I'll start trying to debug WGS84.pas.
Title: Re: GIS Library for Lazarus
Post by: Mike.Cornflake on October 10, 2022, 05:03:22 am
Got to the bottom of the oddities I saw.  Don't understand the why, but it's deliberate code in two spots.  Here's one.

Code: Pascal  [Select][+][-]
  1.   if UTM.southhemi then UTM.Y:=UTM.Y*-1;
  2.  
  3.   // reversal of UTM-X and UTM-Y to geodetic orientation
  4.   // kasta om UTM-X och UTM-Y till geodetisk orientering
  5.   t := UTM.X;
  6.   UTM.X := UTM.Y;
  7.   UTM.Y := t;

Original comment is in swedish.  I've used Google Translate to convert the commments.  Based on that I've added a property: TWGS88.IgnoreGeodeticOrientation.  It's false by default, which leaves the existing code to run.  Setting it True though stops the above conversion.   All values are now as I expect them.
Title: Re: GIS Library for Lazarus
Post by: maurobio on May 09, 2023, 01:35:03 pm
Hi,

I don't know if this is still of interest, but I have successfully used in a Lazarus/FPC application the library below (derived from Shapefile C Library, http://shapelib.maptools.org/ (http://shapelib.maptools.org/)):

https://web.archive.org/web/20070403193915/http://www.triplexware.huckfinn.de/shpapi.html (https://web.archive.org/web/20070403193915/http://www.triplexware.huckfinn.de/shpapi.html)

It is simple and fast.

Hope this helps.

Best regards,
TinyPortal © 2005-2018