[PROJ] PROJ and Unicode on Windows

Peter Townsend peter.townsend at maplarge.com
Thu Apr 6 13:31:32 PDT 2023


Thanks, but I can't really use the SharpProj way. It's kinda using the .NET
string as an intermediary. Plus I need to support a Linux build so I can't
use C++/CLI anyway. The utf_8string method that takes in the .NET string
works kinda similar to doing this w/o it:
std::string utf8_string(String^ v)
{
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> conv;
    pin_ptr<const wchar_t> pPath = PtrToStringChars(v);
    std::wstring vstr(pPath);
    std::string sstr(conv.to_bytes(vstr));
    return sstr;
}
const char* convertResult4(const char* result) {
    if (!result)
        return result;

    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> conv;
    std::wstring str = conv.from_bytes(result);
    std::string sstr(conv.to_bytes(str));
    ...
}

The std::wstring str contains the correctly encoded string, but turning it
back to a const char* using std::string sstr.c_str() just garbles it back
again.

I might have to just proxy everything over the managed/unmanaged pinvoke
wall as a wchar_t* or just make everything IntPtrs and Marshal them that
way.



On Thu, Apr 6, 2023 at 10:32 AM Bert Huijben <bert at qqmail.nl> wrote:

>                 Hi Peter,
>
>
>
> When I needed proj for my work on my previous day-job, I spend a bit extra
> time and created a complete wrapping C# library that is still used there
> and a few other places. The wrapping is specifically targeted towards
> Windows, but works there with .Net Framework and .Net core. See
> https://github.com/ampscm/sharpproj/ (or just use SharpProj from NuGet)
>
>
>
>
>
> The sample code I have on that page shows +- what you try here, so you
> should be able to use that to try your use-cases around encoding.
>
>
>
> [[
>
> using SharpProj;
>
>
>
> using var rd = CoordinateReferenceSystem.CreateFromEpsg(28992);
>
> using var wgs84 = CoordinateReferenceSystem.CreateFromEpsg(4326);
>
>
>
> var area = rd.UsageArea;
>
> Assert.AreEqual("Netherlands - onshore, including Waddenzee, Dutch Wadden
> Islands and 12-mile offshore coastal zone.", area.Name);
>
>
>
> using (var t = CoordinateTransform.Create(rd, wgs84))
>
> {
>
>     var r = t.Apply(new PPoint(155000, 463000));
>
>     Assert.AreEqual(new PPoint(52.155, 5.387), r.ToXY(3)); // Round to 3
> decimals for easy testing
>
> }
>
> ]]
>
>
>
> If you pick EPSG 23031, you will see that the encodings work there.
>
>
>
>
>
> You can check all the sourcecode too, if you just want to check how to get
> the en-/decoding to work. (It is all Apache licensed, so feel free to
> copy&paste… or provide pull requests if you want something added to the
> library)
>
>
>
>                 Bert
>
>
>
>
>
>
>
> *From:* PROJ <proj-bounces at lists.osgeo.org> *On Behalf Of *Even Rouault
> *Sent:* Wednesday, April 5, 2023 11:53 PM
> *To:* Peter Townsend <peter.townsend at maplarge.com>; proj <
> proj at lists.osgeo.org>
> *Subject:* Re: [PROJ] PROJ and Unicode on Windows
>
>
>
> Peter,
>
> there isn't any issue in your build. It is just that PROJ returns UTF-8
> encoded strings and that the typical Windows console isn't configured to
> display UTF-8. Cf
> https://stackoverflow.com/questions/57131654/using-utf-8-encoding-chcp-65001-in-command-prompt-windows-powershell-window
> or similar issues
>
> Even
>
> Le 05/04/2023 à 23:44, Peter Townsend via PROJ a écrit :
>
> I've got a bit of an annoyance with my windows proj build. Hopefully it's
> not too hard to resolve as the world of char/wchar_t/etc. isn't something
> I'm terribly familiar with.
>
>
>
> Take for example the area of use of EPSG:23031. On Linux it's fine, but on
> windows there's a unicode issue.
>
>
>
> PJ* crs = proj_create(m_ctxt, "EPSG:23031");
> ASSERT_NE(crs, nullptr);
> ObjectKeeper keeper_crsH(crs);
>
> double w, s, e, n;
> const char* a;
> proj_get_area_of_use(m_ctxt, crs, &w, &s, &e, &n, &a);
>
>
>
> Contents of a:
>
> "Europe - between 0°E and 6°E - Andorra; Denmark (North Sea); Germany
> offshore; Netherlands offshore; Norway including Svalbard - onshore and
> offshore; Spain - onshore (mainland and Balearic Islands); United Kingdom
> (UKCS) offshore."
>
>
>
> Is there a simple thing I'm overlooking in the build process that might
> clear up the encoding goof? Or do I need to do some bending over backwards
> with character manipulation?
>
>
>
> This is the command line I'm using to build this example:
>
> cmake -DBUILD_SHARED_LIBS=ON
> -DCMAKE_TOOLCHAIN_FILE=C:\dev\vcpkg\scripts\buildsystems\vcpkg.cmake ..
> cmake --build . --config Debug -j 8
>
>
>
> Thanks!
>
> --
>
> Peter Townsend
>
> Senior Software Developer
>
>
>
> _______________________________________________
>
> PROJ mailing list
>
> PROJ at lists.osgeo.org
>
> https://lists.osgeo.org/mailman/listinfo/proj
>
> --
>
> http://www.spatialys.com
>
> My software is free, but my time generally not.
>
>

-- 
Peter Townsend
Senior Software Developer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/proj/attachments/20230406/0d089524/attachment.htm>


More information about the PROJ mailing list