From f6dac8c6ebdb6e48ea52464b53b897a7ec4d660e Mon Sep 17 00:00:00 2001 From: Jacob Robbins Date: Tue, 27 Oct 2020 07:43:04 -0500 Subject: [PATCH] Add ProjectionsRegistry.RegisterAlias method (#72) * Add ProjectionsRegistry.RegisterAlias method * Use existing proj name -> key format in existing proj lookup closes #71 --- .../Projections/ProjectionsRegistry.cs | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/ProjNet/CoordinateSystems/Projections/ProjectionsRegistry.cs b/src/ProjNet/CoordinateSystems/Projections/ProjectionsRegistry.cs index 00cfac1..b0cb481 100644 --- a/src/ProjNet/CoordinateSystems/Projections/ProjectionsRegistry.cs +++ b/src/ProjNet/CoordinateSystems/Projections/ProjectionsRegistry.cs @@ -70,7 +70,7 @@ public static void Register(string name, Type type) if (ci == null) throw new ArgumentException("The provided type is lacking a suitable constructor", nameof(type)); - string key = name.ToLowerInvariant().Replace(' ', '_'); + string key = ProjectionNameToRegistryKey(name); lock (RegistryLock) { if (TypeRegistry.ContainsKey(key)) @@ -86,6 +86,29 @@ public static void Register(string name, Type type) } } + private static string ProjectionNameToRegistryKey(string name) + { + return name.ToLowerInvariant().Replace(' ', '_'); + } + + /// + /// Register an alias for an existing Map. + /// + /// + /// + public static void RegisterAlias(string aliasName, string existingName) + { + lock (RegistryLock) + { + if (!TypeRegistry.TryGetValue(ProjectionNameToRegistryKey(existingName), out var existingProjectionType)) + { + throw new ArgumentException($"{existingName} is not a registered projection type"); + } + + Register(aliasName, existingProjectionType); + } + } + private static Type CheckConstructor(Type type) { // find a constructor that accepts exactly one parameter that's an @@ -106,7 +129,7 @@ private static Type CheckConstructor(Type type) internal static MathTransform CreateProjection(string className, IEnumerable parameters) { - string key = className.ToLowerInvariant().Replace(' ', '_'); + string key = ProjectionNameToRegistryKey(className); Type projectionType; Type ci;