Monday, December 3, 2018

Use PDAL to export colored LAS file to Point Cloud XYZ format text file

PDAL can be used to export out a colored laser LAS file like the one in the screenshot below into a text file in Point Cloud XYZ format, which is just an ASCII text file with each point in a row of X, Y, Z, R, G, B values.

To use PDAL to perform the conversion, follow the following steps.
  1. In Windows, open up an OSGeo4W Shell.


  2. In the command prompt, change directory to the folder containing the LAS file e.g. D:\Temp\data\.

  3. Use a text editor to create a PDAL pipeline JSON file, e.g. las2xyz.json, with the following contents.

    {
      "pipeline":[
        {
          "type":"readers.las",
          "filename":"color.las"
        },
        {
          "type":"writers.text",
          "format":"csv",
          "order":"X,Y,Z,RED:0,GREEN:0,BLUE:0",
          "keep_unspecified":"false",
       "quote_header": "false",
       "delimiter": " ",
          "filename":"outputfile.xyz"
        }
      ]
    }
    

    where color.las is the name of the input LAS file
    and outputfile.xyz is obviously the name of the output XYZ file.
  4. In the command prompt, type in the following command to perform the conversion.

    D:\> pdal pipeline las2xyz.json
  5. Press Enter.

    The X, Y, Z, R, G, B values of the LAS file are exported out into the text Point Cloud XYZ file.

Monday, November 26, 2018

How to add a Singapore OneMap tile layer to QGIS

Singapore OneMap provides a wide range of tile maps in difference color schemes that can be used in web map applications (with the proper attribution, of course). For more information about OneMap, visit the website https://docs.onemap.sg/maps/original.html

QGIS has functions to load and display maps from map tile providers such as OneMap. The following shows a few simple steps to connect and add a OneMap original tile layer to the map pane of QGIS.
  1. Start up QGIS.

  2. In the Browser panel, mouse left-click on the Tile Server (XYZ) node. Choose New Connection.

    The New XYZ tile layer dialog box appears.
  3. In the text field, type in the URL of a OneMap tile layer, e.g. https://www.onemap.gov.sg/maps/tiles/Original/{z}/{x}/{y}.png.
  4. Click OK.

    The New XYZ tile layer dialog box appears.
  5. In the text field, type in a meaningful name, e.g. OneMap. Click OK.

    The tile layer is added to the Browser panel.
  6. To display it in the map view, mouse double click on the added layer in the Browser panel.

    The OneMap tile layer is displayed.


Monday, October 29, 2018

Setup CMakeLists.txt for Android Studio and OpenCV C++ coding

If you want to develop C++ code making use of Android and OpenCV classes, then you have to configure Android Studio's CMakeLists.txt build file accordingly.

Please follow the previous post https://dominoc925.blogspot.com/2018/10/how-i-setup-opencv4android-java-sdk.html to download and setup the OpenCV4Android Java SDK with and Android project before doing the following steps.
  1. In Android Studio, open up the app's build.gradle file.

    Note: the OpenCV native libraries are under the jniLibs directory relative to the app's build.gradle file according to the source set jniLibs srcDirs parameter.
     

  2. Enter the cmake cpp flags and platform abi filters  in the android defaultConfig externalNativeBuild section, as shown below.

    externalNativeBuild {
        cmake {
          cppFlags "-frtti -fexceptions"        
    
    abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
    }
    }

  3. Next, open up the CMakeLists.txt file in the editor.

     
  4. Add in the cmake macros to add in the path to the OpenCV C++ include files e.g. /path/to/OpenCV-android-sdk/sdk/native/jni/include/

     
  5.  Then, use the macro add_library to add the OpenCV library.

  6. Next, use the macro set_target_properties to tell CMake the location of the OpenCV library.

    set_target_properties(
         lib_opencv        
    
    PROPERTIES
    IMPORTED_LOCATION
    ${CMAKE_CURRENT_SOURCE_DIR}/jniLibs/${ANDROID_ABI}/libopencv_java3.so
    )


  7. Finally, link the OpenCV library with your code with the target_link_libraries macro.

