VirtualShape C#

220 views
Skip to first unread message

Joe

unread,
Dec 8, 2009, 4:31:57 PM12/8/09
to SpatiaLite Users
I'm new to spatialite.

I need to import a number of SHP files to my database. My thought was
to create a temporary table using VirtualShape() then create a
permanent table from that. I am having problems with VirtualShape()
importing the shape file data. I've added this shape file to the map
and it has many polygons.

When I check the database after the VirtualShape() query the tmpTable
is there but there is no data and there are only two fields (PKUID,
Geometry)

The CREATE TABLE xxx AS SELECT query also succeeds but the result is
an empty table with the same two fields.

Thanks,
Joe

P.S. Are there any good C# examples out there?

public void loadShpToSqlite(string theShpFile, string
theSqliteFile, string theTableName)
{
string csCreate = @"Data Source="+theSqliteFile
+";New=True;Compress=True;Synchronous=Off";
SQLiteConnection cnn = new SQLiteConnection
(csCreate);
cnn.Open();

if (!loadSpatialiteDLL("loadShpToSqlite", cnn)) return;

initSpatialiteMetaData("loadShpToSqlite", cnn, @"D:
\Spatialite\init_spatialite-2.3.sql");

SQLiteCommand cmd = cnn.CreateCommand();
cmd.CommandText = "CREATE VIRTUAL TABLE tmpTable USING
VirtualShape('" + theShpFile + "', 'CP1252', '3071')";
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("loadShpToSqlite\nFailure to execute
Query:\n'" + cmd.CommandText + "'\n" + ex.Message);
return;
}

cmd.CommandText = "CREATE TABLE " + theTableName + " AS
SELECT * FROM tmpTable;";
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("loadShpToSqlite\nSuccess to execute
Query:\n'" + cmd.CommandText + "'");
}
catch (Exception ex)
{
MessageBox.Show("loadShpToSqlite\nFailure to execute
Query:\n'" + cmd.CommandText + "'\n" + ex.Message);
return;
}
}

Joe

unread,
Dec 9, 2009, 9:52:49 AM12/9/09
to SpatiaLite Users
I've tried inserting records in geometry_columns table before I
execute the VirtualShape query but that didn't work either.

Is 'SHAPE' the correct geometry field for shape files?

cmd.CommandText = "INSERT INTO geometry_columns VALUES ('tmpTable',
'SHAPE', 'POLYGON', 2, 3746, 1 );" +
"INSERT INTO geometry_columns VALUES
('" + theTableName + "', 'SHAPE', 'POLYGON', 2, 3746, 1 );";

Joe

a.furieri

unread,
Dec 9, 2009, 11:37:43 AM12/9/09
to SpatiaLite Users
Hi Joe,

I cannot say you nothing about C#, because I've
never used this language.

Anyway I notice some "suspect point" here and there
in your code sample:

a) loadSpatialiteDLL("loadShpToSqlite", cnn)

I suppose this has to be:
loadSpatialiteDLL("libspatialite-1.dll", cnn)

b) "CREATE VIRTUAL TABLE tmpTable USING
VirtualShape('" + theShpFile + "', 'CP1252', '3071')"

I suppose this has to be:
"CREATE VIRTUAL TABLE tmpTable USING
VirtualShape(" + theShpFile + ", CP1252, 3071)"

======


Please notice:

c) VIRTUAL TABLEs aren't at all TEMPORARY: they
are absolutely persistent tables.
You must explicitly call DROP TABLE XXX once they
are no longer required, exactly as you do for plain
(non-virtual) tables.

d) CREATE TABLE xxx AS SELECT ...
this isn't at all the best way to create and feed
a Geometry table, because you'll lost the fully
qualified Geometry column and you'll get an
unqualified generic BLOB column instead.

CREATE TABLE ... (....)
SELECT AddGeometryColumn(...)
INSERT INTO ... SELECT ... FROM ...
is by far a soundest and safest approach.

e) let alone the 'geometry_columns' table:
this is intended to be updated indirectly,
using SELECT AddGeometryColumn(...)

any attempt to manipulate directly this table
will easily produce a severely corrupted DB.

bye,
Sandro

Joe

unread,
Dec 9, 2009, 12:23:56 PM12/9/09
to SpatiaLite Users
Sandro,

Thanks for the response - I need all the help I can get.

a) loadSpatialiteDLL("loadShpToSqlite", cnn)
"loadShpToSqlite" is the calling module name for exception
handling. The procedure does as you commented:
cmd.CommandText = "SELECT load_extension
('libspatialite-1.dll')"; // or -2 depending on the version

b) I've tried : 'CP1252', '3071' & CP1252, 3071 & 'CP1252', 3071 all
combinations succeed in creating the table but there is no data for
any combination. In addition, there are no entries in the
geometry_columns table after the create succeeds.

c) I do call DROP TABLE IF EXISTS XXX for both tables at the start
of the procedure for now. I didn't include the code in the post to
save space.
Once I get data in the tmpTable, does dropping the table clean up the
geometry_columns table?


d) Thanks - I'll try this once I get data in the tmpTable
> CREATE TABLE ... (....)
> SELECT AddGeometryColumn(...)
> INSERT INTO ... SELECT ... FROM ...
> is by far a soundest and safest approach.

e) Noted - I thought because the CREATE was failing I'd try this. I
removed those lines.
> using SELECT AddGeometryColumn(...)

When I use the spatialite-gui I can both 'Load Shapefile' & 'Virtual
Shapefile' with 236 rows added in both cases. But as I noted, I need
to do this from code and I haven't succeeded yet. If I could see some
code that works it might help.

Thanks,
Joe

FObermaier

unread,
Dec 9, 2009, 2:50:31 PM12/9/09
to SpatiaLite Users
Hello Joe,

I havn't worked with the VirtualShape extension, but you may have a
look at
http://code.google.com/p/sharpmapv2, a c# mapping framework that has a
data provider for spatialite 2, which has code for creating tables and
filling them
with data.

Hth FObermaier

Joseph Wolter

unread,
Dec 9, 2009, 3:30:05 PM12/9/09
to spatiali...@googlegroups.com
I have TortoiseSVN for http://sharpmapv2.googlecode.com/svn/trunk/
sharpmapv2-read-only
but it is asking for a username / password

What's that?
> --
>
> You received this message because you are subscribed to the Google Groups "SpatiaLite Users" group.
> To post to this group, send email to spatiali...@googlegroups.com.
> To unsubscribe from this group, send email to spatialite-use...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/spatialite-users?hl=en.
>
>
>

FObermaier

unread,
Dec 10, 2009, 7:42:20 AM12/10/09
to SpatiaLite Users
I just checked out a fresh copy from
http://sharpmapv2.googlecode.com/svn/trunk/
via TortoiseSVN and was not asked for any password.
Did you, by any chance take the https://... link?

Were you prompted for a password at once or did it
pull some files and asked for a password upon pulling
files from nettopologysuite (nts).

If so, check whether extenals for nettopologysuite are set to
https://nettopologysuite.googlecode.com/svn/branches/v2.11/

If so, change https://... to http://...

Hth
FObermaier

On 9 Dez., 21:30, Joseph Wolter <joe.wol...@gmail.com> wrote:
> I have TortoiseSVN forhttp://sharpmapv2.googlecode.com/svn/trunk/
> sharpmapv2-read-only
> but it is asking for a username / password
>
> What's that?
>
> On Wed, Dec 9, 2009 at 1:50 PM, FObermaier
>

Joseph Wolter

unread,
Dec 10, 2009, 8:46:04 AM12/10/09
to spatiali...@googlegroups.com
Not being that familiar with TortoiseSVN I was trying to do an import
rather than a checkout. Now I have the files Under
D:\SharpMap\Work\trunk - Thanks.


I would just like to use the proper DLLs so I can move on - Now what do I do?

Do I have to build everything?

Are there any good instructions online for building everything?

Thanks again,
Joe

P.S. 16"-18" of snow yesterday and UW-Wisconsin closed for the first
time in more than ten years but I was foolish enough to come in to
work on this stuff.

FObermaier

unread,
Dec 10, 2009, 5:08:57 PM12/10/09
to SpatiaLite Users
There is
- the project for the dataprivider in the Providers folder of the
solution,
- a feature converter project in the demo section that is capable of
converting shapefiles to spatialite db and
- a spatialite test case, that is currently not added to the solution,
but
its there.

Feel free to ask if you have any questions.
http://sharpmap.codeplex.com might be an additional resource
concerning
that framework

Hth FObermaier

Joseph Wolter

unread,
Dec 11, 2009, 10:38:24 AM12/11/09
to spatiali...@googlegroups.com
I've downloaded the trunk and it doesn't build. Is it supposed to or
do I use as is?

I've downloaded the branches\v2.0 and it does build.

---------------- ...\Demos\DesktopClient\DemoWinForm
-----------------------------------
-- Many of the referenced DLLs can't be found (GeoAPI.Net,
ManagedBufferedCoordinate, ...)
Do I have to delete the references and reassign them?
Can't open MainForm.cs Could not find type
"SharpMap.Presentation.WinForms.MapViewControl"

