7

Currently i'm using this DO ogrinfo -al -so to extract Projection, Feature count, Field_names for a bunch of files in batch mode. Is it possible to extract number of fields & how many fields are populated with attributes ? For eg, let us assume a point file which has 70 points and three fields - ID, Name,Type. For those 70 points i would like to extract information of how many "ID" field has values, "Name" field has values and "Type" field has values.

5

You can do everything that is possible to do with SQL also with ogrinfo by utilizing the SQLite dialect http://www.gdal.org/ogr_sql_sqlite.html.

Once you know that the problem turns into a question about how to make such a query and the answer can be found from Stackoverflow https://stackoverflow.com/questions/17194145/sql-count-based-on-column-value

The following query might work for you but remove first newlines which I added for clarity.

ogrinfo -dialect sqlite -sql "
SELECT  
COUNT(case WHEN ID is not null THEN 1 ELSE NULL END) as count_ID,
COUNT(case WHEN Name is not null THEN 1 ELSE NULL END) as count_Name,
COUNT(case WHEN Type is not null THEN 1 ELSE NULL END) as count_Type
FROM your_layer" your_datasource

I tested same kind of query with statas.shp shapefile and it seems to work. I demonstrate also the flexibility of SQLite dialect by counting the number of states which belong to SUB_REGION='Mtn' as count_2.

ogrinfo -dialect sqlite -sql "select count
(case WHEN STATE_NAME is not null THEN 1 ELSE NULL END) as count_1,
count(case WHEN SUB_REGION='Mtn' THEN 1 ELSE NULL END) as count_2
from states" states.shp
INFO: Open of `states.shp'
      using driver `ESRI Shapefile' successful.

Layer name: SELECT
Geometry: None
Feature Count: 1
Layer SRS WKT:
(unknown)
count_1: Integer (0.0)
count_2: Integer (0.0)
OGRFeature(SELECT):0
  count_1 (Integer) = 49
  count_2 (Integer) = 8
  • query works fine for batch of files with same Field_name. But our input layers has different Field_names. – joseph_k Apr 25 '16 at 10:15
  • I fear that you will need a script that reads the field names of your layer and loops and inserts those into the SQL part. Let's hope that some Python guru takes the challenge. Perhaps it will then be off-topic to this question "Using ogrinfo...". – user30184 Apr 25 '16 at 10:20

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.