The full sample of the CMakeLists.txt file is shown below.

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

set (OPENCV4ANDSDK_INCLUDE
        /path/to/OpenCV-android-sdk/sdk/native/jni/include)
include_directories(
    ${OPENCV4ANDSDK_INCLUDE}
)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
        native-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        src/main/cpp/native-lib.cpp)
add_library(
        lib_opencv
        SHARED
        IMPORTED
)
set_target_properties(
        lib_opencv
        PROPERTIES
        IMPORTED_LOCATION
        ${CMAKE_CURRENT_SOURCE_DIR}/jniLibs/${ANDROID_ABI}/libopencv_java3.so
)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)


# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        native-lib

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib}
        lib_opencv
        )




Monday, October 22, 2018

How I setup OpenCV4Android Java SDK with a blank Android project

Setting up an Android Studio project with OpenCV3 can be a little tricky. The official documentation is still using Eclipse and has not been updated with Android Studio. So setting up OpenCV with Android Studio required some trial and error experimentation. I managed to get a template Android Studio project to load the native OpenCV library with the following steps.

Download OpenCV4AndroidSdk
  1. If you have not done so, download OpenCV4Android from this website https://sourceforge.net/projects/opencvlibrary/files/opencv-android/
  2. Extract the files into a folder e.g. /path/to/Open-CV-android-sdk/
Create an Android Project with C++ Support
  1.  Run Android Studio. Choose Create a New Project.

  2. In the Create New Project dialog box, toggle on Include C++ support.
  3. Choose appropriate options and click Next in the following screens.
  4. Click Finish.

    The new project is created.
Import the OpenCV module into Gradle
  1. In the Android Studio File menu, select File | New | Import Module.

    The Import Module from Source dialog box appears.
  2. In the Source directory field, click the ... button and browse to the downloaded OpenCV4Android files and select the java folder e.g. /path/to/OpenCV-android-sdk/sdk/java/.
  3. Optional. In the Module name field, type in a name e.g. :opencv.
  4. Click Next and Finish.

    The module is imported (with most likely error messages)
  5. Options. You can download the missing dependencies by clicking the Install missing platforms and sync project link. Or you can edit the OpenCV4Android's build.gradle file to enter the installed dependencies.
Fix OpenCV4Android configuration files
  1. In Android Studio, open up the file build.gradle from the imported module opencv module e.g. /path/to/opencv/build.gradle. It may be necessary to change the Project pane to display as project view.

  2. In the editor, changed the compiledSdkVersion to 27 (or any installed version), the targetSdkVersion to 27 (or any installed version).
  3. Then sync the gradle project.

    The project is synced but now there is an error about the manifest file.
  4. Open up the manifest file /path/to/opencv/src/main/AndroidManifest.xml in the editor.

  5. Now, comment out the uses-sdk line as shown in the screenshot above.
  6. Sync the project again.

    The OpenCV module is successfully configured.
Add the OpenCV module as a dependency to the Android App project
  1. In Android Studio, select App in the Project pane.
  2. Select File | Project Structure.

    The Project Structure dialog box appears.
  3. Select app. Then click the Dependencies tab. Click +. Choose module dependency.
  4. Choose the imported Opencv module.

    OpenCV is appended to the list.
  5. Click OK.

    The module is added the Android App project.
