I have been searching for an example of using SQLite\SpatiaLite, using c#. I am trying to do this in UAW, but it appears that SpatiaLite is not yet available to UWP (will it ever be?). I get an “not authorized” exception when I do the “cmd.ExecuteNonQuery();” in the code below.
When in UWP (or not), I cannot find an example of loading SpatiaLite in the C# code. I would settle for a non UWP working example if I could find one (one with an example of using SpatiaLite to do something as simple as a polygon would be great.). I am not so much interested right now in the writing and reading from the DB, as I am in using the spatial functions to complete a polygon etc.
------------------------------------------------------------
using SQLite.Net;
using SQLite.Net.Attributes;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace UWPSQLiteDemo
{
public sealed partial class MainPage : Page
{
string path;
SQLite.Net.SQLiteConnection conn;
public MainPage()
{
this.InitializeComponent();
path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path,
"LouSQlite.db");
conn = new SQLite.Net.SQLiteConnection(new
SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path);
conn.CreateTable<Customer>();
try
{
conn.EnableLoadExtension(1);
}
catch (SQLiteException e)
//catch (Exception e)
{
Debug.WriteLine("{0} enable Exception caught.", e);
}
SQLiteCommand cmd = conn.CreateCommand($"SELECT load_extension('mod_spatialite.dll')");
try
{
cmd.ExecuteNonQuery();
}
catch (SQLiteException e)
{
//https://www.sqlite.org/rescode.html - search for error code ex: "not authorized"
//(23) SQLITE_AUTH
//The SQLITE_AUTH error is returned when the authorizer callback indicates that an SQL statement being prepared is not authorized.
Debug.WriteLine("{0} execute Exception caught.", e);
}
}
I do not believe there is any way you can load a compiled extension into a UWP application. “UWP” / WinRT is what I meant by the “tablet” API.
For System.Data.SQLite – please read the web page. It is not UWP.
For the “recent enough SQLite” – please read the linked web page in my original email. It explains about the API change.
If you want spatialite functions, then you still need spatialite. Spatialite is a C/C++ library and sqlite extension. If you insist on UWP, it is going to be very hard. I’d suggest making a desktop application first.
Brad
string p1 = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string path = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine) + ";" + p1;
Environment.SetEnvironmentVariable("Path", path, EnvironmentVariableTarget.Process);
var sqliteConnection = new SQLiteConnection(sqliteConnectionString);
sqliteConnection.Open();
sqliteConnection.EnableExtensions(true);
string mod_spatialite_dllPath = p1 + @"\mod_spatialite";
sqliteConnection.LoadExtension(mod_spatialite_dllPath);
//sqliteConnection.LoadExtension(mod_spatialite_dllPath, "sqlite3_extension_init");
//sqliteConnection.LoadExtension(interopPath, mod_spatialite_dllPath);
I have no idea of what I am missing.
I am using System.Data.SQLite. I have the contents of mod_spatialite-4.3.0a-win-amd64 in my bin\x64\debug (and release) folders. My Env Path reflects this. I open my connection, IEnableExtensions(true), I try multiple ways of loading the spatialite extension, and keep getting the error "The specified procedure could not be found".
I have no idea of what I am missing.
SQLiteCommand cmd = conn.CreateCommand($"SELECT load_extension('mod_spatialite.dll')");
--
You received this message because you are subscribed to the Google Groups "SpatiaLite Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spatialite-use...@googlegroups.com.
To post to this group, send email to spatiali...@googlegroups.com.
Visit this group at https://groups.google.com/group/spatialite-users.
For more options, visit https://groups.google.com/d/optout.
I decided to try doing things differently. Instead of the
}
//test to see if load of Spatialite was successful
using (SQLiteCommand mycommand2 = new SQLiteCommand("SELECT spatialite_version();", conn))
{
//next line = red underline: cannot implicitly convert object to int, are you missing a cast?
//retVal = (mycommand2.ExecuteScalar();
// next line gets exception "input string was not in the correct format"
retVal = Convert.ToInt32(mycommand2.ExecuteScalar());
MessageBox.Show("SpatiaLite version: " + retVal.ToString());
}
--
You received this message because you are subscribed to the Google Groups "SpatiaLite Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spatialite-use...@googlegroups.com.
To post to this group, send email to spatiali...@googlegroups.com.
Visit this group at https://groups.google.com/group/spatialite-users.
For more options, visit https://groups.google.com/d/optout.
private static SqlGeometry GetGeometry(SurveySegment[] segments)
{
SqlGeometryBuilder builder = new SqlGeometryBuilder();
builder.SetSrid(0); // Don't need an SRID here
builder.BeginGeometry(OpenGisGeometryType.Polygon);
// Walk around the segments
PointF p = new PointF(0, 0);
builder.BeginFigure(p.X, p.Y);
for (int i = 0; i < segments.Length; i++)
{
p += segments[i].FromOrigin;
builder.AddLine(p.X, p.Y);
}
// Polygon's end point must be at the origin
if (p.X != 0 || p.Y != 0)
builder.AddLine(0, 0);
builder.EndFigure();
builder.EndGeometry();
// Return Geometry
return builder.ConstructedGeometry;
}
PointF[] ps = new PointF[geom.STNumPoints().Value]; While I do admit to not being a full time OOP developer, no one asked you to code my entire program. The code is many more lines than what I asked for help with. I showed the lines that were the reason that I was hoping that SpatiaLite was the tool that I could use to replace the SQLServer code. I admit to even less knowledge with Spatial\Geometry. If you feel that you already spent too much time with me, then perhaps someone should better document how to get SpatiaLite set up, it appears that many others had and have the same problems. A couple of small Visual Studio solutions (showing different ways of getting set up), that include all the Nuget packages, and current documentation as to what to pull down from where, would probably have you spending less time on these matters.
From the beginning I should have just shown those lines and asked if what they were doing could be done in SpatiaLite. Your last response (to someone who is new to these things), is cryptic to me. You say that SpatiaLite does not have a geometry builder class, and you point me to SQL. Without doing all the code that I showed, a “here’s how you would start” example would have been more helpful, then “No more, you've used up your share of my time - you have to do the programming yourself. “
Should I be looking at another tool other than SpatiaLite? If this is not a tool that can be used in UWP eventually (no one is maintaining this?), then it will be a dead end for future development. Can anyone else help me out here?
--
You received this message because you are subscribed to the Google Groups "SpatiaLite Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spatialite-use...@googlegroups.com.
To post to this group, send email to spatiali...@googlegroups.com.
Visit this group at https://groups.google.com/group/spatialite-users.
For more options, visit https://groups.google.com/d/optout.
While I do admit to not being a full time OOP developer, no one asked you to code my entire program. The code is many more lines than what I asked for help with. I showed the lines that were the reason that I was hoping that SpatiaLite was the tool that I could use to replace the SQLServer code. I admit to even less knowledge with Spatial\Geometry. If you feel that you already spent too much time with me, then perhaps someone should better document how to get SpatiaLite set up, it appears that many others had and have the same problems. A couple of small Visual Studio solutions (showing different ways of getting set up), that include all the Nuget packages, and current documentation as to what to pull down from where, would probably have you spending less time on these matters.
From the beginning I should have just shown those lines and asked if what they were doing could be done in SpatiaLite. Your last response (to someone who is new to these things), is cryptic to me.
You say that SpatiaLite does not have a geometry builder class, and you point me to SQL.
Without doing all the code that I showed, a “here’s how you would start” example would have been more helpful,
SELECT ST_AsBinary(GeomFromEWKT('SRID=-1;POLYGON((0 492,512 492,512 1004,0 1004,0 492))'));
SqlGeometry.Read MethodReads a binary representation of a geometry type into a SqlGeometry object.
Should I be looking at another tool other than SpatiaLite?
If this is not a tool that can be used in UWP eventually (no one is maintaining this?), then it will be a dead end for future development.
Can anyone else help me out here?
I have been searching for an example of using SQLite\SpatiaLite, using c#. I am trying to do this in UAW, but it appears that SpatiaLite is not yet available to UWP (will it ever be?). I get an “not authorized” exception when I do the “cmd.ExecuteNonQuery();” in the code below.
When in UWP (or not), I cannot find an example of loading SpatiaLite in the C# code. I would settle for a non UWP working example if I could find one (one with an example of using SpatiaLite to do something as simple as a polygon would be great.). I am not so much interested right now in the writing and reading from the DB, as I am in using the spatial functions to complete a polygon etc.
If my code looks rough, it is because I do not code all the time in C#.Whether UWP or not, do I need to match the version of the spatialite\mod file to the 3.12.2 as shown in the references section below. Where does that Mod file need to be? If building on x64 system needs to be x64 file? (the amd64 is confusing, is that just for AMD processors?) Is there a way to get more specific error information from SQLite?
//https://www.sqlite.org/rescode.html - search for error code ex: "not authorized"
"SqlGeometryBuilder builder = new SqlGeometryBuilder();", where the DB is not being accessed?
I have been searching for an example of using SQLite\SpatiaLite, using c#. I am trying to do this in UAW, but it appears that SpatiaLite is not yet available to UWP (will it ever be?). I get an “not authorized” exception when I do the “cmd.ExecuteNonQuery();” in the code below.
When in UWP (or not), I cannot find an example of loading SpatiaLite in the C# code. I would settle for a non UWP working example if I could find one (one with an example of using SpatiaLite to do something as simple as a polygon would be great.). I am not so much interested right now in the writing and reading from the DB, as I am in using the spatial functions to complete a polygon etc.
If my code looks rough, it is because I do not code all the time in C#.Whether UWP or not, do I need to match the version of the spatialite\mod file to the 3.12.2 as shown in the references section below. Where does that Mod file need to be? If building on x64 system needs to be x64 file? (the amd64 is confusing, is that just for AMD processors?) Is there a way to get more specific error information from SQLite?
//https://www.sqlite.org/rescode.html - search for error code ex: "not authorized"
Here is where my confusion\ignorance\newness to all this comes into play (this, and the last reply)....cmd.CommandText = "SELECT ST_Area(ST_GeomFromText('POLYGON ((0 0,0 1,1 1,1 0,0 0))'))";
double area = (double)cmd.ExecuteScalar();Are you telling me here that I MUST pull from a DB in order to do what I want? That I cannot get the user input, use Spatialite functions to calculate a polygon (draw it for display also), and calculate the area?ORAs I look at the code above, I do not see a table specified (to indicate where to get that Polygon data from). Should I infer that this is how Spatialite functions are called\executed, in that you must execute a Spatialite cmd (passing the polygon points to it)...unlike the following SQL Server code
--You received this message because you are subscribed to a topic in the Google Groups "SpatiaLite Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/spatialite-users/yEEC-gbUHOc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to spatialite-use...@googlegroups.com.
using System;
using System.Data.SQLite;
namespace spatialiteCsharp
{
class Program
{
static void Main(string[] args)
{
SQLiteConnection conn = new SQLiteConnection("Data Source=:memory:;Version=3;New=True;");
conn.Open();
conn.EnableExtensions(true);
conn.LoadExtension("mod_spatialite");
SQLiteCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT InitSpatialMetadata(1)";
I have successfully tested getting the area of the Polygon (thanks). Now I am trying to understand what is happening (yes, I am dense at times).
In the old code there is… “SqlGeometry geom = GetGeometry(segs);”. Here you are instantiating a SqlGeometry. Basically, it now exists, and I can use it to do what I need, I can refer to it at any time.
In the Spatialite world, on this page (https://www.gaia-gis.it/spatialite-2.1/SpatiaLite-manual.html#t4.2wkt), it says “Geometry (non-instantiable)”, “Polygon (instantiable)”. Ok, I cannot instantiate the Geometry (previously pointed out to me), but it says that I can instantiate the Polygon.
When I do the “cmd.CommandText = "SELECT
ST_Area(ST_GeomFromText('POLYGON ((0 0,0 1,1
> 1,1 0,0 0))'))";” and then
execute it, is that Polygon instantiated and available to me from that
point on, or do I have to keep recreating it to refer to it?
If I do something like…
cmd.CommandText = "SELECT(PolyFromText(((0 0,0 1,1 1,1 0,0 0))'))";
var P1 = cmdPolygon.ExecuteScalar();
Is that Polygon (P1), now available to me to use?
I want to draw an image of that Polygon (display it on the screen). In the old code, I would pass the instantiated geom to the code that does the drawing.
I have successfully tested getting the area of the Polygon (thanks). Now I am trying to understand what is happening (yes, I am dense at times).
In the old code there is… “SqlGeometry geom = GetGeometry(segs);”. Here you are instantiating a SqlGeometry. Basically, it now exists, and I can use it to do what I need, I can refer to it at any time.
In the Spatialite world, on this page (https://www.gaia-gis.it/spatialite-2.1/SpatiaLite-manual.html#t4.2wkt), it says “Geometry (non-instantiable)”, “Polygon (instantiable)”. Ok, I cannot instantiate the Geometry (previously pointed out to me), but it says that I can instantiate the Polygon.
When I do the “cmd.CommandText = "SELECT ST_Area(ST_GeomFromText('POLYGON ((0 0,0 1,1
> 1,1 0,0 0))'))";” and then execute it, is that Polygon instantiated and available to me from that point on, or do I have to keep recreating it to refer to it?
SELECT ST_AsBinary(GeomFromEWKT('SRID=-1;POLYGON((0 492,512 492,512 1004,0 1004,0 492))'));
- you would build the POLYGON String from your POINTS-- GeomFromEWKT will create a Spatialite-specific Geometry from that string-- ST_AsBinary: will return the Spatialite-specific Geometry to the 'Well-known Binary representation'--> check your NET classes how to convert the returned result to what you need, possibly:
If I do something like…
cmd.CommandText = "SELECT(PolyFromText(((0 0,0 1,1 1,1 0,0 0))'))";
var P1 = cmdPolygon.ExecuteScalar();
Is that Polygon (P1), now available to me to use?
I want to draw an image of that Polygon (display it on the screen). In the old code, I would pass the instantiated geom to the code that does the drawing.
SqlGeometry.Read MethodReads a binary representation of a geometry type into a SqlGeometry object.
DbGeometry polygonGeom = DbGeometry.FromText(strPolygon);
double area2 = polygonGeom.Area.Value;
I cannot find anything for being able to do this in UWP (there are not yet any Spatial classes in UWP). EF7 (renamed to "Entity Framework Core"), might have spatial someday. It is on the list of things to do, but there is no timetable for it.