Import Proj native library in C#

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Import Proj native library in C#

zagot
Someone can help me to define this C struct in C#?

#if !defined(PROJECTS_H)
    typedef struct { double u, v; } projUV;
    typedef void *projPJ;
    #define projXY projUV
    #define projLP projUV
#else
    typedef PJ *projPJ;
#   define projXY   XY
#   define projLP       LP
#endif

typedef struct PJconsts {
    XY  (*fwd)(LP, struct PJconsts *);
    LP  (*inv)(XY, struct PJconsts *);
    void (*spc)(LP, struct PJconsts *, struct FACTORS *);
    void (*pfree)(struct PJconsts *);
    const char *descr;
    paralist *params;   /* parameter list */
    int over;   /* over-range flag */
    int geoc;   /* geocentric latitude flag */
        int is_latlong; /* proj=latlong ... not really a projection at all */
        int is_geocent; /* proj=geocent ... not really a projection at all */
    double
        a,  /* major axis or radius if es==0 */
                a_orig, /* major axis before any +proj related adjustment */
        es, /* e ^ 2 */
                es_orig, /* es before any +proj related adjustment */
        e,  /* eccentricity */
        ra, /* 1/A */
        one_es, /* 1 - e^2 */
        rone_es, /* 1/one_es */
        lam0, phi0, /* central longitude, latitude */
        x0, y0, /* easting and northing */
        k0, /* general scaling factor */
        to_meter, fr_meter; /* cartesian scaling */

        int     datum_type; /* PJD_UNKNOWN/3PARAM/7PARAM/GRIDSHIFT/WGS84 */
        double  datum_params[7];
        double  from_greenwich; /* prime meridian offset (in radians) */
        double  long_wrap_center; /* 0.0 for -180 to 180, actually in radians*/

} PJ;

Thanks in advance, G.
Reply | Threaded
Open this post in threaded view
|

Re: Import Proj native library in C#

support.mn
Hello,

there is a manual about how to link proj.4 to your C
project. It is with the other pdf manuals and it tells
you how to do it.

Regards: Janne.

-----------------------------------------

zagot [[hidden email]] kirjoitti:

> Someone can help me to define this C struct in C#?
>
> #if !defined(PROJECTS_H)
>     typedef struct { double u, v; } projUV;
>     typedef void *projPJ;
>     #define projXY projUV
>     #define projLP projUV
> #else
>     typedef PJ *projPJ;
> #   define projXY   XY
> #   define projLP       LP
> #endif
>
> typedef struct PJconsts {
>     XY  (*fwd)(LP, struct PJconsts *);
>     LP  (*inv)(XY, struct PJconsts *);
>     void (*spc)(LP, struct PJconsts *, struct FACTORS *);
>     void (*pfree)(struct PJconsts *);
>     const char *descr;
>     paralist *params;   /* parameter list */
>     int over;   /* over-range flag */
>     int geoc;   /* geocentric latitude flag */
>         int is_latlong; /* proj=latlong ... not really a projection at all
> */
>         int is_geocent; /* proj=geocent ... not really a projection at all
> */
>     double
>         a,  /* major axis or radius if es==0 */
>                 a_orig, /* major axis before any +proj related adjustment */
>         es, /* e ^ 2 */
>                 es_orig, /* es before any +proj related adjustment */
>         e,  /* eccentricity */
>         ra, /* 1/A */
>         one_es, /* 1 - e^2 */
>         rone_es, /* 1/one_es */
>         lam0, phi0, /* central longitude, latitude */
>         x0, y0, /* easting and northing */
>         k0, /* general scaling factor */
>         to_meter, fr_meter; /* cartesian scaling */
>
>         int     datum_type; /* PJD_UNKNOWN/3PARAM/7PARAM/GRIDSHIFT/WGS84 */
>         double  datum_params[7];
>         double  from_greenwich; /* prime meridian offset (in radians) */
>         double  long_wrap_center; /* 0.0 for -180 to 180, actually in
> radians*/
>
> } PJ;
>
> Thanks in advance, G.
>
>
> --
> View this message in context: http://osgeo-org.1803224.n2.nabble.com/Import-Proj-native-library-in-C-tp6589496p6589496.html
> Sent from the PROJ.4 mailing list archive at Nabble.com.
> _______________________________________________
> Proj mailing list
> [hidden email]
> http://lists.maptools.org/mailman/listinfo/proj
>

_______________________________________________
Proj mailing list
[hidden email]
http://lists.maptools.org/mailman/listinfo/proj
Reply | Threaded
Open this post in threaded view
|

Re: Import Proj native library in C#

zagot
This post was updated on .
Sorry I have read the manuals

e.g. I follow this sample
http://trac.osgeo.org/proj/wiki/ProjAPI

