Custom map query coordinate conversion

Tibor4 years ago

Hi,
I'd like to use a local custom server so that I can use the service while being offline. The tile server uses a different coordinate system(EPSG 23700, Hungary specific) from the one that traccar uses by default (EPSG 3857). Is there a way to transform the coordinate system in Traccar so that the query get valid tiles?
Right now, at low zoom levels I can see the map, but the tracked object is at a wrong location. I know this, because I tried using a different tile server with EPSG 3857 system and it worked. However that is not suitable for my offline usage.

Thanks!

Anton Tananaev4 years ago

Check BaseMap class. We are using other coordinate systems for some map layers. It should give you an idea on how to do it.

Tibor4 years ago

I found the relevant parts in BaseMap.js. Since the browser does not include this file directly, but app.min.js, I added the following to the other projection definitions part of the file(replaced '=' signs with \x3d):

proj4.defs("EPSG:23700",'+proj\x3dsomerc +lat_0\x3d47.14439372222222 +lon_0\x3d19.04857177777778 +k_0\x3d0.99993 +x_0\x3d650000 +y_0\x3d200000 +ellps\x3dGRS67 +datum\x3dHD72 +towgs84\x3d52.17,-71.82,-14.9,0,0,0,0 +units\x3dm +no_defs');ol.proj.proj4.register(proj4);

I also modified the following line in the tile query part in app.min.js, removing the unnecessary whitespaces. I left them here for readability.

            case 'custom':
                layer = new ol.layer.Tile({
                    source: new ol.source.XYZ({
                        projection:'EPSG:23700', // Added for compatibility with GeoServer
                        url: Ext.String.htmlDecode(server.get('mapUrl')),
                        attributions: ''
                    })
                });

Now the map looks as if it was at the north pole.
https://pasteboard.co/JyFNUux.jpg

The server query stays the same:

http://[ip]:[port]/geoserver/gwc/service/wmts?layer=osm_hungary&style=&tilematrixset=EPSG:23700&Service=WMTS&Request=GetTile&Version=1.0.0&Format=image/png&TileMatrix=EPSG:23700:{z}&TileCol={x}&TileRow={y}

Can you give me some guidance on what might be wrong with this and how to resolve it?

Thanks!

Anton Tananaev4 years ago

Not sure. It would require deeper look into the code.

Andyw4 years ago

Hi - I had the same issue adding UK ordnance survey leisure maps, I added the following code the the baseMaps to get it to work plus the relevant entries in language files and stroes.

            case 'osleisure':
                layer = new ol.layer.Tile({
                    source: new ol.source.XYZ({
                        url: 'https://api.os.uk/maps/raster/v1/zxy/Leisure_27700/{z}/{x}/{y}.png?key=' + oskey,
                        projection: 'EPSG:27700',
                        tileGrid: new ol.tilegrid.TileGrid({
                            resolutions: [ 896.0, 448.0, 224.0, 112.0, 56.0, 28.0, 14.0, 7.0, 3.5, 1.75 ],
                            origin: [ -238375.0, 1376256.0 ]
                        }),						
                        attributions: 'Leisure_27700'
                    })
                });
                break;

