Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a spatialite database I created that spans several years of crime data. To facilitate a real-world scenario where I may simplify the interaction for a user by introducing a View that simply limits the time span or what types of crimes are returned, I want to know if it is possible for QGIS to read this View like it would any other spatial table in my database. The point here is that I obviously do not want to create a separate table for every possible View I may use, and I am trying to make it simple by not requiring the user to know the database and the appropriate SQL to subset the data themselves. A similar workflow in the ArcGIS world might be creating separate layer files that the user could open in ArcMap that access the database (say, SQL Server) and present only the limited data set.

I created a View in my database, but it does not show up in the list of feature classes I can import when I connect to the spatialite db in QGIS. Maybe Views aren't considered spatial? (This is something I'm going to look into later, but I thought I already did; I just don't recall the conclusion.) In any case, how might one replicate the sort of work flow I want or the similar ArcGIS alternative?

share|improve this question

2 Answers 2

up vote 8 down vote accepted

I think you have to register the view in geometry_columns table to be able to use it in QGIS.

A good resource is "Hand-writing your own Spatial VIEW" with the following example:

INSERT INTO views_geometry_columns
(view_name, view_geometry, view_rowid, f_table_name, f_geometry_column)
VALUES ('italy', 'geometry', 'ROWID', 'local_councils', 'geometry');

Anyway you must register this VIEW into the views_geometry_columns, so to make it become a real Spatial View.

share|improve this answer
    
That seems to be the issue. I created the view and checked the meta tables. Obviously, the geometry from the view isn't listed. How do I register the view's geometry? I tried the RecoverGeometryColumn but it's telling me the table doesn't exist. I thought I was on a 2.4 version, but apparently I'm working with the Spatialite 3.0 beta. Maybe I'm implementing the command wrong? Maybe it doesn't work on views? (It better!); for clarity, my command was SELECT RecoverGeometryColumn('tblName', 'SHAPE', 2226, 'POINT', 2); –  Bryan Goodrich May 9 '12 at 1:35
4  
There is a special table for handling geometry based views. Use the following syntax INSERT INTO VIEWS_GEOMETRY_COLUMNS VALUES ('{TABLE NAME}', '{GEOM COLUMN NAME}', '{GEOM TYPE}', 2, {SRID}, 0); –  Nathan W May 9 '12 at 1:53
    
example: INSERT INTO VIEWS_GEOMETRY_COLUMNS VALUES ('Towns_v1', 'Geometry', 'POINT', 2, 32632, 0); –  Nathan W May 9 '12 at 1:53
    
Use SELECT * FROM views_geometry_columns; to see what is needed to be inserted. More info at gaia-gis.it/spatialite-3.0.0-BETA/spatialite-cookbook/html/… –  Nathan W May 9 '12 at 1:55

You may also have to put a unique ID into the view, similar to what is discussed here: In PostGIS is it possible to create a View with a unique ID?

share|improve this answer
    
Unless I explicitly don't select the PK in the view creation, wouldn't a unique ID be carried along? At least for the simple select query I'm doing here. –  Bryan Goodrich May 9 '12 at 1:30
    
@BryanGoodrich- Give it a try, but it doesn't work that way with PostGIS views, as far as I have found. –  RyanDalton May 9 '12 at 14:10
    
The view was created by "SELECT * FROM crime WHERE ..." and I subset by the where-clause. This returned the PK column (rowname, which I generated from the column names in the R table I used to populate the SQLite DB). The problem is as underdark pointed out: it needs a geometry reference in the meta tables. This is what QGIS uses to identify spatial tables, and the view doesn't have any record in those meta tables. –  Bryan Goodrich May 9 '12 at 20:43
    
Good to know. Thanks for asking the question and getting a good answer –  RyanDalton May 9 '12 at 21:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.