I have downloaded a shape file of Chicago building footprints from here. Unzipped it and ran
ogr2ogr -f csv -dialect sqlite -sql "select AsGeoJSON(geometry) AS geom, * from 'geo_export_98941210-c995-4502-8ebb-93a09f29d615'" chicago_building_footprints.csv geo_export_98941210-c995-4502-8ebb-93a09f29d615.shp
Why do I get the following error? Have no issues with several other shape files downloaded from the same site. Using GDAL 2.2.3, released 2017/11/20 on Ubuntu 18.04.2.
ERROR 1: Cannot create virtual table for layer
'geo_export_98941210-c995-4502-8ebb-93a09f29d615' : CREATE VIRTUAL:
invalid SQL statement : CREATE TABLE
"geo_export_98941210-c995-4502-8ebb-93a09f29d615"("date_bldg_"
DATE,"time_bldg_" VARCHAR(254),"bldg_condi" VARCHAR(254),"date_bldg_"
DATE,"time_bldg_" VARCHAR(254),"date_bldg_" DATE,"time_bldg_"
VARCHAR(254),"bldg_id" FLOAT,"bldg_name1" VARCHAR(254),"bldg_name2"
VARCHAR(254),"bldg_sq_fo" FLOAT,"bldg_statu" VARCHAR(254),"cdb_city_i"
VARCHAR(254),"comments" VARCHAR(254),"date_condi" DATE,"time_condi"
VARCHAR(254),"create_use" VARCHAR(254),"date_demol" DATE,"time_demol"
VARCHAR(254),"date_edit_" DATE,"time_edit_" VARCHAR(254),"edit_sourc"
VARCHAR(254),"edit_useri" VARCHAR(254),"f_add1" FLOAT,"footprint_"
VARCHAR(254),"harris_str" VARCHAR(254),"label_hous"
VARCHAR(254),"no_of_unit" FLOAT,"no_stories" FLOAT,"non_standa"
VARCHAR(254),"orig_bldg_" FLOAT,"pre_dir1" VARCHAR(254),"date_qc_da"
DATE,"time_qc_da" VARCHAR(254),"qc_source" VARCHAR(254),"qc_userid"
VARCHAR(254),"shape_area" FLOAT,"shape_len" FLOAT,"st_name1"
VARCHAR(254),"st_type1" VARCHAR(254),"stories" FLOAT,"suf_dir1"
VARCHAR(254),"t_add1" FLOAT,"unit_name" VARCHAR(254),"vacancy_st"
VARCHAR(254),"x_coord" FLOAT,"y_coord" FLOAT,"year_built"
FLOAT,"z_coord" FLOAT,OGR_STYLE VARCHAR HIDDEN,"GEOMETRY"
BLOB_POLYGON_XY_4326, OGR_NATIVE_DATA VARCHAR, OGR_NATIVE_MEDIA_TYPE
VARCHAR) ERROR 1: In ExecuteSQL(): sqlite3_prepare_v2(select
AsGeoJSON(geometry) AS geom, * from
'geo_export_98941210-c995-4502-8ebb-93a09f29d615'): no such table:
geo_export_98941210-c995-4502-8ebb-93a09f29d615
Best Answer
That site seems to deliver an invalid shapefile that has duplicate field names in the attributes. The shapefile is also invalid for another reason according to the shapefile specification because the .dbf part is more than 6 GB in size while the standard allows only 2 GB. Technically the .dbf part is valid and GDAL can handle that but a workaround is needed for fixing the duplicate field names.
The error message that you got does not tell the real reason for the failure but I reused the CREATE TABLE part with spatialite-gui which prints a clear message:
Next I verified that with ogrinfo:
The workaround is to let GDAL to convert all the data into some suitable format like GeoPackage or SpatiaLite. Because there seems to be some problem with GeoPackage and your workflow with current software versions you'd better use Spatialite as interim format for fixing the data.
Now your command will work with some changes:
Test the command with a couple of features:
Chect the result
Looks perfect so remove
limit 2
and run the command again.Please contact the City of Chicago and ask them to stop delivering invalid shapefiles. I would recommend them to use GeoPackage format instead.