-- Does not build: Error 1 The OutputPath property is not set for this
project. Please check to make sure that you have specified a valid
Configuration/Platform combination. Configuration='Debug'
Platform='AnyCPU' DemoWinForm
But they are set properly in the project properties.


------------------ \Demos\SharpMap.Demo.FeatureExporter
--------------------------------
-- Does not build: Error 1 The OutputPath property is not set for this
project. [same as above]
Many of the referenced DLLs can't be found


----------------------------------------------------------------------------------------
The other demos do build but I still need to look at the code.

Joseph Wolter

unread,
Dec 11, 2009, 12:49:11 PM12/11/09
to spatiali...@googlegroups.com
v2 DemoWinForm

I re-referenced the DLLs and now there is only one error.
Error 1 The type or namespace name 'BufferedCoordinate2D' could not be
found (are you missing a using directive or an assembly
reference?) D:\SharpMap2\Work\trunk\Demos\DesktopClient\MainForm.cs 40 43 DemoWinForm

I looked in GeoAPI and there isn't any 'BufferedCoordinate2D' is this
old code or am I missing another reference?

Joe

FObermaier

unread,
Dec 11, 2009, 3:08:50 PM12/11/09
to SpatiaLite Users
Hello Joe,
the trunk should build out of the box (apart from one test project) if
you have VisualStudio 2008.
The branches v2.0 is an old version.
Could you post the compile error output?
Maybe for questions related more related to sharpmap than to
spatialite you/we should use the forum at http://sharpmap.codeplex.com.
hth FObermaier

On 11 Dez., 18:49, Joseph Wolter <joe.wol...@gmail.com> wrote:
> v2 DemoWinForm
>
> I re-referenced the DLLs and now there is only one error.
> Error   1       The type or namespace name 'BufferedCoordinate2D' could not be
> found (are you missing a using directive or an assembly
> reference?)     D:\SharpMap2\Work\trunk\Demos\DesktopClient\MainForm.cs 40      43      DemoWinForm
>
> I looked in GeoAPI and there isn't any 'BufferedCoordinate2D' is this
> old code or am I missing another reference?
>
> Joe
>
> On Thu, Dec 10, 2009 at 4:08 PM, FObermaier
>
> <felix.oberma...@netcologne.de> wrote:
> > There is
> > - the project for the dataprivider in the Providers folder of the
> > solution,
> > - a feature converter project in the demo section that is capable of
> >  converting shapefiles to spatialite db and
> > - a spatialite test case, that is currently not added to the solution,
> > but
> >  its there.
>
> > Feel free to ask if you have any questions.
> >http://sharpmap.codeplex.commight be an additional resource

Joseph Wolter

unread,
Dec 11, 2009, 4:08:06 PM12/11/09
to spatiali...@googlegroups.com
FObermaier,

From 'How to build SharpMap v2.0 locally' at
http://sharpmap.codeplex.com/wikipage?title=Building%20SharpMap%20v2.0&referringTitle=SharpMap%20v2

TRUNK: http://sharpmapv2.googlecode.com/svn/trunk/
NTS: http://nettopologysuite.googlecode.com/svn/branches/v2.0/
GeoAPI:http://geoapi.codeplex.com/SourceControl/changeset/view/36918#/trunk/src/GeoAPI
Moved NTS and GeoAPI to trunk folder with sharpMap

I'm obviously doing something wrong because I get 68 errors

Joe
Error5777The type.doc

FObermaier

unread,
Dec 11, 2009, 5:17:08 PM12/11/09
to SpatiaLite Users
Sorry Joe, those instructions are out of date.
I suggest you delete D:\SharpMap\Work\trunk and do a fresh checkout.
That solution should build, apart from maybe one or two test projects.
If you don't have a visual studio 2008, you need to either change
lambda
expressions to delegates or us vs2008 express. If you do the last one,
the web and test projects won't be loaded, but the desktop part should
build.

If it doesn't, I'd like to see that error protocol
Hth FObermaier


On 11 Dez., 22:08, Joseph Wolter <joe.wol...@gmail.com> wrote:
> FObermaier,
>
> From 'How to build SharpMap v2.0 locally' athttp://sharpmap.codeplex.com/wikipage?title=Building%20SharpMap%20v2....
>     GeoAPI:http://geoapi.codeplex.com/SourceControl/changeset/view/36918#/trunk/...
>     Moved NTS and GeoAPI to trunk folder with sharpMap
>
> I'm obviously doing something wrong because I get 68 errors
>
> Joe
>
> On Fri, Dec 11, 2009 at 2:08 PM, FObermaier
>
> <felix.oberma...@netcologne.de> wrote:
> > Hello Joe,
> > the trunk should build out of the box (apart from one test project) if
> > you have VisualStudio 2008.
> > The branches v2.0 is an old version.
> > Could you post the compile error output?
> > Maybe for questions related more related to sharpmap than to
> > spatialite you/we should use the forum athttp://sharpmap.codeplex.com.
> > hth FObermaier
>
>
>
>  Error5777The type.doc
> 60KAnzeigenHerunterladen