but if I try to import this code in C# as follow
    unsafe class Program
    {
        public struct projUV  {public double u; public double v;        }

        [DllImport(@"C:\Users\t\Desktop\Lib\proj.dll", CharSet = CharSet.Auto,
            EntryPoint = "pj_free")]
        static extern void pj_free(void* p);

        [DllImport(@"C:\Users\t\Desktop\Lib\proj.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "pj_init_plus")]
        static extern void* pj_init_plus([MarshalAs(UnmanagedType.LPStr)]string m);

        [DllImport(@"C:\Users\t\Desktop\Lib\proj.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto,  EntryPoint = "pj_inv")]
        static extern projUV pj_inv(projUV px, void* py);

        [DllImport(@"C:\Users\t\Desktop\Lib\proj.dll", EntryPoint = "pj_transform")]
        static extern int pj_transform(void* src, void* dst, long point_count, int point_offset,
                          double* x, double* y, double* z);

        static void Main(string[] args)  {

            string proSrc = "+proj=merc +ellps=clrk66 +lat_ts=33";
            string proDst = "+proj=latlong +ellps=clrk66";
       
            void* projSrc = pj_init_plus(proSrc);
            void* projDst = pj_init_plus(proDst);

            double x = 15000;
            double y = 10000;
            double z = 0.0;

            double ris = pj_transform(projSrc, projDst, 1, 0, &x, &y, &z);

        }
    }

 I have this error:Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Note:
in the official sample there are this declaration: projPJ pj_merc, pj_latlong;
I use void* when call pj_init_plus but I think that  typedef PJ *projPJ is more exactly
..then I need to define PJ struct in C# ...as I ask you in the first post

what I wrong?

thanks
p.s. sorry for my english
G.

Reply | Threaded
Open this post in threaded view
|

Re: Import Proj native library in C#

support.mn
In reply to this post by zagot
Zagot,

the real juice is in the pdf document

http://www.polardist.com/pub/PROJ.4.3.pdf

starting from page 26 chapter:

"Programming with the Cartographic Library"

You should read that and it gives all the answers
needed to use that in your C-sharp program. Do
not add any classes. Everything is already in the
headers.

You just need to include the header file and
start to use the procedures and data available.

Regards: Janne.

---------------------------------------------------------


zagot [[hidden email]] kirjoitti:

> Sorry I have read the manuals
>
> e.g. I follow this sample
> http://trac.osgeo.org/proj/wiki/ProjAPI
>
> but if I try to import this code in C# as follow
>     unsafe class Program
>     {
>         public struct projUV  {public double u; public double v;        }
>
>         [DllImport(@"C:\Users\t\Desktop\Lib\proj.dll", CharSet =
> CharSet.Auto,
>             EntryPoint = "pj_free")]
>         static extern void pj_free(void* p);
>
>         [DllImport(@"C:\Users\t\Desktop\Lib\proj.dll", CallingConvention =
> CallingConvention.Cdecl, EntryPoint = "pj_init_plus")]
>         static extern void*
> pj_init_plus([MarshalAs(UnmanagedType.LPStr)]string m);
>
>         [DllImport(@"C:\Users\t\Desktop\Lib\proj.dll", CallingConvention =
> CallingConvention.Cdecl, CharSet = CharSet.Auto,  EntryPoint = "pj_inv")]
>         static extern projUV pj_inv(projUV px, void* py);
>
>         [DllImport(@"C:\Users\t\Desktop\Lib\proj.dll", EntryPoint =
> "pj_transform")]
>         static extern int pj_transform(void* src, void* dst, long
> point_count, int point_offset,
>                           double* x, double* y, double* z);
>
>         static void Main(string[] args)  {
>
>             string proSrc = "+proj=merc +ellps=clrk66 +lat_ts=33";
>             string proDst = "+proj=latlong +ellps=clrk66";
>
>             void* projSrc = pj_init_plus(proSrc);
>    void* projDst = pj_init_plus(proDst);
>
>             double x = 15000;
>             double y = 10000;
>             double z = 0.0;
>
>             double ris = pj_transform(projSrc, projDst, 1, 0, &x, &y, &z);
>
>         }
>     }
>
>  I have this error:Attempted to read or write protected memory. This is
> often an indication that other memory is corrupt.
>
> Note: in the official sample there are this declaration:
> projPJ pj_merc, pj_latlong;
>
> I use void* when call pj_init_plus (projPJ is defined as void* in
> proj_api.h)
>
> what I wrong?
>
> thanks
> p.s. sorry for my english
> G.
>
>
>
> --
> View this message in context: http://osgeo-org.1803224.n2.nabble.com/Import-Proj-native-library-in-C-tp6589496p6589769.html
> Sent from the PROJ.4 mailing list archive at Nabble.com.
> _______________________________________________
> Proj mailing list
> [hidden email]
> http://lists.maptools.org/mailman/listinfo/proj
>

_______________________________________________
Proj mailing list
[hidden email]
http://lists.maptools.org/mailman/listinfo/proj
Reply | Threaded
Open this post in threaded view
|

Re: Import Proj native library in C#

zagot
Hi Janne,

you are right ...the headers is the only we need
...with the right definition of function calls in C#

thanks for the reply
Reply | Threaded
Open this post in threaded view
|

Re: Import Proj native library in C#

Eric Miller-4
In reply to this post by zagot
I'm not sure if that sample is based on an I early version I published, but a somewhat better implementation is available in the following zip file:

http://ftp.dfg.ca.gov/Public/BDB/Tools/proj4/csharp-4.7.0.zip

It's a little dated, but is able to avoid "unsafe" code in most cases.

--
Eric G. Miller
Staff Programmer
Department of Fish and Game
>>> zagot <[hidden email]> 07/16/11 8:20 AM >>>
Sorry I have read the manuals

e.g. I follow this sample
http://trac.osgeo.org/proj/wiki/ProjAPI



_______________________________________________
Proj mailing list
[hidden email]
http://lists.maptools.org/mailman/listinfo/proj