im creating plugin vbs2. in program can output grid in either lat long,utm or mgrs. need able convert bng. have managed create tkinter application in python works using proj.4 need create in c++ dll.
the latlong im using (51.20650n 1.81906w) known point, bng conversion approx su 127 452.
#include <proj_api.h> double x = 51.20650; //atof(getstrargument(input, 0)); double y = 1.81906; //atof(getstrargument(input, 1)); char *pj_latlongc = "+proj=longlat +datum=wgs84"; char *pj_utmc = "+proj=utm +zone=29 +ellps=wgs84"; char *osc = "+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=osgb36 +units=m +no_defs"; projpj pj_os = pj_init_plus(osc); projpj pj_utm = pj_init_plus(pj_utmc); projpj pj_latlong = pj_init_plus(pj_latlongc); x *= deg_to_rad; y *= deg_to_rad; int p = pj_transform(pj_latlong, pj_os, 1, 1, &x, &y, null); unfortunately result wrong , doesn't make sense.could on this?
switching x y , flipping sign of x seems yield better results. running numbers on console, proj output seems agree better the point specified (51.20650n 1.81906w)
echo -1.81906 51.20650 | proj -v +proj=tmerc longitude: 1d49'8.616"w [ -1.81906 ] latitude: 51d12'23.4"n [ 51.2065 ] similarly, cs2cs returns seemingly sensible output:
echo -1.81906 51.20650 | cs2cs -v +proj=longlat +datum=wgs84 +to +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=osgb36 +units=m +no_defs # ---- coordinate system ---- #lat/long (geodetic alias) # # +proj=longlat +datum=wgs84 +ellps=wgs84 +towgs84=0,0,0 # ---- coordinate system ---- #transverse mercator # cyl, sph&ell # +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 # +ellps=airy +datum=osgb36 +units=m +no_defs # +towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894 #--- following specified not used # +ellps=airy 412736.92 145270.05 -47.95 this identical c++ program's output
double x = -1.81906; double y = 51.20650; 412736.924409 145270.054358 for comparison, ran numbers through bng converter of british geological survey, seems agree well
easting: 412737 northing: 145270 for writing these numbers in form of ordnance survey national grid reference, subtract integer multiples of 100km derive corresponding letter illustrated in source code javascript. final result su 127 452, desired:
#include <proj_api.h> #include <iostream> int main() { double x = -1.81906; double y = 51.20650; const char* pj_latlongc = "+proj=longlat +datum=wgs84"; const char* osc = "+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=osgb36 +units=m +no_defs"; projpj pj_latlong = pj_init_plus(pj_latlongc); projpj pj_os = pj_init_plus(osc); x *= deg_to_rad; y *= deg_to_rad; int p = pj_transform(pj_latlong, pj_os, 1, 1, &x, &y, null); std::cout.setf(std::ios::fixed, std::ios::floatfield); std::cout.setf(std::ios::showpoint); std::cout << x << " " << y << std::endl;; // prints 412736.924409 145270.054358 // convert uk grid ref int e1 = floor(x/100000); int n1 = floor(y/100000); int e2 = (int)x % 100000 / 100; int n2 = (int)y % 100000 / 100; char l1 = (19 - n1) - (19-n1) % 5 + ((e1 + 10)/5); char l2 = (19 - n1) * 5 % 25 + e1 % 5; if (l1 > 7) l1++; if (l2 > 7) l2++; l1 = 'a' + l1; l2 = 'a' + l2; std::cout << l1 << l2 << ' ' << e2 << ' ' << n2 << std::endl; // prints su 127 452 }
Comments
Post a Comment