GIS Library for Lazarus lazarus / 21-General ---------------------------------------------------------------------- amir.eng · 2022-01-10 11:55 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. ---------------------------------------------------------------------- MarkMLl · 2022-01-10 12:17 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 ---------------------------------------------------------------------- amir.eng · 2022-01-11 07:56 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. ---------------------------------------------------------------------- stab · 2022-01-12 17:52 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. ---------------------------------------------------------------------- amir.eng · 2022-01-13 14:06 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? ---------------------------------------------------------------------- wp · 2022-01-13 14:58 My 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. ---------------------------------------------------------------------- stab · 2022-01-13 15:08 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 ---------------------------------------------------------------------- stab · 2022-01-13 15:15 Forgot to answer your question: Yes you can draw shapefiles in different colors on a canvas and have access to attributes /stab ---------------------------------------------------------------------- amir.eng · 2022-01-14 22:16 Quote from: stab on January 13, 2022, 04:08:45 pmHi 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. ---------------------------------------------------------------------- stab · 2022-01-15 12:53 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! ---------------------------------------------------------------------- amir.eng · 2022-01-15 20:25 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). ---------------------------------------------------------------------- stab · 2022-01-17 22:36 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 ---------------------------------------------------------------------- amir.eng · 2022-01-18 05:24 Hi Thank you for replying, What is this error while compiling ?? : ---------------------------------------------------------------------- stab · 2022-01-18 11:06 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. ---------------------------------------------------------------------- stab · 2022-01-19 00:07 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 ---------------------------------------------------------------------- amir.eng · 2022-01-19 05:31 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 ? ---------------------------------------------------------------------- zamtmn · 2022-01-19 07:55 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 ---------------------------------------------------------------------- stab · 2022-01-19 23:04 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 ---------------------------------------------------------------------- loaded · 2022-01-20 07:18 Quote from: amir.eng on January 19, 2022, 06:31:42 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) Quote from: stab on January 20, 2022, 12:04:08 amRegarding 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; ---------------------------------------------------------------------- amir.eng · 2022-01-20 10:56 Quote from: stab on January 20, 2022, 12:04:08 amRegarding 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 ?? ---------------------------------------------------------------------- stab · 2022-01-20 11:43 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. ---------------------------------------------------------------------- stab · 2022-01-26 17:48 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. ---------------------------------------------------------------------- winni · 2022-01-26 18:43 @stab Hi! You forgot the dir auxiliary with the file shapeobjbgra.pas  in your zip file. Winni ---------------------------------------------------------------------- stab · 2022-01-26 21:04 Sorry, new zip file included test shapefile ---------------------------------------------------------------------- winni · 2022-01-26 23:04 Hi! First shot: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ShapeObj.pas(5595,58) Error: Call by var for arg no. 5 has to match exactly: Got "Double" expected "Extended"  On Linux64 Winni ---------------------------------------------------------------------- stab · 2022-01-28 13:03 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. ---------------------------------------------------------------------- Mike.Cornflake · 2022-10-08 12:32 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     InitialiseGPS(edtLatitude.Value, edtLongitude.Value);   LatLonToEN(edtLatitude.Value, edtLongitude.Value, dEast{%H-}, dNor{%H-});  ENToLatLon(dEast, dNor, dLat{%H-}, dLon{%H-});   memGPS.Lines.Add('Original Lat:%.6f, Long:%.6f', [edtLatitude.Value, edtLongitude.Value]);  memGPS.Lines.Add('E:%.2f, N:%.2f', [dEast, dNor]);  memGPS.Lines.Add('Converted Lat:%.6f, Long:%.6f', [dLat, dLon]); GPSSupport.pas Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Var  FWGS84: TWGS84;  FFuseau: Integer;  FSouthernHemi: Boolean; Function GetWGS88: TWGS84;Begin  If Not Assigned(FWGS84) Then    FWGS84 := TWGS84.Create;   Result := FWGS84;End;    Procedure InitialiseGPS(ALat, ALon: Double);Begin  FFuseau := floor((ALon + 180) / 6) + 1;  FSouthernHemi := (ALat < 0);End;   Procedure LatLonToEN(ALat, ALon: Double; Var AEast, ANorth: Double);Var  oLatLon: TrecLatLon;  oUTM: TrecUTM;  oWGS84: TWGS84;Begin  oLatLon.Lat := ALat;  oLatLon.Lon := ALon;   oWGS84 := GetWGS88;  oWGS84.WGS84ToUTM(oLatLon, oUTM{%H-});   AEast := oUTM.Y;  ANorth := oUTM.X;End; Procedure ENToLatLon(AEast, ANorth: Double; Var ALat, ALon: Double);Var  oLatLon: TrecLatLon;  oUTM: TrecUTM;  oWGS84: TWGS84;Begin  If FFuseau = -1 Then    Raise Exception.Create('Fuseau (UTM Zone) not defined.  Call InitialiseGPS first.');   oUTM.X := ANorth;  oUTM.Y := AEast;   oUTM.southhemi := FSouthernHemi;  oUTM.fuseau := FFuseau;   oWGS84 := GetWGS88;  oWGS84.UTMToWGS84(oUTM, oLatLon{%H-});   ALat := oLatLon.Lat;  ALon := oLatLon.Lon;End;       Initialization  FWGS84 := nil;  FFuseau := -1;  FSouthernHemi := True; Finalization  FreeAndNil(FWGS84);         And finally, here's the output from the above: QuoteOriginal 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 ---------------------------------------------------------------------- loaded · 2022-10-08 18:41 Quote from: Mike.Cornflake on October 08, 2022, 02:32:49 pmI 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/ ---------------------------------------------------------------------- Mike.Cornflake · 2022-10-08 18:51 Quote from: loaded on October 08, 2022, 08:41:26 pmYou can control the conversions between units from the relevant site. 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   oUTM.X := ANorth;  oUTM.Y := AEast; This didn't Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   oUTM.X := AEast;  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. ---------------------------------------------------------------------- loaded · 2022-10-08 19:54 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i GPS (WGS84) (deg) to GPS (WGS84) (UTM) Lat =-19.809287°N      Lng =114.608388°E Hemisphere =South       Zone =50        X =     249468.543 m    Y =7807850.062 m Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i GPS (WGS84) (UTM)  to GPS (WGS84) (deg) Hemisphere =South       Zone =50        X =249468.543 m         Y =-7807850.062 m Lat =-19.772537982827114°N   Lng =-60.60893696769726°E Quote from: Mike.Cornflake on October 08, 2022, 08:51:43 pmI have no survey training, so this latter is a real possibility. I don't have surveyor training either. ---------------------------------------------------------------------- MarkMLl · 2022-10-08 20:26 Quote from: Mike.Cornflake on October 08, 2022, 08:51:43 pmSo, 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 ---------------------------------------------------------------------- Mike.Cornflake · 2022-10-08 21:21 Quote from: loaded on October 08, 2022, 09:54:04 pmIt 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. ---------------------------------------------------------------------- Mike.Cornflake · 2022-10-10 03:03 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   if UTM.southhemi then UTM.Y:=UTM.Y*-1;   // reversal of UTM-X and UTM-Y to geodetic orientation  // kasta om UTM-X och UTM-Y till geodetisk orientering  t := UTM.X;  UTM.X := UTM.Y;  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. ---------------------------------------------------------------------- maurobio · 2023-05-09 11:35 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/): https://web.archive.org/web/20070403193915/http://www.triplexware.huck finn.de/shpapi.html It is simple and fast. Hope this helps. Best regards,