Skip to content

Instantly share code, notes, and snippets.

@hugoledoux
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugoledoux/0798ee79fe76a1b0ed8f to your computer and use it in GitHub Desktop.
Save hugoledoux/0798ee79fe76a1b0ed8f to your computer and use it in GitHub Desktop.
are the triangles in a shapefile valid?
import sys
import os
import fiona
from shapely.geometry import Polygon
from shapely.geometry import asShape
from shapely.geometry import mapping
#-- Global variables
INFILE = "Vlak_3D_LOD0/terreinVlak_3D_LOD0.shp"
def main():
aretrianglesvalid(INFILE)
print "done."
def aretrianglesvalid(infile):
c = fiona.open(infile, 'r')
#-- Find and print the number of geometries
print "Number of features:", len(c)
#-- store the geometries in a list
lsPolys = []
for each in c:
lsPolys.append(asShape(each['geometry']))
totaltr = 0
totalinvalid = 0
for mp in lsPolys:
totaltr += len(mp)
for p in mp:
valid = True
if len(list(p.exterior.coords)) != 4:
print p.wkt
valid = False
if len(p.interiors) != 0:
print p.wkt
valid = False
if valid == False:
totalinvalid += 1
print "total triangles", totaltr
print "total invalid", totalinvalid
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment