NationalSecurityAgency/qgis-latlontools-plugin

Include functions for expression builder

klakar opened this issue · 11 comments

Would it be possible to include a set of functions for the expression builder based on the LatLonTools calculations?

Example:
MGRS functions to extract GZD and bigram etc for use in layouts.

Code for some MGRS functions as example (should be saved as python/expressions/latlontools.py):
latlontools.txt

Including functions like this would mean that it is possible to create layout templates with reference to mgrs with only one single plugin required.

/Thanks for a really useful plugin.

@klakar Thank you for your nice comments. I like your idea and will make it my next project. It may take several weeks to get started because I am currently working on another project.

@klakar Sorry it has taken so long to get to this. I had other projects I was working on. Thank you for the examples and I just wanted to let you know I am starting this project. Are there any other functions that you think would be nice to include. I will do an initial release with just the MGRS coordinates and then will work on all the other different coordinate conversions. I should have an initial release this week.

Fantastic, looking forward to it.

@klakar I just released 3.4.0 of lat lon tools. It might take a few minutes to propagate so that it shows up as a new version. At this point I am not really advertising that the functions are there and would like you to test them. You will find them under 'Lat Lon Tools'. I renamed some of them. Here are the names: mgrs, mgrs_gzd, mgrs_ups, mgrs_east, mgrs_north.

The first 3 characters are the grid zone designator (GZD). The first two characters are the UTM zone. Do you think the UTM zone should be added? I will start populating this with more functions, but wanted to show you this first release.

Tested it and it looks great.
For mgrs it would be useful with a function for the 100 km grid square ID letters, characters number 4 and 5. (We call them "bigram" in Sweden)
I'm not sure what would be an appropriate name for a function for those in English.

All seems to work and it will be really useful, but it will be a lot of code in the expressions to generate the required latitude and longitude using the transform function. It should be possible to make it optional to specify the coordinates in any CRS, as long as the third variable is the coordinate EPSG code. Especially since most mgrs maps will be UTM projected anyway.

mgrs(Any_X, Any_Y, EPSG)

It could very well default to EPGS:4326, but this would really speed up the use in the expression builder. The conversion from one CRS to another should be easier to do once in python, than every time in the expressions. There may also be an issue with what is more intuitive to have as the first parameter. Always X, or Latitude for lat/long. I guess it could be lat, long as default and switch to x, y when projected coordinates and EPSG is used.

The UTM zone number could be useful, but as a separate utm_zone() function with utm_east() and utm_north().
For mgrs I think mgrs_gzd() is enough to cover the UTM-zone.

I just noted in 3.4.0 that the mgrs_ups() function gives a result even outside the pole areas. It should not be any results between 80 deg south and 84 deg north.

Also checked on Wikipedia (https://en.wikipedia.org/wiki/Military_Grid_Reference_System) and one reference to the 100km grid square id is "AA scheme", so just simply mgrs_AA() could be an option for the function name for that.

@klakar Here is one of the documents I have been looking at for MGRS.

https://earth-info.nga.mil/GandG/coordsys/grids/mgrs.doc

I will change the functions to work properly with the 80 deg south and 84 deg north boundaries. It appears to me the the grid zone in the polar regions becomes 1 character (A,B,Y,Z). So grid zone seems to be valid in the polar regions, but utm is not. I will add a UTM zone function as well.

The above document gives the name for the 100km grid square as UPS. That is why I called the function that, but to avoid confusion I think I will call it mgrs_100km. Since it appears that UPS can have different meanings and the 100km grid is define in both the UTM and polar areas I hope this avoids confusion. Do you have any better suggestion?

I have added an option CRS name and will have it default to EPSG:4326. Before I release the next version would you be able to take a look at the attached version? Uninstall the current Lat Lon Tools then run the install from zip. This contains all the changes I have made so far.

latlontools_preview.zip

Great job!

I think mgrs_100k() is a great name that won't be mistaken. I also hope the very clear help text for the reversed y and x order don't confuse anyone, might need to keep an eye on that one. (Damn those navy folks, switching x and y around like that.)

Yes, the mgrs.doc is correct, and I think you misinterpreted the (or UPS) part as "that is its name". The document talks about 100'000-meter grid squares within the grid zone i.e. within the normal GZD areas and not the UPS area, which are only found in the North and South Pole areas. As stated the grid zones are in the UPS areas too.

(I'm sorry if I over-explain it)

UPS is actually "Universal Polar Stereographic" and not at all a UTM projection. Therefore it is not accurate to describe UTM zone numbers for these areas in a context with MGRS, since MGRS don't use UTM in those areas...

Screenshot-20200910210320-1992x1584
UPS (for the North) is inside the circle, UTM outside it. But this separation between UTM and UPS only apply on MGRS, not to UTM by it self.

Also, the UTM zone part is present in the GZD function where it matters, so the mgrs_utm() function should be removed. It could be included in a set of utm() functions like:

  • utm() - The full UTM 00[NS] 000000E 0000000N
  • utm_zone() - The UTM zone number 00
  • utm_hemi() - The UTM zone hemisphere [NS]
  • utm_east() - The easting part without letter 000000
  • utm_north() - The easting part without letter 0000000

Again, great work! It will be really useful.

When I read more about the specifics on UTM and UPS, UTM actually don't exist north of 84 degree and south of 80. The combination of UTM and UPS is a package for world coverage in a uniform projected system.
So even an UTM function should not have any values in the UPS areas, and vice versa.

Thanks for all of your assistance. I updated the QGIS repo with version 3.4.1. I added in an mgrs_to_point function to create a point geometry. I removed the UTM function, but plan on doing a suite of UTM functions along with all the other formats Lat Lon Tools supports. If there are any other changes you think I should make please let me know and I will work on them next week.

Works great, and now the only requirement for MGRS map layouts are the Lat Lon Tools plugin.
Find a template example at http://qgis-hub.fast-page.org/layouts.php
Screenshot-20200912093843-726x878