5

I have a vector layer tipor several polylines. I opened this layer in QGIS and exoportar to a spatial table in PostGIS. ST_AsGeoJSON want to use the function to get the GeoJSON to draw on Google Maps. I used the select below:

SELECT ST_AsGeoJSON (geom), Codlin 
   FROM "lines"; 

I noticed that in the result column st_asgeojson few lines comes in white and records that return blank geom field of the table space is blank. I think some records of my table are not georeferenced. How can I fix this error?

6

No, your records have the same SRID as the rest of the records in your table.

I've tested your code on one of my spatial tables. And, surprise! I've observed the same problem:

enter image description here

How did I've resolved? By loading the table as a layer in QGIS. Then checking the geometry validity,

enter image description here

this was the result:

enter image description here

It is true, I have a lot of other errors, but repairing the invalide shapes, I was able to eliminate those nasty white lines.


EDIT

Today I've faced again the white lines problem, working on another table. As you can see bellow, there is a white line (741):

enter image description here

No big deal, I've said, I need only to follow my own advice and to check the table geometry validity. But again, another surprise: the feature 741 is missing from the non-valid list:

enter image description here

Now really I'm in trouble! Inside QGIS, the shape is in its place, it is valid but why on earth I cannot see it in PgAdmin? I've had the idea to go to the PostGIS site and here is what I have found: pgAdmin shows no data:

Every one or two months, someone asks on the PostGIS users list usually somewhat panicked, My geometry is missing when I look in pgAdmin III.

Spatial objects can be big things and if your geometry, geography, or raster row field is big enough, pgAdmin is not going to load it and will just show an empty field. This is by design. There are a couple of ways to verify you really have data.

Ok, I've understand there isn't really a problem, but because I was able to solve some shapes by cleaning their wrong vertices, I've realize that, when you have a shape with a lot of vertices, if you are sure that it is valid, you have nothing to worry about!

I've followed the site advice and I've checked there is really data:

enter image description here

As you can see, I have 0 null geometries.

Eventually I was curious to see how really big was that shape. For this task I've used the following script:

layer = qgis.utils.iface.activeLayer()
iter = layer.getFeatures()
for feature in iter:
    geom = feature.geometry()
    if geom.type() == QGis.Line:
        x = geom.asPolyline()
        feature_vertices = len(x)
        print "Feature %d: Vertices %d" % (feature.id(), len(x))
    else:
        # other geometry type 
        print "Other geometry type" 

And here is the script running result:

enter image description here


EDIT 2

Yet what to do when the user understands why the data weren't shown, but really need to see the values behind those white lines?

In this case just export the query result into a file, and everything will be ok, as it must be:

enter image description here

CONCLUSIONS

Steps to solve similar problems:

Assure yourself that you have 0 null geometries.

if you have a data output greater than 0:
    bad luck; you have features with null geometry (but, at least, you know the real cause)
else:
    if you are not sure about the geometry validity:
        check geometry validity
        clean the excessive points avoiding intersections
        if now you can see the missing data:
            you've done a good job (in fact, your shape has not so much vertices...)
        else:
            the shape have too many vertices to be displayed in a single row 
            (optionally) you can count the vertices number
    else:
        your data are safe and ok, so why bother running all those boring tasks?
        just perform a query export as a file, and the data will be inside!
0

My layer is multiline, I changed your code to:

layer = qgis.utils.iface.activeLayer()
iter = layer.getFeatures()
for feature in iter:
    geom = feature.geometry()
    if geom.type() == QGis.Line:
        x = geom.asMultiPolyline()
        feature_vertices = len(x)
        print "Feature %d: Vertices %d" % (feature.id(), len(x))
    else:
        # other geometry type 
        print "Other geometry type"

But this time all 1 results

enter image description here

  • In your case is better to use this code: qgis.org/en/docs/pyqgis_developer_cookbook/vector.html – Sorin Călinică Feb 10 '14 at 14:01
  • Ok, I understand that due to the size of the geometry it appears in white right? But I need to return them in GeoJSON to draw in google maps api which alternative I can use? – csf Feb 10 '14 at 15:43
  • Ok, if you use pgAdmin just use this command SELECT ST_AsGeoJSON (geom), Codlin FROM "lines"; then go to File > Export as csv an that's it! Your whole lines must be there! – Sorin Călinică Feb 10 '14 at 16:07
  • So even if they do not appear in pgadmin by design, when I see my java application returns all my lines in GeoJSON: D Thank you! 0 / – csf Feb 10 '14 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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