FObermaier

unread,
Dec 11, 2009, 5:32:06 PM12/11/09
to SpatiaLite Users
Joe, one more thing, take the sharpmap_v2_vs2008.sln solution file,
the *_vs2005.sln file is probably not up to date.

hth fobermaier

Joseph Wolter

unread,
Dec 12, 2009, 9:36:51 AM12/12/09
to spatiali...@googlegroups.com
FObermaier,

I deleted trunk and checked out
I am using Visual Studio 2008 but I don't know what you mean by the
following statement:
> you need to either change lambda expressions to delegates ...

I've never worked with lambda statements or delegates so I don't have
a clue how to convert the lambda statements I've seen. It would be
nice if the lambda/delegates code were in #if #else #endif blocks

Of course I got 68 Errors and 403 warnings building as is.

Thanks for sticking with me through these problems,
Joe

-------------------------------------------------------------------------------------
FROM:
x => x * x

TO:
delegate int del(int i);
del myDelegate = x => x * x;
int j = myDelegate(5); //j = 25
-------------------------------------------------------------------------------------
(1) kamap.txt //??? no conversion necessary ???
$aszMapFiles = array(
"world" => array( "World", "/path/to/your/mapfile", array( 10000 ),
# in openlayers, the scale array doesn't matter. "PNG24")

-------------------------------------------------------------------------------------
(1) BeautifulSoup.py //??? no conversion necessary ???
<foo><bar>baz</bar></foo> => <foo bar="baz"><bar>baz</bar></foo>

-------------------------------------------------------------------------------------
(2) Converter.cs
foreach (IProcessFeatureDataRecords processor in realProcessors)
{
processChain = Equals(processChain, null)
? processor.Processor
:
((IEnumerable<IFeatureDataRecord> o, ref int i) =>

processor.Processor(processChain(o, ref i), ref i));
}


processChain = processChain ??
new
FeatureDataRecordProcessor((IEnumerable<IFeatureDataRecord> o, ref int
i) => o);

-------------------------------------------------------------------------------------
(7) DynamicRTreeTests.cs //same statement seven times
// Man, I can't wait.
resultsList.AddRange(rTree.Query(searchExtents, x => x.Value));


------------------------------------------------------------------------
(1) Dictionaries.cs //string builder.Append(" => "); //??? no
conversion necessary ???
public bool Show(StringBuilder stringbuilder, ref int rest,
IFormatProvider formatProvider)
{
if (rest < 0)
return false;
if (!Showing.Show(Key, stringbuilder, ref rest, formatProvider))
return false;
stringbuilder.Append(" => ");
rest -= 4;
if (!Showing.Show(Value, stringbuilder, ref rest, formatProvider))
return false;
return rest >= 0;
Error68The type.doc

Joseph Wolter

unread,
Dec 12, 2009, 9:40:39 AM12/12/09
to spatiali...@googlegroups.com
When I did a rebuild solution I got 68 Errors and 6921 Warnings before
the build failed and I don't think that has to do with the
lambda/delegate issues.

Joe

FObermaier

unread,
Dec 12, 2009, 1:48:14 PM12/12/09
to SpatiaLite Users
If you are using VisualStudio 2008 then you don't have to worry about
the lambda expressions. From the build errors it seems to me that all
the web related projects fail to build. Could you unload them from the
solution. They are:
SharpMap.Rendering.Web.Gdi
SharpMap.Rendering.Web.GdiTransGif
SharpMap.Rendering.Web.GeoJson
SharpMap.Rendering.Presentation.Web.SharpLayers

There might be other dependencies not met, try unloading those
projects until you get it compiled.

Hth FObermaier

Btw: Is the output complete?

FObermaier

unread,
Dec 12, 2009, 6:48:33 PM12/12/09
to SpatiaLite Users
Sorry Joe, for all the hustle you have had with SharpMap.
I must admit, it does not build out of the box.

The three Rendering Projects are missing references to the included
NPack project, the Presentation project does not build because the
Rendering ones did not build. That leaves a NetTopologySuite test
project which you can unload since there is anotherone, doing the
same.

I cannot add any file to this post, so I'll send a patchfile to your
google-groups account.

Hth FObermaier

On 12 Dez., 15:40, Joseph Wolter <joe.wol...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages