def _validate_geometry(geom): if geom is not None: simple = models.DBSession.query(func.ST_IsSimple(geom)).scalar() if not simple: raise TopologicalError("Not simple") valid = models.DBSession.query(func.ST_IsValid(geom)).scalar() if not valid: reason = models.DBSession.query(func.ST_IsValidReason(geom)).scalar() raise TopologicalError(reason)
def _validate_geometry(self, geom): validate = self.settings.get("geometry_validation", False) if validate and geom is not None: simple = DBSession.query(func.ST_IsSimple(geom)).scalar() if not simple: raise TopologicalError("Not simple") valid = DBSession.query(func.ST_IsValid(geom)).scalar() if not valid: reason = DBSession.query(func.ST_IsValidReason(geom)).scalar() raise TopologicalError(reason)
def geom_invalid(table, index=None): """ Return DataFrame with information on records with invalid geometry. Returned columns include record identifier, whether geometry is simple, and reason for invalidity. Parameters ---------- table : sqlalchemy.ext.declarative.DeclarativeMeta Table ORM class to diagnose. index : sqlalchemy.orm.attributes.InstrumentedAttribute, optional Column ORM object to use as index. Returns ------- df : pandas.DataFrame """ # Build list of columns to return, including optional index. columns = [func.ST_IsValidReason(table.geom).label('reason'), table.geom] if index: columns.append(index) # Query information on rows with invalid geometries. with db.session() as sess: q = sess.query( *columns ).filter( ~table.geom.ST_IsValid() ) # Convert query to DataFrame. if index: df = io.db_to_df(q, index_col=index.name) else: df = io.db_to_df(q) return df