Skip to content

Commit 9aba50e

Browse files
author
Szekeres Tamás
committed
CSharp interface: Add SpatialReference.FindMatches (fixes OSGeo#12578)
1 parent d11b3d5 commit 9aba50e

File tree

3 files changed

+90
-29
lines changed

3 files changed

+90
-29
lines changed

swig/csharp/apps/OSRTransform.cs

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*****************************************************************************/
1313

1414
using System;
15+
using System.Linq;
1516
using OSGeo.OSR;
1617

1718
/**
@@ -25,33 +26,37 @@
2526
/// A C# based sample to make simple transformations.
2627
/// </summary>
2728
class OSRTransform {
28-
public static void Main(string[] args) {
29-
try
30-
{
31-
/* -------------------------------------------------------------------- */
32-
/* Initialize srs */
33-
/* -------------------------------------------------------------------- */
34-
SpatialReference src = new SpatialReference("");
35-
src.ImportFromProj4("+proj=latlong +datum=WGS84 +no_defs");
36-
Console.WriteLine( "SOURCE IsGeographic:" + src.IsGeographic() + " IsProjected:" + src.IsProjected() );
37-
SpatialReference dst = new SpatialReference("");
38-
dst.ImportFromProj4("+proj=somerc +lat_0=47.14439372222222 +lon_0=19.04857177777778 +x_0=650000 +y_0=200000 +ellps=GRS67 +units=m +no_defs");
39-
Console.WriteLine( "DEST IsGeographic:" + dst.IsGeographic() + " IsProjected:" + dst.IsProjected() );
40-
/* -------------------------------------------------------------------- */
41-
/* making the transform */
42-
/* -------------------------------------------------------------------- */
43-
CoordinateTransformation ct = new CoordinateTransformation(src, dst);
44-
double[] p = new double[3];
45-
p[0] = 19; p[1] = 47; p[2] = 0;
46-
ct.TransformPoint(p);
47-
Console.WriteLine("x:" + p[0] + " y:" + p[1] + " z:" + p[2]);
48-
ct.TransformPoint(p, 19.2, 47.5, 0);
49-
Console.WriteLine("x:" + p[0] + " y:" + p[1] + " z:" + p[2]);
50-
}
51-
catch (Exception e)
52-
{
53-
Console.WriteLine("Error occurred: " + e.Message);
54-
System.Environment.Exit(-1);
55-
}
56-
}
29+
public static void Main(string[] args) {
30+
try
31+
{
32+
/* -------------------------------------------------------------------- */
33+
/* Initialize srs */
34+
/* -------------------------------------------------------------------- */
35+
int nvalues;
36+
int[] confidence_values;
37+
SpatialReference src = new SpatialReference("");
38+
src.ImportFromProj4("+proj=latlong +datum=WGS84 +no_defs");
39+
var srName = src.FindMatches(new string[] { "", null }, out nvalues, out confidence_values).FirstOrDefault()?.GetName() ?? "NotFound";
40+
Console.WriteLine($"SOURCE Name:'{srName}' IsGeographic:{src. IsGeographic ()} IsProjected:{src.IsProjected()}");
41+
SpatialReference dst = new SpatialReference("");
42+
dst.ImportFromProj4("+proj=somerc +lat_0=47.14439372222222 +lon_0=19.04857177777778 +x_0=650000 +y_0=200000 +ellps=GRS67 +units=m +no_defs");
43+
srName = dst.FindMatches(new string[] { "", null }, out nvalues, out confidence_values).FirstOrDefault()?.GetName() ?? "NotFound";
44+
Console.WriteLine($"DEST Name:'{srName}' IsGeographic:{dst.IsGeographic()} IsProjected:{dst.IsProjected()}");
45+
/* -------------------------------------------------------------------- */
46+
/* making the transform */
47+
/* -------------------------------------------------------------------- */
48+
CoordinateTransformation ct = new CoordinateTransformation(src, dst);
49+
double[] p = new double[3];
50+
p[0] = 19; p[1] = 47; p[2] = 0;
51+
ct.TransformPoint(p);
52+
Console.WriteLine("x:" + p[0] + " y:" + p[1] + " z:" + p[2]);
53+
ct.TransformPoint(p, 19.2, 47.5, 0);
54+
Console.WriteLine("x:" + p[0] + " y:" + p[1] + " z:" + p[2]);
55+
}
56+
catch (Exception e)
57+
{
58+
Console.WriteLine("Error occurred: " + e.Message);
59+
System.Environment.Exit(-1);
60+
}
61+
}
5762
}

