From 182dc0cbab80636591fcf97277242ed488fdd392 Mon Sep 17 00:00:00 2001 From: zwishing Date: Fri, 31 May 2024 21:18:10 +0800 Subject: [PATCH 1/3] Add Proj::to_epsg --- src/proj.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/proj.rs b/src/proj.rs index 755b662c..71f38ca5 100644 --- a/src/proj.rs +++ b/src/proj.rs @@ -6,9 +6,9 @@ use proj_sys::{ proj_context_destroy, proj_context_errno, proj_context_get_url_endpoint, proj_context_is_network_enabled, proj_context_set_search_paths, proj_context_set_url_endpoint, proj_create, proj_create_crs_to_crs, proj_destroy, proj_errno_string, proj_get_area_of_use, - proj_grid_cache_set_enable, proj_info, proj_normalize_for_visualization, proj_pj_info, - proj_trans, proj_trans_array, proj_trans_bounds, PJconsts, PJ_AREA, PJ_CONTEXT, PJ_COORD, - PJ_DIRECTION_PJ_FWD, PJ_DIRECTION_PJ_INV, PJ_INFO, PJ_LPZT, PJ_XYZT, + proj_get_id_code, proj_grid_cache_set_enable, proj_info, proj_normalize_for_visualization, + proj_pj_info, proj_trans, proj_trans_array, proj_trans_bounds, PJconsts, PJ, PJ_AREA, + PJ_CONTEXT, PJ_COORD, PJ_DIRECTION_PJ_FWD, PJ_DIRECTION_PJ_INV, PJ_INFO, PJ_LPZT, PJ_XYZT, }; use std::{ convert, ffi, @@ -584,6 +584,17 @@ impl Proj { transform_epsg(ctx, from, to, area) } + /// Returns epsg code for Proj + /// + /// # Safety + /// This method contains unsafe code. + pub fn to_epsg(&self) -> Result<&str, str::Utf8Error> { + let pj = self.c_proj as *const PJ; + let c_char = unsafe { proj_get_id_code(pj, 0) }; + let c_str = unsafe { CStr::from_ptr(c_char) }; + c_str.to_str() + } + /// Set the bounding box of the area of use /// /// This bounding box will be used to specify the area of use @@ -1461,7 +1472,7 @@ mod test { let usa_m = MyPoint::new(-115.797615, 37.2647978); let usa_ft = to_feet.convert(usa_m).unwrap(); assert_relative_eq!(6693625.67217475, usa_ft.x()); - assert_relative_eq!(3497301.5918027232, usa_ft.y(), epsilon=1e-8); + assert_relative_eq!(3497301.5918027232, usa_ft.y(), epsilon = 1e-8); } #[test] @@ -1476,4 +1487,10 @@ mod test { assert_eq!(area.north, 84.73); assert!(name.contains("Europe")); } + #[test] + fn test_to_epsg() { + let proj = Proj::new("EPSG:3059").unwrap(); + let epsg = proj.to_epsg().unwrap(); + assert_eq!(epsg, "3059") + } } From c655515cf0d9e051784ad33e5ae1a8c68a78195a Mon Sep 17 00:00:00 2001 From: zwishing Date: Sat, 1 Jun 2024 22:11:02 +0800 Subject: [PATCH 2/3] Add Proj::id_code and Proj::id_auth_name --- src/proj.rs | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/proj.rs b/src/proj.rs index 71f38ca5..0149bafc 100644 --- a/src/proj.rs +++ b/src/proj.rs @@ -1,20 +1,8 @@ use libc::c_int; use libc::{c_char, c_double}; use num_traits::Float; -use proj_sys::{ - proj_area_create, proj_area_destroy, proj_area_set_bbox, proj_cleanup, proj_context_create, - proj_context_destroy, proj_context_errno, proj_context_get_url_endpoint, - proj_context_is_network_enabled, proj_context_set_search_paths, proj_context_set_url_endpoint, - proj_create, proj_create_crs_to_crs, proj_destroy, proj_errno_string, proj_get_area_of_use, - proj_get_id_code, proj_grid_cache_set_enable, proj_info, proj_normalize_for_visualization, - proj_pj_info, proj_trans, proj_trans_array, proj_trans_bounds, PJconsts, PJ, PJ_AREA, - PJ_CONTEXT, PJ_COORD, PJ_DIRECTION_PJ_FWD, PJ_DIRECTION_PJ_INV, PJ_INFO, PJ_LPZT, PJ_XYZT, -}; -use std::{ - convert, ffi, - fmt::{self, Debug}, - str, -}; +use proj_sys::{proj_area_create, proj_area_destroy, proj_area_set_bbox, proj_cleanup, proj_context_create, proj_context_destroy, proj_context_errno, proj_context_get_url_endpoint, proj_context_is_network_enabled, proj_context_set_search_paths, proj_context_set_url_endpoint, proj_create, proj_create_crs_to_crs, proj_destroy, proj_errno_string, proj_get_area_of_use, proj_get_id_code,proj_get_id_auth_name, proj_grid_cache_set_enable, proj_info, proj_normalize_for_visualization, proj_pj_info, proj_trans, proj_trans_array, proj_trans_bounds, PJconsts, PJ, PJ_AREA, PJ_CONTEXT, PJ_COORD, PJ_DIRECTION_PJ_FWD, PJ_DIRECTION_PJ_INV, PJ_INFO, PJ_LPZT, PJ_XYZT}; +use std::{convert, ffi, fmt::{self, Debug}, ptr, str}; #[cfg(feature = "network")] use proj_sys::proj_context_set_enable_network; @@ -584,17 +572,28 @@ impl Proj { transform_epsg(ctx, from, to, area) } - /// Returns epsg code for Proj - /// + /// Return Authority code for Proj + /// /// # Safety /// This method contains unsafe code. - pub fn to_epsg(&self) -> Result<&str, str::Utf8Error> { + pub fn id_code(&self) -> Result<&str, str::Utf8Error> { let pj = self.c_proj as *const PJ; let c_char = unsafe { proj_get_id_code(pj, 0) }; let c_str = unsafe { CStr::from_ptr(c_char) }; c_str.to_str() } + /// Return Authority Name for Proj + /// + /// # Safety + /// This method contains unsafe code. + pub fn id_auth_name(&self)-> Result<&str, str::Utf8Error>{ + let pj = self.c_proj as *const PJ; + let c_char = unsafe { proj_get_id_auth_name(pj, 0) }; + let c_str = unsafe { CStr::from_ptr(c_char) }; + c_str.to_str() + } + /// Set the bounding box of the area of use /// /// This bounding box will be used to specify the area of use @@ -1488,9 +1487,15 @@ mod test { assert!(name.contains("Europe")); } #[test] - fn test_to_epsg() { - let proj = Proj::new("EPSG:3059").unwrap(); - let epsg = proj.to_epsg().unwrap(); - assert_eq!(epsg, "3059") + fn test_id_code() { + let proj = Proj::new("EPSG:3035").unwrap(); + let code = proj.id_code().unwrap(); + assert_eq!(code, "3035") + } + #[test] + fn test_id_auth_name() { + let proj = Proj::new("EPSG:3035").unwrap(); + let auth_name = proj.id_auth_name().unwrap(); + assert_eq!(auth_name, "EPSG") } } From 6131e823fc07b6ac47de841a906843a5b17247f4 Mon Sep 17 00:00:00 2001 From: zwishing Date: Sun, 2 Jun 2024 00:53:01 +0800 Subject: [PATCH 3/3] cargo fmt code --- src/proj.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/proj.rs b/src/proj.rs index 0149bafc..43046b62 100644 --- a/src/proj.rs +++ b/src/proj.rs @@ -1,8 +1,21 @@ use libc::c_int; use libc::{c_char, c_double}; use num_traits::Float; -use proj_sys::{proj_area_create, proj_area_destroy, proj_area_set_bbox, proj_cleanup, proj_context_create, proj_context_destroy, proj_context_errno, proj_context_get_url_endpoint, proj_context_is_network_enabled, proj_context_set_search_paths, proj_context_set_url_endpoint, proj_create, proj_create_crs_to_crs, proj_destroy, proj_errno_string, proj_get_area_of_use, proj_get_id_code,proj_get_id_auth_name, proj_grid_cache_set_enable, proj_info, proj_normalize_for_visualization, proj_pj_info, proj_trans, proj_trans_array, proj_trans_bounds, PJconsts, PJ, PJ_AREA, PJ_CONTEXT, PJ_COORD, PJ_DIRECTION_PJ_FWD, PJ_DIRECTION_PJ_INV, PJ_INFO, PJ_LPZT, PJ_XYZT}; -use std::{convert, ffi, fmt::{self, Debug}, ptr, str}; +use proj_sys::{ + proj_area_create, proj_area_destroy, proj_area_set_bbox, proj_cleanup, proj_context_create, + proj_context_destroy, proj_context_errno, proj_context_get_url_endpoint, + proj_context_is_network_enabled, proj_context_set_search_paths, proj_context_set_url_endpoint, + proj_create, proj_create_crs_to_crs, proj_destroy, proj_errno_string, proj_get_area_of_use, + proj_get_id_auth_name, proj_get_id_code, proj_grid_cache_set_enable, proj_info, + proj_normalize_for_visualization, proj_pj_info, proj_trans, proj_trans_array, + proj_trans_bounds, PJconsts, PJ, PJ_AREA, PJ_CONTEXT, PJ_COORD, PJ_DIRECTION_PJ_FWD, + PJ_DIRECTION_PJ_INV, PJ_INFO, PJ_LPZT, PJ_XYZT, +}; +use std::{ + convert, ffi, + fmt::{self, Debug}, + ptr, str, +}; #[cfg(feature = "network")] use proj_sys::proj_context_set_enable_network; @@ -587,7 +600,7 @@ impl Proj { /// /// # Safety /// This method contains unsafe code. - pub fn id_auth_name(&self)-> Result<&str, str::Utf8Error>{ + pub fn id_auth_name(&self) -> Result<&str, str::Utf8Error> { let pj = self.c_proj as *const PJ; let c_char = unsafe { proj_get_id_auth_name(pj, 0) }; let c_str = unsafe { CStr::from_ptr(c_char) };