From 864d1e33f5dd717642801d300d408669a04ff9a4 Mon Sep 17 00:00:00 2001 From: djes Date: Mon, 11 Jun 2018 23:23:48 +0200 Subject: [PATCH 01/14] Optimisation Little optimisation by Michael Vogel, see https://www.purebasic.fr/english/viewtopic.php?p=523247#p523247 --- PBMap.pb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index fb1b6a9..98019a7 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -515,7 +515,7 @@ Module PBMap Protected *PBMap.PBMap = PBMaps(Str(MapGadget)) Protected n.d = *PBMap\TileSize * Pow(2.0, Zoom) ; Ensures the longitude to be in the range [-180; 180[ - *Location\Longitude = Mod(Mod(*Coords\x / n * 360.0, 360.0) + 360.0, 360.0) - 180 + *Location\Longitude = Mod((1 + *Coords\x / n) * 360, 360) - 180 ; Mod(Mod(*Coords\x / n * 360.0, 360.0) + 360.0, 360.0) - 180 *Location\Latitude = Degree(ATan(SinH(#PI * (1.0 - 2.0 * *Coords\y / n)))) If *Location\Latitude <= -89 *Location\Latitude = -89 @@ -3141,9 +3141,9 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf -; IDE Options = PureBasic 5.61 (Windows - x64) -; CursorPosition = 2751 -; FirstLine = 2744 +; IDE Options = PureBasic 5.61 (Windows - x86) +; CursorPosition = 517 +; FirstLine = 513 ; Folding = --------------------- ; EnableThread ; EnableXP From 79707581148dfe0838e99d29808a603abe9f7eeb Mon Sep 17 00:00:00 2001 From: djes Date: Mon, 11 Jun 2018 23:36:47 +0200 Subject: [PATCH 02/14] UTF8 source --- PBMap.pb | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 3800a50..43b94ee 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -1,4 +1,4 @@ -; ******************************************************************** +; ******************************************************************** ; Program: PBMap ; Description: Permits the use of tiled maps like ; OpenStreetMap in a handy PureBASIC module @@ -2543,14 +2543,13 @@ Module PBMap EndIf Next EndIf - ; YA pour sélectionner un point de la trace avec le clic gauche If *PBMap\EditMarker = #False Location\Latitude = GetMouseLatitude(MapGadget) Location\Longitude = GetMouseLongitude(MapGadget) If *PBMap\CallBackLeftClic > 0 CallFunctionFast(*PBMap\CallBackLeftClic, @Location) EndIf - ; ajout YA // change la forme du pointeur de souris pour les déplacements de la carte + ; ajout YA // Mouse pointer when moving map SetGadgetAttribute(*PBMap\Gadget, #PB_Canvas_Cursor, #PB_Cursor_Hand) Else SetGadgetAttribute(*PBMap\Gadget, #PB_Canvas_Cursor, #PB_Cursor_Default) ; ajout YA pour remettre le pointeur souris en normal @@ -2608,7 +2607,7 @@ Module PBMap EndIf Next ; Check if mouse touch tracks - If *PBMap\Options\ShowTrackSelection ; YA ajout pour éviter la sélection de la trace + If *PBMap\Options\ShowTrackSelection ; YA to avoid selecting track With *PBMap\TracksList() ; Trace Track If ListSize(*PBMap\TracksList()) > 0 @@ -2642,11 +2641,11 @@ Module PBMap EndIf EndIf Case #PB_EventType_LeftButtonUp - SetGadgetAttribute(*PBMap\Gadget,#PB_Canvas_Cursor,#PB_Cursor_Default) ; ajout YA pour remettre le pointeur souris en normal + SetGadgetAttribute(*PBMap\Gadget,#PB_Canvas_Cursor,#PB_Cursor_Default) ; YA normal mouse pointer ; *PBMap\MoveStartingPoint\x = - 1 *PBMap\Dragging = #False *PBMap\Redraw = #True - ;YA pour connaitre les coordonnées d'un marqueur après déplacement + ;YA to knows marker coordinates after moving ForEach *PBMap\Markers() If *PBMap\Markers()\Selected = #True If *PBMap\CallBackMarker > 0 @@ -3141,11 +3140,11 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf -; IDE Options = PureBasic 5.61 (Windows - x86) -; CursorPosition = 517 -; FirstLine = 513 +; IDE Options = PureBasic 5.61 (Windows - x64) +; CursorPosition = 2648 +; FirstLine = 2606 ; Folding = --------------------- ; EnableThread ; EnableXP ; CompileSourceDirectory -; DisablePurifier = 1,1,1,1 +; DisablePurifier = 1,1,1,1 \ No newline at end of file From 7b17b7166e7e5411edf5401dac71d70191f44d80 Mon Sep 17 00:00:00 2001 From: djes Date: Mon, 11 Jun 2018 23:39:02 +0200 Subject: [PATCH 03/14] UTF8 --- PBMap.pb | 1 + 1 file changed, 1 insertion(+) diff --git a/PBMap.pb b/PBMap.pb index 43b94ee..f0be39b 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -2543,6 +2543,7 @@ Module PBMap EndIf Next EndIf + ; YA To select a track with LMB If *PBMap\EditMarker = #False Location\Latitude = GetMouseLatitude(MapGadget) Location\Longitude = GetMouseLongitude(MapGadget) From b09559c5ee7ef8cdc41ce1e367482e9ac425353f Mon Sep 17 00:00:00 2001 From: djes Date: Mon, 3 Sep 2018 14:46:24 +0200 Subject: [PATCH 04/14] Pixel2Lon()/Pixel2Lat() correction and GetCanvasPixelLon()/Lat() functions --- PBMap.pb | 77 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index f0be39b..82a68e3 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -511,20 +511,6 @@ Module PBMap EndIf EndProcedure - Procedure Pixel2LatLon(MapGadget.i, *Coords.PixelCoordinates, *Location.GeographicCoordinates, Zoom) - Protected *PBMap.PBMap = PBMaps(Str(MapGadget)) - Protected n.d = *PBMap\TileSize * Pow(2.0, Zoom) - ; Ensures the longitude to be in the range [-180; 180[ - *Location\Longitude = Mod((1 + *Coords\x / n) * 360, 360) - 180 ; Mod(Mod(*Coords\x / n * 360.0, 360.0) + 360.0, 360.0) - 180 - *Location\Latitude = Degree(ATan(SinH(#PI * (1.0 - 2.0 * *Coords\y / n)))) - If *Location\Latitude <= -89 - *Location\Latitude = -89 - EndIf - If *Location\Latitude >= 89 - *Location\Latitude = 89 - EndIf - EndProcedure - ; Ensures the longitude to be in the range [-180; 180[ Procedure.d ClipLongitude(Longitude.d) ProcedureReturn Mod(Mod(Longitude + 180, 360.0) + 360.0, 360.0) - 180 @@ -565,19 +551,37 @@ Module PBMap *Pixel\y = *PBMap\Drawing\RadiusY + (py - *PBMap\PixelCoordinates\y) EndProcedure + Procedure Pixel2LatLon(MapGadget.i, *Coords.PixelCoordinates, *Location.GeographicCoordinates, Zoom) + Protected *PBMap.PBMap = PBMaps(Str(MapGadget)) + Protected n.d = *PBMap\TileSize * Pow(2.0, Zoom) + ; Ensures the longitude to be in the range [-180; 180[ + *Location\Longitude = Mod((1 + *Coords\x / n) * 360, 360) - 180 ; Mod(Mod(*Coords\x / n * 360.0, 360.0) + 360.0, 360.0) - 180 + *Location\Latitude = Degree(ATan(SinH(#PI * (1.0 - 2.0 * *Coords\y / n)))) + If *Location\Latitude <= -89 + *Location\Latitude = -89 + EndIf + If *Location\Latitude >= 89 + *Location\Latitude = 89 + EndIf + EndProcedure + Procedure.d Pixel2Lon(MapGadget.i, x) Protected *PBMap.PBMap = PBMaps(Str(MapGadget)) - Protected NewX.d = (*PBMap\PixelCoordinates\x - *PBMap\Drawing\RadiusX + x) / *PBMap\TileSize - Protected n.d = Pow(2.0, *PBMap\Zoom) - ; double mod is to ensure the longitude to be in the range [-180; 180[ - ProcedureReturn Mod(Mod(NewX / n * 360.0, 360.0) + 360.0, 360.0) - 180 + Protected n.d = *PBMap\TileSize * Pow(2.0, *PBMap\Zoom) + ProcedureReturn Mod((1 + x / n) * 360.0, 360.0) - 180 EndProcedure Procedure.d Pixel2Lat(MapGadget.i, y) Protected *PBMap.PBMap = PBMaps(Str(MapGadget)) - Protected NewY.d = (*PBMap\PixelCoordinates\y - *PBMap\Drawing\RadiusY + y) / *PBMap\TileSize - Protected n.d = Pow(2.0, *PBMap\Zoom) - ProcedureReturn Degree(ATan(SinH(#PI * (1.0 - 2.0 * NewY / n)))) + Protected n.d = *PBMap\TileSize * Pow(2.0, *PBMap\Zoom) + Protected latitude.d = Degree(ATan(SinH(#PI * (1.0 - 2.0 * y / n)))) + If latitude <= -89 + latitude = -89 + EndIf + If latitude >= 89 + latitude = 89 + EndIf + ProcedureReturn latitude EndProcedure ; HaversineAlgorithm @@ -2079,20 +2083,30 @@ Module PBMap ;-*** Misc functions - Procedure.d GetMouseLongitude(MapGadget.i) + Procedure.d GetCanvasPixelLon(MapGadget.i, x) Protected *PBMap.PBMap = PBMaps(Str(MapGadget)) - Protected MouseX.d = (*PBMap\PixelCoordinates\x - *PBMap\Drawing\RadiusX + GetGadgetAttribute(*PBMap\Gadget, #PB_Canvas_MouseX)) / *PBMap\TileSize + Protected MouseX.d = (*PBMap\PixelCoordinates\x - *PBMap\Drawing\RadiusX + x) / *PBMap\TileSize Protected n.d = Pow(2.0, *PBMap\Zoom) ; double mod is to ensure the longitude to be in the range [-180; 180[ ProcedureReturn Mod(Mod(MouseX / n * 360.0, 360.0) + 360.0, 360.0) - 180 EndProcedure - Procedure.d GetMouseLatitude(MapGadget.i) + Procedure.d GetCanvasPixelLat(MapGadget.i, y) Protected *PBMap.PBMap = PBMaps(Str(MapGadget)) - Protected MouseY.d = (*PBMap\PixelCoordinates\y - *PBMap\Drawing\RadiusY + GetGadgetAttribute(*PBMap\Gadget, #PB_Canvas_MouseY)) / *PBMap\TileSize + Protected MouseY.d = (*PBMap\PixelCoordinates\y - *PBMap\Drawing\RadiusY + y) / *PBMap\TileSize Protected n.d = Pow(2.0, *PBMap\Zoom) ProcedureReturn Degree(ATan(SinH(#PI * (1.0 - 2.0 * MouseY / n)))) EndProcedure + + Procedure.d GetMouseLongitude(MapGadget.i) + Protected *PBMap.PBMap = PBMaps(Str(MapGadget)) + ProcedureReturn GetCanvasPixelLon(MapGadget.i, GetGadgetAttribute(*PBMap\Gadget, #PB_Canvas_MouseX)) + EndProcedure + + Procedure.d GetMouseLatitude(MapGadget.i) + Protected *PBMap.PBMap = PBMaps(Str(MapGadget)) + ProcedureReturn GetCanvasPixelLat(MapGadget.i, GetGadgetAttribute(*PBMap\Gadget, #PB_Canvas_MouseY)) + EndProcedure Procedure SetLocation(MapGadget.i, latitude.d, longitude.d, Zoom = -1, Mode.i = #PB_Absolute) Protected *PBMap.PBMap = PBMaps(Str(MapGadget)) @@ -2510,7 +2524,12 @@ Module PBMap Else ; Absolute zoom (centered on the center of the map) SetZoom(MapGadget, GetGadgetAttribute(*PBMap\Gadget, #PB_Canvas_WheelDelta), #PB_Relative) - EndIf + EndIf +; Case #PB_EventType_RightClick +; Debug GetMouseLongitude(MapGadget) +; Debug GetMouseLatitude(MapGadget) +; Debug GetCanvasPixelLon(MapGadget, CanvasMouseX + *PBMap\Drawing\RadiusX) +; Debug GetCanvasPixelLat(MapGadget, CanvasMouseY + *PBMap\Drawing\RadiusY) Case #PB_EventType_LeftButtonDown ; LatLon2Pixel(@*PBMap\GeographicCoordinates, @*PBMap\PixelCoordinates, *PBMap\Zoom) *PBMap\Dragging = #True @@ -3141,9 +3160,9 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf -; IDE Options = PureBasic 5.61 (Windows - x64) -; CursorPosition = 2648 -; FirstLine = 2606 +; IDE Options = PureBasic 5.62 (Windows - x64) +; CursorPosition = 2105 +; FirstLine = 2084 ; Folding = --------------------- ; EnableThread ; EnableXP From 55930e68b15d6f9ef0ee0e79abb27404d746a615 Mon Sep 17 00:00:00 2001 From: djes Date: Thu, 18 Jul 2019 15:04:43 +0200 Subject: [PATCH 05/14] Bug tracking Some bugs removed and OnError() coded added (thanks to falsam) Spotted png image library bug, reported on PB forum. --- PBMap.pb | 71 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index f0be39b..66e6fda 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -1,18 +1,41 @@ -; ******************************************************************** +; ******************************************************************** ; Program: PBMap ; Description: Permits the use of tiled maps like ; OpenStreetMap in a handy PureBASIC module ; Author: Thyphoon, djes, Idle, yves86 -; Date: June, 2018 +; Date: July, 2019 ; License: PBMap : Free, unrestricted, credit ; appreciated but not required. ; OSM : see http://www.openstreetmap.org/copyright ; Note: Please share improvement ! -; Thanks: Progi1984 +; Thanks: Progi1984, falsam +; HowToRun: Just compile this code, example is included +; ******************************************************************** +; +; Track bugs with the following options with debugger enabled (see in the example) +; PBMap::SetOption(#Map, "ShowDebugInfos", "1") +; PBMap::SetDebugLevel(5) +; PBMap::SetOption(#Map, "Verbose", "1") +; +; or with the OnError() PB capabilities : +; +; CompilerIf #PB_Compiler_LineNumbering = #False +; MessageRequester("Warning !", "You must enable 'OnError lines support' in compiler options", #PB_MessageRequester_Ok ) +; End +; CompilerEndIf +; +; Declare ErrorHandler() +; +; OnErrorCall(@ErrorHandler()) +; +; Procedure ErrorHandler() +; MessageRequester("Ooops", "The following error happened : " + ErrorMessage(ErrorCode()) + #CRLF$ +"line : " + Str(ErrorLine())) +; EndProcedure +; ; ******************************************************************** CompilerIf #PB_Compiler_Thread = #False - MessageRequester("Warning !", "You must enable ThreadSafe support in compiler options", #PB_MessageRequester_Ok ) + MessageRequester("Warning !", "You must enable 'Create ThreadSafe Executable' in compiler options", #PB_MessageRequester_Ok ) End CompilerEndIf @@ -29,7 +52,7 @@ UseJPEGImageEncoder() DeclareModule PBMap #PBMAPNAME = "PBMap" - #PBMAPVERSION = "0.9" + #PBMAPVERSION = "0.91" #USERAGENT = #PBMAPNAME + "/" + #PBMAPVERSION + " (https://github.com/djes/PBMap)" CompilerIf #PB_Compiler_OS = #PB_OS_Linux @@ -57,10 +80,10 @@ DeclareModule PBMap EndStructure Structure Marker - GeographicCoordinates.GeographicCoordinates ; Marker latitude and longitude + GeographicCoordinates.GeographicCoordinates ; Marker's latitude and longitude Identifier.s Legend.s - Color.l ; Marker color + Color.l ; Marker's color Focus.i Selected.i ; Is the marker selected ? CallBackPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) @@ -1220,16 +1243,16 @@ Module PBMap Repeat Progress = HTTPProgress(*Tile\Download) Select Progress - Case #PB_Http_Success + Case #PB_HTTP_Success *Tile\Size = FinishHTTP(*Tile\Download) ; \Size signals that the download is OK ;MyDebug(*PBMap, " Thread nb " + Str(*Tile\GetImageThread) + " " + *Tile\key + " for image " + *Tile\CacheFile + " finished. Size : " + Str(*Tile\Size), 5) Quit = #True - Case #PB_Http_Failed + Case #PB_HTTP_Failed FinishHTTP(*Tile\Download) *Tile\Size = 0 ; \Size = 0 signals that the download has failed ;MyDebug(*PBMap, " Thread nb " + Str(*Tile\GetImageThread) + " " + *Tile\key + " for image " + *Tile\CacheFile + " failed.", 5) Quit = #True - Case #PB_Http_Aborted + Case #PB_HTTP_Aborted FinishHTTP(*Tile\Download) *Tile\Size = 0 ; \Size = 0 signals that the download has failed ;MyDebug(*PBMap, " Thread nb " + Str(*Tile\GetImageThread) + " " + *Tile\key + " for image " + *Tile\CacheFile + " aborted.", 5) @@ -1299,9 +1322,9 @@ Module PBMap MyDebug(*PBMap, "Key : " + key + " added in memory cache", 4) EndIf ; If there's no active download thread for this tile - If *timg\Tile <= 0 + If *timg\Tile <= 0 *timg\nImage = 0 - *timg\Size = FileSize(CacheFile) + *timg\Size = FileSize(CacheFile) ; Manage tile file lifetime, delete if too old, or if size = 0 If *PBMap\Options\TileLifetime <> -1 If *timg\Size >= 0 ; Does the file exists ? @@ -1311,14 +1334,14 @@ Module PBMap *timg\Size = 0 Else MyDebug(*PBMap, " Can't delete image file " + CacheFile, 3) - UnlockMutex(*PBMap\MemoryCacheAccessMutex) - ProcedureReturn #False EndIf + UnlockMutex(*PBMap\MemoryCacheAccessMutex) + ProcedureReturn #False EndIf EndIf EndIf ; Try To load it from HD - If *timg\Size > 0 + If *timg\Size > 0 *timg\nImage = GetTileFromHDD(*PBMap, CacheFile.s) Else MyDebug(*PBMap, " Failed loading from HDD " + CacheFile + " -> Filesize = " + FileSize(CacheFile), 3) @@ -1333,9 +1356,10 @@ Module PBMap If *PBMap\ThreadsNB < *PBMap\Options\MaxThreads If *PBMap\DownloadSlots < *PBMap\Options\MaxDownloadSlots ; Launch a new web loading thread - *PBMap\DownloadSlots + 1 Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) If *NewTile + *timg\Tile = *NewTile ; There's now a loading thread + *timg\Alpha = 0 With *NewTile ; New tile parameters \key = key @@ -1346,12 +1370,13 @@ Module PBMap \Window = *PBMap\Window \Gadget = *PBMap\Gadget \GetImageThread = CreateThread(@GetImageThread(), *NewTile) - If \GetImageThread - *timg\Tile = *NewTile ; There's now a loading thread - *timg\Alpha = 0 + If \GetImageThread MyDebug(*PBMap, " Creating get image thread nb " + Str(\GetImageThread) + " to get " + CacheFile + " (key = " + key, 3) *PBMap\ThreadsNB + 1 + *PBMap\DownloadSlots + 1 Else + ; Thread creation failed this time + *timg\Tile = 0 MyDebug(*PBMap, " Can't create get image thread to get " + CacheFile, 3) FreeMemory(*NewTile) EndIf @@ -3141,11 +3166,13 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf -; IDE Options = PureBasic 5.61 (Windows - x64) -; CursorPosition = 2648 -; FirstLine = 2606 +; IDE Options = PureBasic 5.70 LTS (Windows - x64) +; CursorPosition = 255 +; FirstLine = 227 ; Folding = --------------------- ; EnableThread ; EnableXP +; EnableOnError +; DisableDebugger ; CompileSourceDirectory ; DisablePurifier = 1,1,1,1 \ No newline at end of file From 629c469a6b4c6f31f11f5f803876e54bc360a46a Mon Sep 17 00:00:00 2001 From: djes Date: Thu, 18 Jul 2019 16:24:12 +0200 Subject: [PATCH 06/14] Code cleanup Misc internal fixes, especially in GetImageThread() --- PBMap.pb | 130 +++++++++++++++++++++++++++---------------------------- 1 file changed, 64 insertions(+), 66 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 66ac1fd..d9dc0fb 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -1141,7 +1141,7 @@ Module PBMap ; If cache size exceeds limit, try to delete the oldest tiles used (first in the time stack) Procedure MemoryCacheManagement(MapGadget.i) Protected *PBMap.PBMap = PBMaps(Str(MapGadget)) - LockMutex(*PBMap\MemoryCacheAccessMutex) ; Prevents thread to start or finish + LockMutex(*PBMap\MemoryCacheAccessMutex) ; Prevents threads to start or finish Protected CacheSize = MapSize(*PBMap\MemCache\Images()) * Pow(*PBMap\TileSize, 2) * 4 ; Size of a tile = TileSize * TileSize * 4 bytes (RGBA) Protected CacheLimit = *PBMap\Options\MaxMemCache * 1024 MyDebug(*PBMap, "Cache size : " + Str(CacheSize/1024) + " / CacheLimit : " + Str(CacheLimit/1024), 5) @@ -1228,7 +1228,7 @@ Module PBMap Threaded Progress = 0, Quit = #False - Procedure GetImageThread(*Tile.Tile) + Procedure GetImageThread(*Tile.Tile) ;LockMutex(*PBMap\MemoryCacheAccessMutex) ;MyDebug(*PBMap, "Thread nb " + Str(*Tile\GetImageThread) + " " + *Tile\key + " starting for image " + *Tile\CacheFile, 5) ; If MemoryCache is currently being cleaned, abort @@ -1287,7 +1287,7 @@ Module PBMap Protected *timg.ImgMemCach = FindMapElement(*PBMap\MemCache\Images(), key) If *timg MyDebug(*PBMap, "Key : " + key + " found in memory cache", 4) - ; Is the associated image already been loaded in memory ? + ; Is the associated image already loaded in memory ? If *timg\nImage ; Yes, returns the image's nb MyDebug(*PBMap, " as image " + *timg\nImage, 4) @@ -1325,13 +1325,14 @@ Module PBMap *PBMap\MemCache\ImagesTimeStack()\MapKey = MapKey(*PBMap\MemCache\Images()) MyDebug(*PBMap, "Key : " + key + " added in memory cache", 4) EndIf - ; If there's no active download thread for this tile + ; If there's no active downloading thread for this image If *timg\Tile <= 0 *timg\nImage = 0 - *timg\Size = FileSize(CacheFile) - ; Manage tile file lifetime, delete if too old, or if size = 0 - If *PBMap\Options\TileLifetime <> -1 - If *timg\Size >= 0 ; Does the file exists ? + *timg\Size = FileSize(CacheFile) + ; Does a valid file exists on HD ? Try to load it. + If *timg\Size >= 0 + ; Manage tile file lifetime, delete if too old, or if size = 0 + If *PBMap\Options\TileLifetime <> -1 If *timg\Size = 0 Or (Date() - GetFileDate(CacheFile, #PB_Date_Modified) > *PBMap\Options\TileLifetime) ; If Lifetime > MaxLifeTime ; There's a bug with #PB_Date_Created If DeleteFile(CacheFile) MyDebug(*PBMap, " Deleting image file " + CacheFile, 3) @@ -1343,58 +1344,53 @@ Module PBMap ProcedureReturn #False EndIf EndIf - EndIf - ; Try To load it from HD - If *timg\Size > 0 - *timg\nImage = GetTileFromHDD(*PBMap, CacheFile.s) - Else - MyDebug(*PBMap, " Failed loading from HDD " + CacheFile + " -> Filesize = " + FileSize(CacheFile), 3) - EndIf - If *timg\nImage - ; Image found and loaded from HDD - *timg\Alpha = 0 - UnlockMutex(*PBMap\MemoryCacheAccessMutex) - ProcedureReturn *timg - Else - ; If GetTileFromHDD failed, will load it (again?) from the web - If *PBMap\ThreadsNB < *PBMap\Options\MaxThreads - If *PBMap\DownloadSlots < *PBMap\Options\MaxDownloadSlots - ; Launch a new web loading thread - Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) - If *NewTile - *timg\Tile = *NewTile ; There's now a loading thread - *timg\Alpha = 0 - With *NewTile - ; New tile parameters - \key = key - \URL = URL - \CacheFile = CacheFile - \nImage = 0 - \Time = ElapsedMilliseconds() - \Window = *PBMap\Window - \Gadget = *PBMap\Gadget - \GetImageThread = CreateThread(@GetImageThread(), *NewTile) - If \GetImageThread - MyDebug(*PBMap, " Creating get image thread nb " + Str(\GetImageThread) + " to get " + CacheFile + " (key = " + key, 3) - *PBMap\ThreadsNB + 1 - *PBMap\DownloadSlots + 1 - Else - ; Thread creation failed this time - *timg\Tile = 0 - MyDebug(*PBMap, " Can't create get image thread to get " + CacheFile, 3) - FreeMemory(*NewTile) - EndIf - EndWith - Else - MyDebug(*PBMap, " Error, can't allocate memory for a new tile loading thread", 3) - EndIf - Else - MyDebug(*PBMap, " Thread needed " + key + " for image " + CacheFile + " canceled because no free download slot.", 5) - EndIf - Else - MyDebug(*PBMap, " Error, maximum threads nb reached", 3) + ; Try to load tile's image from HD + *timg\nImage = GetTileFromHDD(*PBMap, CacheFile.s) + If *timg\nImage + ; Success : image found and loaded from HDD + *timg\Alpha = 0 + UnlockMutex(*PBMap\MemoryCacheAccessMutex) + ProcedureReturn *timg EndIf EndIf + ; If GetTileFromHDD failed, will try to download the image from the web in a thread + MyDebug(*PBMap, " Failed loading from HDD " + CacheFile + " -> Filesize = " + FileSize(CacheFile), 3) + If *PBMap\ThreadsNB < *PBMap\Options\MaxThreads + If *PBMap\DownloadSlots < *PBMap\Options\MaxDownloadSlots + Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) + If *NewTile + *timg\Tile = *NewTile ; There's now a loading thread + *timg\Alpha = 0 + With *NewTile + ; New tile parameters + \key = key + \URL = URL + \CacheFile = CacheFile + \nImage = 0 + \Time = ElapsedMilliseconds() + \Window = *PBMap\Window + \Gadget = *PBMap\Gadget + \GetImageThread = CreateThread(@GetImageThread(), *NewTile) + If \GetImageThread + MyDebug(*PBMap, " Creating get image thread nb " + Str(\GetImageThread) + " to get " + CacheFile + " (key = " + key, 3) + *PBMap\ThreadsNB + 1 + *PBMap\DownloadSlots + 1 + Else + ; Thread creation failed this time + *timg\Tile = 0 + MyDebug(*PBMap, " Can't create get image thread to get " + CacheFile, 3) + FreeMemory(*NewTile) + EndIf + EndWith + Else + MyDebug(*PBMap, " Error, can't allocate memory for a new tile loading thread", 3) + EndIf + Else + MyDebug(*PBMap, " Thread needed " + key + " for image " + CacheFile + " canceled because no free download slot.", 5) + EndIf + Else + MyDebug(*PBMap, " Error, maximum threads nb reached", 3) + EndIf EndIf UnlockMutex(*PBMap\MemoryCacheAccessMutex) ProcedureReturn #False @@ -2687,7 +2683,7 @@ Module PBMap EndIf Case #PB_EventType_LeftButtonUp SetGadgetAttribute(*PBMap\Gadget,#PB_Canvas_Cursor,#PB_Cursor_Default) ; YA normal mouse pointer - ; *PBMap\MoveStartingPoint\x = - 1 + ; *PBMap\MoveStartingPoint\x = - 1 *PBMap\Dragging = #False *PBMap\Redraw = #True ;YA to knows marker coordinates after moving @@ -2702,18 +2698,19 @@ Module PBMap *PBMap\Redraw = #True Case #PB_MAP_RETRY *PBMap\Redraw = #True - ;- #PB_MAP_TILE_CLEANUP : Tile web loading thread cleanup - ; After a Web tile loading thread, clean the tile structure memory, see GetImageThread() + ;- *** Tile web loading thread cleanup + ; After a Web tile loading thread, cleans the tile structure memory, see GetImageThread() Case #PB_MAP_TILE_CLEANUP + LockMutex(*PBMap\MemoryCacheAccessMutex) ; Prevents threads to start or finish *Tile = EventData() key = *Tile\key *Tile\Download = 0 If FindMapElement(*PBMap\MemCache\Images(), key) <> 0 ; If the map element has not been deleted during the thread lifetime (should not occur) - *PBMap\MemCache\Images(key)\Tile = *Tile\Size + ;*PBMap\MemCache\Images(key)\Tile = *Tile\Size If *Tile\Size *PBMap\MemCache\Images(key)\Tile = -1 ; Web loading thread has finished successfully - ;- Allows to post edit the tile image file with a customised code + ; Allows to post edit the tile image file with a customised code If *PBMap\CallBackModifyTileFile TileNewFilename = *PBMap\CallBackModifyTileFile(*Tile\CacheFile, *Tile\URL) If TileNewFilename @@ -2729,10 +2726,11 @@ Module PBMap *PBMap\ThreadsNB - 1 *PBMap\DownloadSlots - 1 *PBMap\Redraw = #True + UnlockMutex(*PBMap\MemoryCacheAccessMutex) EndSelect EndProcedure - ; Redraws at regular intervals + ;-*** Main timer : Cache management and drawing Procedure TimerEvents() Protected *PBMap.PBMap ForEach PBMaps() @@ -3185,8 +3183,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.70 LTS (Windows - x64) -; CursorPosition = 3154 -; FirstLine = 3136 +; CursorPosition = 1369 +; FirstLine = 1354 ; Folding = --------------------- ; EnableThread ; EnableXP \ No newline at end of file From ff6a10feb4011333490d344577ddabc8b63f0be9 Mon Sep 17 00:00:00 2001 From: djes Date: Thu, 18 Jul 2019 16:38:22 +0200 Subject: [PATCH 07/14] Update README.md --- README.md | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0e5a1d2..4bc0dbd 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,14 @@ -# PBMap +# PBMap 0.91 Open source tiled map software. -To develop tiled map applications in PureBasic. +Module to develop tiled map applications in PureBasic, like like OpenStreetMap(c), Google Maps(c), Here(c), ... -Based on OpenStreetMap services +Functional example based on OpenStreetMap services(c) OSM copyright : http://www.openstreetmap.org/copyright -This code is free, but any use should mention the origin of this code. +This code is free, but any user should mention the origin of this code. -Officials forums topics here : +Official forums topics : http://www.purebasic.fr/english/viewtopic.php?f=27&t=66320 (english) http://www.purebasic.fr/french/viewtopic.php?f=3&t=16160 (french) @@ -16,6 +16,11 @@ Contributors : Thyphoon djes Idle + +Thanks to : Progi1984 yves86 -André +André +falsam + +Special thanks to Fred and Fantaisie Software's team From 86acc0be602f89b882d513138096fbc21c08d4a5 Mon Sep 17 00:00:00 2001 From: djes Date: Thu, 18 Jul 2019 16:39:22 +0200 Subject: [PATCH 08/14] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 4bc0dbd..3ef3f0d 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,12 @@ Open source tiled map software. Module to develop tiled map applications in PureBasic, like like OpenStreetMap(c), Google Maps(c), Here(c), ... + Functional example based on OpenStreetMap services(c) + OSM copyright : http://www.openstreetmap.org/copyright + This code is free, but any user should mention the origin of this code. Official forums topics : From 741aa2afb11adf3570833ddffda37a0580862114 Mon Sep 17 00:00:00 2001 From: djes Date: Thu, 18 Jul 2019 16:39:41 +0200 Subject: [PATCH 09/14] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3ef3f0d..4d3b905 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Open source tiled map software. Module to develop tiled map applications in PureBasic, like like OpenStreetMap(c), Google Maps(c), Here(c), ... -Functional example based on OpenStreetMap services(c) +Functional example based on OpenStreetMap(c) services OSM copyright : http://www.openstreetmap.org/copyright From 705be8f877082b0ad158b6b8186cc1cc1742fff3 Mon Sep 17 00:00:00 2001 From: djes Date: Thu, 18 Jul 2019 20:56:33 +0200 Subject: [PATCH 10/14] Incomplete image file download automatic deletion Little update to delete incomplete (eventually malformed) image file --- PBMap.pb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index d9dc0fb..e01259f 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -1273,7 +1273,7 @@ Module PBMap EndIf ; End of the memory cache access ;LockMutex(*PBMap\MemoryCacheAccessMutex) - PostEvent(#PB_Event_Gadget, *Tile\Window, *Tile\Gadget, #PB_MAP_TILE_CLEANUP, *Tile) ; To free memory outside the thread + PostEvent(#PB_Event_Gadget, *Tile\Window, *Tile\Gadget, #PB_MAP_TILE_CLEANUP, *Tile) ; To free memory and eventually delete aborted image file outside the thread ;UnlockMutex(*PBMap\MemoryCacheAccessMutex) EndProcedure @@ -2720,6 +2720,11 @@ Module PBMap EndIf Else *PBMap\MemCache\Images(key)\Tile = 0 + If DeleteFile(*Tile\CacheFile) + MyDebug(*PBMap, " Deleting not fully loaded image file " + *Tile\CacheFile, 3) + Else + MyDebug(*PBMap, " Can't delete not fully loaded image file " + *Tile\CacheFile, 3) + EndIf EndIf EndIf FreeMemory(*Tile) ; Frees the data needed for the thread (*tile=*PBMap\MemCache\Images(key)\Tile) @@ -3183,8 +3188,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.70 LTS (Windows - x64) -; CursorPosition = 1369 -; FirstLine = 1354 +; CursorPosition = 1261 +; FirstLine = 1242 ; Folding = --------------------- ; EnableThread ; EnableXP \ No newline at end of file From a909362ee2375c63b02a05240f74ae7eec200b5b Mon Sep 17 00:00:00 2001 From: djes Date: Thu, 18 Jul 2019 21:39:34 +0200 Subject: [PATCH 11/14] Update PBMap.pb --- PBMap.pb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index e01259f..ad145bd 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -1243,6 +1243,7 @@ Module PBMap ;UnlockMutex(*PBMap\MemoryCacheAccessMutex) *Tile\Size = 0 *Tile\Download = ReceiveHTTPFile(*Tile\URL, *Tile\CacheFile, #PB_HTTP_Asynchronous, #USERAGENT) + ;TODO : obtain original file size to compare and eventually delete truncated file If *Tile\Download Repeat Progress = HTTPProgress(*Tile\Download) @@ -1273,7 +1274,8 @@ Module PBMap EndIf ; End of the memory cache access ;LockMutex(*PBMap\MemoryCacheAccessMutex) - PostEvent(#PB_Event_Gadget, *Tile\Window, *Tile\Gadget, #PB_MAP_TILE_CLEANUP, *Tile) ; To free memory and eventually delete aborted image file outside the thread + ; To free memory and eventually delete aborted image file outside the thread + PostEvent(#PB_Event_Gadget, *Tile\Window, *Tile\Gadget, #PB_MAP_TILE_CLEANUP, *Tile) ;UnlockMutex(*PBMap\MemoryCacheAccessMutex) EndProcedure @@ -2709,6 +2711,7 @@ Module PBMap ; If the map element has not been deleted during the thread lifetime (should not occur) ;*PBMap\MemCache\Images(key)\Tile = *Tile\Size If *Tile\Size + ;TODO : check if file size = server file size *PBMap\MemCache\Images(key)\Tile = -1 ; Web loading thread has finished successfully ; Allows to post edit the tile image file with a customised code If *PBMap\CallBackModifyTileFile @@ -3188,8 +3191,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.70 LTS (Windows - x64) -; CursorPosition = 1261 -; FirstLine = 1242 +; CursorPosition = 1245 +; FirstLine = 1230 ; Folding = --------------------- ; EnableThread ; EnableXP \ No newline at end of file From 10e88b685a6ac3a9f0711303574907a9882d9a72 Mon Sep 17 00:00:00 2001 From: djes Date: Thu, 18 Jul 2019 22:07:23 +0200 Subject: [PATCH 12/14] pngcheck comment --- PBMap.pb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index ad145bd..e82184f 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -2712,6 +2712,7 @@ Module PBMap ;*PBMap\MemCache\Images(key)\Tile = *Tile\Size If *Tile\Size ;TODO : check if file size = server file size + ;and eventually use pngcheck to avoid problematic files http://www.libpng.org/pub/png/apps/pngcheck.html *PBMap\MemCache\Images(key)\Tile = -1 ; Web loading thread has finished successfully ; Allows to post edit the tile image file with a customised code If *PBMap\CallBackModifyTileFile @@ -3191,8 +3192,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.70 LTS (Windows - x64) -; CursorPosition = 1245 -; FirstLine = 1230 +; CursorPosition = 2714 +; FirstLine = 2702 ; Folding = --------------------- ; EnableThread ; EnableXP \ No newline at end of file From 297fe00f575533f4440e841f3f51e12193520d30 Mon Sep 17 00:00:00 2001 From: djes Date: Fri, 19 Jul 2019 11:15:29 +0200 Subject: [PATCH 13/14] PNG checking Thanks to idle ! --- PBMap.pb | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index e82184f..0c9f547 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -1181,10 +1181,30 @@ Module PBMap UnlockMutex(*PBMap\MemoryCacheAccessMutex) EndProcedure + ;- LoadImage workaround + ; by idle + ; Check that the file is valid + Procedure _LoadImage(ImageNumber, File.s) + Protected fn, pat, pos, res + If UCase(GetExtensionPart(File)) = "PNG" + pat = $444E4549 + fn= ReadFile(#PB_Any, File) + If fn + pos = Lof(fn) + FileSeek(fn, pos - 8) + res = ReadLong(fn) + CloseFile(fn) + If res = pat + ProcedureReturn LoadImage(ImageNumber, File) + EndIf + EndIf + EndIf + EndProcedure + Procedure.i GetTileFromHDD(*PBMap.PBMap, CacheFile.s) ;Directly pass the PBMap structure (faster) Protected nImage.i, LifeTime.i, MaxLifeTime.i ; Everything is OK, loads the file - nImage = LoadImage(#PB_Any, CacheFile) + nImage = _LoadImage(#PB_Any, CacheFile) If nImage MyDebug(*PBMap, " Success loading " + CacheFile + " as nImage " + Str(nImage), 3) ProcedureReturn nImage @@ -3192,8 +3212,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.70 LTS (Windows - x64) -; CursorPosition = 2714 -; FirstLine = 2702 +; CursorPosition = 2962 +; FirstLine = 2945 ; Folding = --------------------- ; EnableThread ; EnableXP \ No newline at end of file From bbd1d75c02d320fddcc24c723938b7089992f814 Mon Sep 17 00:00:00 2001 From: djes Date: Fri, 19 Jul 2019 13:42:09 +0200 Subject: [PATCH 14/14] Update PBMap.pb --- PBMap.pb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 0c9f547..f27d88d 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -1183,7 +1183,7 @@ Module PBMap ;- LoadImage workaround ; by idle - ; Check that the file is valid + ; Check that the PNG file is valid Procedure _LoadImage(ImageNumber, File.s) Protected fn, pat, pos, res If UCase(GetExtensionPart(File)) = "PNG" @@ -3212,8 +3212,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.70 LTS (Windows - x64) -; CursorPosition = 2962 -; FirstLine = 2945 +; CursorPosition = 1185 +; FirstLine = 1171 ; Folding = --------------------- ; EnableThread ; EnableXP \ No newline at end of file