swig/include/csharp/typemaps_csharp.i

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,14 @@ OPTIONAL_POD(int, int);
556556
}
557557
%}
558558

559+
%csmethodmodifiers CPLMemDestroy "internal";
560+
%inline %{
561+
void CPLMemDestroy(void *buffer_ptr) {
562+
if (buffer_ptr)
563+
CPLFree(buffer_ptr);
564+
}
565+
%}
566+
559567
/******************************************************************************
560568
* ErrorHandler callback support *
561569
*****************************************************************************/
@@ -598,3 +606,44 @@ OPTIONAL_POD(int, int);
598606
* GDALGetLayerByName typemaps *
599607
*****************************************************************************/
600608
%apply ( const char *utf8_path ) { const char* layer_name };
609+
610+
/******************************************************************************
611+
* SpatialReference.FindMatches *
612+
*****************************************************************************/
613+
%apply (int *hasval) {int *nvalues};
614+
%typemap(imtype, out="IntPtr") OSRSpatialReferenceShadow** FindMatches "SpatialReference[]"
615+
%typemap(cstype) OSRSpatialReferenceShadow** FindMatches %{SpatialReference[]%}
616+
%typemap(imtype) int** confidence_values "out IntPtr"
617+
%typemap(cstype) int** confidence_values %{out int[]%}
618+
%typemap(csin) int** confidence_values "out confValPtr"
619+
%typemap(in) (int** confidence_values)
620+
{
621+
/* %typemap(in) (int** confidence_values) */
622+
$1 = ($1_ltype)$input;
623+
}
624+
%typemap(csout, excode=SWIGEXCODE) OSRSpatialReferenceShadow** FindMatches {
625+
/* %typemap(csout) char** FindMatches */
626+
IntPtr confValPtr;
627+
IntPtr cPtr = $imcall;
628+
IntPtr objPtr;
629+
SpatialReference[] ret = new SpatialReference[nvalues];
630+
confidence_values = (confValPtr == IntPtr.Zero) ? null : new int[nvalues];
631+
if (nvalues > 0) {
632+
for(int cx = 0; cx < nvalues; cx++) {
633+
objPtr = System.Runtime.InteropServices.Marshal.ReadIntPtr(cPtr, cx * System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)));
634+
ret[cx]= (objPtr == IntPtr.Zero) ? null : new SpatialReference(objPtr, true, null);
635+
if (confValPtr != IntPtr.Zero) {
636+
confidence_values[cx] = System.Runtime.InteropServices.Marshal.ReadInt32(confValPtr, cx * System.Runtime.InteropServices.Marshal.SizeOf(typeof(Int32)));
637+
}
638+
639+
}
640+
}
641+
if (cPtr != IntPtr.Zero) {
642+
$modulePINVOKE.CPLMemDestroy(cPtr);
643+
}
644+
if (confValPtr != IntPtr.Zero) {
645+
$modulePINVOKE.CPLMemDestroy(confValPtr);
646+
}
647+
$excode
648+
return ret;
649+
}

swig/include/osr.i

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,13 @@ public:
566566
}
567567
#endif
568568

569+
#ifdef SWIGCSHARP
570+
OSRSpatialReferenceShadow** FindMatches(char** options, int* nvalues, int** confidence_values)
571+
{
572+
return OSRFindMatches(self, options, nvalues, confidence_values);
573+
}
574+
#endif
575+
569576
OGRErr SetProjection( char const *arg ) {
570577
return OSRSetProjection( self, arg );
571578
}

0 commit comments

Comments
 (0)