Copy over the OpenCV native platform binaries to the Android App project
  1.  Open up a File Explorer. Select all the native platform folders under the /path/to/OpenCV-android-sdk/sdk/native/libs/*.

  2. Right click and select Copy.
  3. In Android Studio, right click on the app folder (in project view). Choose Paste.

    The Copy dialog box appears.
  4. In the To directory field, change the text to /path/to/myandroid/app/jniLibs/.
  5. Click OK.

    The native binaries are copied to the project.
  6. To enable the Android project to know where the native binaries are, open up the app's build.gradle file in the editor.

  7. Add in the android class, add in the following sourceSets lines:

    Note: 'jniLibs' means the jniLibs directory relative to this build.gradle file location.
    sourceSets {
        main {
            jniLibs.srcDirs = ['jniLibs']
        }
    }
  8. Sync the project.
Load the native library
  1.  In the MainActivity Kotlin class, type in the following to load in the OpenCV library.

    System.loadLibrary("opencv_java3")
  2. If everything is configured properly, then the Android app can be launched successfully.

    Now you can make any java/Kotlin calls to the built in OpenCV interfaces.

Monday, October 8, 2018

Nana CMake file for Ubuntu

Nana is a "cross-platform library for GUI programming in modern C++ style". More information is available on this website http://nanapro.org/en-us/.

I wanted to use this library with a simple C++ application on Ubuntu. So I created a CMake project with the following folders (see the screenshot below).

Following the getting started and installation instructions here http://nanapro.org/en-us/blog/2016/05/an-introduction-to-nana-c-library/, I downloaded the nana source files, placed them under the nana folder and compiled them into the static library libnana.a.

Next, I created a simple Hello World C++ source file and placed under the src folder.

I then created a CMakeLists.txt file that describes the project and dependencies.

cmake_minimum_required(VERSION 2.8.6)
project(runnana)

find_package (Threads REQUIRED)
find_package (X11 REQUIRED)
find_package(Freetype REQUIRED)

set (CMAKE_BUILD_TYPE Debug)
set (LIBRARY_OUTPUT_PATH bin)

set (CMAKE_CXX_STANDARD 14)
set (CMAKE_CXX_STANDARD_REQUIRED ON)

set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ./bin)
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ./bin)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ./bin)

set (NANA_INCLUDE nana/include)
set (NANA_LINK nana/build/bin)

include_directories (
 ${FREETYPE_INCLUDE_DIRS}
 ${X11_INCLUDE_DIR}
 ${NANA_INCLUDE}
)
link_directories (
 ${NANA_LINK}
)
set (RUNNANA_LINK_LIBS
 stdc++fs
 nana
 ${X11_LIBRARIES}
 ${CMAKE_THREAD_LIBS_INIT}
 rt
 Xft
 png
 asound
 fontconfig
 ${FREETYPE_LIBRARIES}
)
set (RUNNANA_INCLUDE
 include
)

file (GLOB RUNNANA_SOURCES "src/*.c*")
add_executable (runnana ${RUNNANA_SOURCES})
target_include_directories (runnana PUBLIC ${RUNNANA_INCLUDE})
target_link_libraries (runnana ${RUNNANA_LINK_LIBS} stdc++fs)

Using this CMake file, I can easily build the project and compile the simple hello world executable, as shown below.



The example hello world application.




Monday, September 24, 2018

Use CMake to build Visual Studio C++ projects with PDAL on Windows

The tutorial on the PDAL website https://pdal.io/development/writing.html provides the details on developing and compiling a simple C++ program using the PDAL API on *nux. I followed the steps but encountered errors trying to compile the example on Windows 10. The following screenshot shows the compilation errors - the Microsoft Visual Studio linker could not find some external symbols such as __imp_htonl_ among others.


 
To fix the compilation errors, the Windows library ws2_32.lib needs to be linked to the final executable. This can be done in the following steps:
  1. Add the library ws2_32 to the CMakeLists.txt file, as shown below.

  2. Run CMake to build the Visual Studio project files.

    C:\> cmake -G"Visual Studio 15 2017 Win64" ..


    The project files are generated.

  3. Open up the solution project in Visual Studio. Then build the solution.

    The project is successfully built and the executable is generated.

Monday, August 20, 2018

Android Studio: Resolving a "Didn't find class "some.class.name" on path: DexPathList" error

I encountered this error message when trying to run an Android project in Android Studio.
08-15 21:05:44.338 7789-7789/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dom925.cadmon.tampa, PID: 7789
java.lang.RuntimeException: Unable to instantiate application com.dom925.cadmon.tampa.App: java.lang.ClassNotFoundException: Didn't find class "com.dom925.cadmon.tampa.App" on path: DexPathList[[zip file "/data/app/com.dom925.cadmon.tampa-1/base.apk"],nativeLibraryDirectories=[/data/app/com.dom925.cadmon.tampa-1/lib/x86, /system/lib, /vendor/lib]]
at android.app.LoadedApk.makeApplication(LoadedApk.java:802)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5377)
at android.app.ActivityThread.-wrap2(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)



The cause to this particular problem was some missing plugins in the Android app's build.gradle file. As I was adding some Kotlin code to the project, the Gradle build system needs to recognize the Kotlin classes. If the plugins are not included, the Gradle build system will not be able to run the Kotlin code. 

The app's build.gradle file with missing Kotlin plugins
So adding in the Kotlin plugins kotlin-android and/or kotlin-android-extension as shown below solved the problem.
 

Monday, August 13, 2018

How to find the cause of a lint error while generating a signed Android APK

Sometimes when generating a signed APK of an Android App from Android Studio, I would encounter a cryptic lint error message:

Lint found fatal errors while assembling a release target.

To proceed, either fix the issues identified by lint, or modify your build script as follows:
...
android {
    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }
}
...




The message suggests to disable the lint checking but it would probably not be to your benefit to ignore the error.

After some digging around, I found that lint checking outputs a report which you can read to identify the source of the problem. The report can be found in the application's source build directory e.g. /your/Android/app/build/reports/ and named as lint-results-release-fatal.html




If you open up the report in a browser, the cause of the lint error can be easily seen. In this example, the reason for the compilation error is a missing language translation.
 

Monday, May 21, 2018

Perform a 2D geo-registration of a LiDAR LAS file to a GeoTiff image with Cloud Compare

Recently I received LiDAR LAS point clouds generated without using accurate GPS positioning. As a result, the data is not placed at the correct geographic location in the world. To rectify this, I used Cloud Compare to so-called geo-register the point clouds to the world using GeoTiff images as reference.

The following illustrates how it was done:

Load the GeoTiff image as reference
  1. Start Cloud Compare.


  2. Select File | Open.

    The Open file(s) dialog box appears.

  3. Browse and select the reference GeoTiff raster file, e.g. autzen.tif. Click Open.

    The Result type prompt appears.

  4. Click No to load as a texture map instead of converting to a point cloud.

    The Global shift/scale dialog box appears.

  5. Accept the defaults. Click Yes.

    The GeoTiff image is loaded.
Load the LAS file
  1. Select File | Open.


  2. Browse and choose the LAS file, e.g. autzen.las. Click Open.

    The Open LAS File dialog box appears.
  3. Click Apply.

    The Global shift/scale dialog box appears.

  4. Click Yes to accept the defaults.

    The LAS file is loaded.
 Setup the display
  1. In the vertical tool bar on the left, click the Set Current View Mode icon
    and choose Orthographic projection.

    Note: we do not want the perspective projection display to mislead us as to the location of the features
  2. Optional. Select Edit | Colors | Height Ramp. Click OK in the Gradient color dialog box to display the point cloud with a height color gradient for better visualization.


Perform the rectification
  1.  In the DB Tree pane, select the point cloud to rectify, e.g. autzen.las.
  2. In the top tool bar, click the Translate/Rotate icon .

    The Translate/Rotate widget appears in the top right corner.
  3. In the Rotation field, select Z. Toggle off Tz.

    Note: we only want to move horizontally in the 2D plane.
  4. Look for matching features in the point cloud and the GeoTiff image.



  5. To translate the point cloud, press down the mouse right button and drag the point cloud to the new location. To rotate the point cloud, press down the mouse left button and rotate the point cloud.

    Note 1: To toggle the display of the point cloud, toggle on/off the Visible property in the Properties pane on the left.
    Note 2: To zoom in/out/pane around, pause the Translate/Rotate widget by clicking the widget's Pause icon.
  6. In the Translate/Rotate widget, click the Tick icon to save the changes.

    The point cloud is rectified.