Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Retrieve Geometry type and coordinate from a spatialite database

Discussion in 'General Discussion' started by ColasN, Jul 12, 2023.

  1. ColasN

    ColasN

    Joined:
    Jun 29, 2023
    Posts:
    3
    Capture d’écran 2023-07-12 101707.png
    This is my GEOMETRY field content, it says it's MULTIPOLYGON but i don't know how to decode this.
    I've tried with a sqlite database and it return me the coordinates and the type of geometry but here i tried a lot of solutions and no one work.

    This is my code :


    using System.Data;
    using Mono.Data.Sqlite;
    using UnityEngine;
    public class SqliteExampleSimpleV2 : MonoBehaviour
    {
    void Start()
    {
    IDbConnection dbConnection = OpenDatabase("/Resources/DRD_Monumenten_Spatialite.sqlite");
    IDbCommand dbCommandReadValues = dbConnection.CreateCommand();
    dbCommandReadValues.CommandText = "SELECT objectid, naam, type, datering, GEOMETRY FROM 'drd_monum;enten_spatialite' order by GEOMETRY";
    IDataReader dataReader = dbCommandReadValues.ExecuteReader();
    while (dataReader.Read())
    {
    for (int fieldIndex = 0; fieldIndex < dataReader.FieldCount; fieldIndex++)
    {
    if (dataReader.GetName(fieldIndex) != "GEOMETRY" && dataReader.GetName(fieldIndex) == "naam")
    {
    Debug.Log(dataReader.GetName(fieldIndex) + ": " + dataReader[fieldIndex]);
    }
    else if (dataReader.GetName(fieldIndex) == "GEOMETRY")
    {
    // Get Offset
    double minX = 0;
    double minY = 0;
    IDbCommand dbCommandReadOffset = dbConnection.CreateCommand();
    dbCommandReadOffset.CommandText = "SELECT * from geometry_columns_statistics";
    IDataReader offset = dbCommandReadOffset.ExecuteReader();
    while (offset.Read())
    {
    for (int i = 0; i < offset.FieldCount; i++)
    {
    if (offset.GetName(i).Contains("min_x", System.StringComparison.OrdinalIgnoreCase))
    {
    minX = (double)offset[i];
    }

    if (offset.GetName(i).Contains("min_y", System.StringComparison.OrdinalIgnoreCase))
    {
    minY = (double)offset[i];
    }
    }

    break;
    }

    Debug.Log(dataReader.GetName(fieldIndex) + ": " + dataReader[fieldIndex]);
    // output: GEOMETRY: System.Byte[]


    }
    }
    }
    dbConnection.Close();
    }

    private IDbConnection OpenDatabase(string path)
    {
    string dbUri = "URI=file:" + Application.dataPath + path;

    IDbConnection dbConnection = new SqliteConnection(dbUri);
    dbConnection.Open();

    return dbConnection;
    }
    }


    If you have any recommendations it would help me a lot.
     
  2. ColasN

    ColasN

    Joined:
    Jun 29, 2023
    Posts:
    3
    The data are exported from QGIS !
     
  3. algio_

    algio_

    Joined:
    Jul 18, 2019
    Posts:
    86
    You have to know the encoding of the database data before talking about decoding in Unity, it may even be encoded in different steps by different applications/libraries so you have to check relative documentations. If I have to guess what's going there at least I would check the length of a single field (your console output is trimmed), if it's hex encoded floating point data you may check this
     
  4. ColasN

    ColasN

    Joined:
    Jun 29, 2023
    Posts:
    3
    It is UTF-8 encoded but when i use functions to decode it using UTF-8 decoder it return nothing