Giter Club home page Giter Club logo

maplibrenative.net's Introduction

MapLibre Logo

MaplibreNative.NET

This is a .NET wrapper to the maplibre-native. It is meant to be used in Windows and works with .NET Framework and .NET Core.

Using in your project

Just add a reference to the MaplibreNative.NET.dll (see below how to build from sources), and you can immediately use the classes in the MaplibreNative namespace.

This is a minimal example using the HeadlessFrontend renderer, in a project with .NET 7.0 (Windows) and WPF enabled:

using MaplibreNative;
using Microsoft.Win32;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Media.Imaging;

...

var size = new Size(2048, 2048);

using (var runLoop = new RunLoop(RunLoop.Type.Default))
using (var frontend = new HeadlessFrontend(size))
using (var map = new Map(frontend, new MapObserver(), new MapOptions().WithSize(size).WithMapMode(MapMode.Static)))
{
    map.Style.LoadURL("https://raw.githubusercontent.com/maplibre/demotiles/gh-pages/style.json");

    byte[]? imageData = null;

    map.RenderStill(new CameraOptions().WithZoom(0), MapDebugOptions.NoDebug, ex =>
    {
        var image = frontend.ReadStillImage();
        imageData = new byte[image.Bytes];
        Marshal.Copy(image.Data, imageData, 0, (int)image.Bytes);

        runLoop.Stop();
    });

    runLoop.Run();

    var saveDialog = new SaveFileDialog();
    saveDialog.Filter = "PNG Image|*.png";

    if (saveDialog.ShowDialog() ?? false)
    {
        for (int i = 0; i < imageData.Length; i += 4)
        {
            var temp = imageData[i];
            imageData[i] = imageData[i + 2];
            imageData[i + 2] = temp;
        }

        var bitmap = new WriteableBitmap((int)size.Width, (int)size.Height, 96, 96, System.Windows.Media.PixelFormats.Pbgra32, null);
        bitmap.WritePixels(new System.Windows.Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight), imageData, bitmap.BackBufferStride, 0);
        var encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bitmap));

        using (var file = new FileStream(saveDialog.FileName, FileMode.Create))
        {
            encoder.Save(file);
        }
    }
}

...

You can check other examples at the Examples directory.

Attention: the examples have a reference to MaplibreNative.NET.dll located at build\Release subdirectory. So you have to build MaplibreNative.NET with Release configuration before building the examples.

Building the sources

Prerequisites

As this is a .NET build, Microsoft Visual Studio is required. This project was developed using a Microsoft Visual Studio 2022 environment, but it should work with Microsoft Visual Studio 2019 as well.

To install the required Visual Studio components, open Visual Studio Installer and check Desktop Development with C++ option. Make sure C++ CMake tools for Windows is selected in the right pane. If git is not already installed, select Git for Windows option in Individual Components. When Visual Studio finishes the install process, everything is ready to start.

Downloading sources

Open x64 Native Tools Command Prompt for VS 2022 and then clone the repository:

git clone --recurse-submodules -j8 https://github.com/tdcosta100/MaplibreNative.NET.git
cd MaplibreNative.NET

Configuring

Configure the build with the following command:

cmake . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release

It will take some time to build and install all components on which the project depends.

Alternative configure commands

To configure build with EGL support (ANGLE libraries will be build), use the following command:

cmake . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DMLN_WITH_EGL=ON

To configure build with OSMesa (software rendering), use the following command:

cmake . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DMLN_WITH_OSMESA=ON

WARNING: as OSMesa doesn't have static libraries, it's necessary to copy libglapi.dll, libGLESv2.dll and osmesa.dll from dependencies\maplibre-native\platform\windows\vendor\mesa3d\<arch> to executable/dll directory you want to use, otherwise it won't run.

Building

Finally, build the project with the following command:

cmake --build build

Building with Microsoft Visual Studio

Just omit the -G Ninja option from the configure command:

cmake . -B build

The same can be done with alternative configure commands:

cmake . -B build -DMLN_WITH_EGL=ON

or

cmake . -B build -DMLN_WITH_OSMESA=ON

Once configure is done, open the file build\MaplibreNative.NET.sln. Build the target ALL_BUILD to build all targets, or pick a specific target. Don't forget to pick a build configuration (Release, RelWithDebInfo, MinSizeRel or Debug), otherwise the project will be built with default configuration (Debug).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.