4

I am working with this file. Kansas seems to have a self intersection:

TopologyException: Input geom 0 is invalid: Self-intersection at or near point -98.918045000000006 37.379078 at -98.918045000000006 37.379078
TopologyException: Input geom 0 is invalid: Self-intersection at or near point -98.918045000000006 37.379078 at -98.918045000000006 37.379078
TopologyException: Input geom 0 is invalid: Self-intersection at or near point -98.918045000000006 37.379078 at -98.918045000000006 37.379078

Is there a command line tool to repair this quickly and easily?

(I do not have access to QGIS.)

2

This is tested to work with your data and GDAL command line tools installed from OSGeo4W on Windows.

Test your data with ogrinfo. Escaped double quotes \"\" are needed because the layer name contains dot character.

ogrinfo -dialect sqlite -sql "select * from \"ks_kansas_zip_codes_geo.min\" where ST_IsValid(geometry)=0" ks_kansas_zip_codes_geo.min.json

Confirmed that there is one error

GEOS warning: Self-intersection at or near point -98.918045000000006 37.379078
Feature Count: 1

Create a new, fixed GeoJSON file by utilizing the ST_MakeValid function of SpatiaLite http://www.gaia-gis.it/gaia-sins/spatialite-sql-latest.html

ogr2ogr -f GeoJSON -dialect sqlite -sql "select ST_MakeValid(geometry),STATEFP10,ZCTA5CE10,GEOID10,CLASSFP10,MTFCC10,FUNCSTAT10,ALAND10,AWATER10,INTPTLAT10,INTPTLON10,PARTFLG10 from \"ks_kansas_zip_codes_geo.min\"" clean.json ks_kansas_zip_codes_geo.min.json

Check the new GeoJSON file. Ogrinfo has showed that the layer name inside GeoJSON is SELECT which is reserved keyword for SQL so it needs to be escaped.

Result:

INFO: Open of `clean.json'
      using driver `GeoJSON' successful.

Layer name: SELECT
Geometry: Unknown (any)
Feature Count: 0

You must have GDAL that is built with new enough SpatiaLite which has the ST_MakeValid function. If you do not have such version you will see this error:

ERROR 1: In ExecuteSQL(): sqlite3_prepare_v2(select ST_MakeValid(geometry),STATEFP10,ZCTA5CE10,GEOID
10,CLASSFP10,MTFCC10,FUNCSTAT10,ALAND10,AWATER10,INTPTLAT10,INTPTLON10,PARTFLG10 from "ks_kansas_zip
_codes_geo.min"):
  no such function: ST_MakeValid
  • This is an amazing and thorough answer! Do you happen to know which version of GDAL makes that possible? I have: $ gdalinfo --version GDAL 2.2.4, released 2018/03/19 And get this error. – Mittenchops Nov 30 '18 at 18:26
  • Thanks, @user30184. I'm having a lot of trouble getting a version of GDAL that allows for this, but working on it. stackoverflow.com/questions/53564543/… – Mittenchops Nov 30 '18 at 22:32
  • user30184, any chance you can post the corrected output of your Kansas json to a pastebin while I work on compiling GDAL correctly in Ubuntu? – Mittenchops Dec 1 '18 at 0:22
  • When compiling pay attention to SpatiaLite because that is the engine that runs MakeValid and it must be compiled with LWGEOM. Then GDAL must be compiled against this SpatiaLite. – user30184 Dec 1 '18 at 11:02
  • I believe this to be the correct answer, but I'm sorry I haven't accepted yet because I am unable to get these tools specially compiled on any machine I have access to. – Mittenchops Jan 8 at 23:16

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.