and then updated the proj4defs with:

}, function () {
    var projection;
    proj4.defs('BD-MC', '+proj=merc +lon_0=0 +units=m +ellps=clrk66 +no_defs');
    proj4.defs('EPSG:3395', '+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs');
    proj4.defs('EPSG:27700', '+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 +units=m +no_defs');
    ol.proj.proj4.register(proj4);
    projection = ol.proj.get('EPSG:3395');
    if (projection) {
        projection.setExtent([-20037508.342789244, -20037508.342789244, 20037508.342789244, 20037508.342789244]);
    }    

After this it's possibe to test using the {traccarurl}/debug.html, after that use minify to create the app.min.js. I wouldn't recomend you manually edit app.min.js.

Will_ed3 years ago

Would you be able to give more help on getting OS maps to work? I’ve tried adding your above code. The map tiles have gone from not loading at all to only loading blank light blue ones.

Andyw3 years ago

It really depends on which OS map layer you're trying to get working. The URLs below should work with no code changes just using the custom maps. You'll obviously need a key from OS - It's free for light usage - check their T&Cs
Custom xyz

https://api.os.uk/maps/raster/v1/zxy/Outdoor_3857/{z}/{x}/{y}.png?key=<YOUR KEY>
https://api.os.uk/maps/raster/v1/zxy/Road_3857/{z}/{x}/{y}.png?key=<YOUR KEY>
https://api.os.uk/maps/raster/v1/zxy/Light_3857/{z}/{x}/{y}.png?key=<YOUR KEY>

or
ArcGis

https://api.os.uk/maps/raster/v1/wmts?key=<YOUR KEY>&layer=Light_3857&Service=WMTS&Request=GetTile&Version=2.0.0&Format=image/png&TileMatrix=EPSG:3857:{z}&TileCol={x}&TileRow={y}
https://api.os.uk/maps/raster/v1/wmts?key=<YOUR KEY>a&layer=Outdoor_3857&Service=WMTS&Request=GetTile&Version=2.0.0&Format=image/png&TileMatrix=EPSG:3857:{z}&TileCol={x}&TileRow={y}`

I generally use the Outdoor or Roads maps as they're more detailed than the usual 1:50,000 to 1:25,000 maps, but if you want to use these (leisure maps) then you'll need to make some changes to the code. If you want to go down this route then I can send you all the pages I've had to make changes to.

Will_ed3 years ago

Amazing, thank you. I’m looking to use the 1:25/1:50. Outdoor youth group work so need the paths and tracks they show. I’m already signed up with their data hub for the api etc.

Andyw3 years ago

Yes I work with youngsters doing DofE, to get the OS maps working I changed the following files:
web/l10n/en.json
web/app/store/MapTypes.js
web/app/view/map/BaseMap.jsn

Which I've put here: https://www.mediafire.com/file/wf0akvrxrsk9lm8/files.zip/file

Let me know how you get on. - You'll need to update the oskey in the BaseMaps with your key, I'd planned to move it to the config file or DB but never got round to doing it.

Will_ed3 years ago

You sir are an absolute genius, I gave up trying to make this work about a year ago as all of the threads I founds were dead. Made my day, if you're ever in the New Forest for DofE there will be a beer waiting!

Connor3 years ago

Hey Andyw,

Is there any chance that you could upload those files again? Trying to get OS Maps for our DofE Trackers.

Thanks

Andyw3 years ago

Hi Connor,
Always happy to help someone who is willing to give up their time for DofE, Here's the files:
https://www.mediafire.com/file/gwxpeootgme89j7/OSMAP_Files.zip/file

You'll need to amend to add your api key, I've also created a new version where the api key is held in the DB and changed via the admin interface, but that required a small change to the traccar-server.jar code and woud break if you upgrade to a newer version - just let me know if you want that code as well.

I'm not sure how much you know about traccar, but these changes will only work if you run in debug mode unless you run minifi to create the app.min.js.

-Andy

Connor3 years ago

Hey Andy,

You are a star my friend! Works like a charm. Owe you a pint if you are ever in the Nottingham area!

Cheers,

Connor

Freddie3 years ago

I love the open source world at times - I'd just found the OS APIs and was about to start trying to get the leisure maps working, and you've already done it - Thank you!

Would you consider contributing your work to the project on GitHub to make it easier for other GB users to use the maps?

Thanks again,

Freddie

Andyw3 years ago

Hi Freddie,

Yes I'd planned to do this when I get time, there's a coulpe of issues I've not yet resolved.

  1. I Don't have translations for all the language files supported by traccar
  2. An OS maps icon should be added to the map to comply with the API license agreement.

-Andy