#!/usr/bin/env python
###############################################################################
#
# Purpose:  Test ESRI shapefile spatial index mechanism (.sbn files). This can serve
#           as a test for the functionnality of shapelib's sbnsearch.c
# Author:   Even Rouault <even dot rouault at mines dash paris dot org>
#
###############################################################################
# Copyright (c) 2012, Even Rouault <even dot rouault at mines dash paris dot org>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
###############################################################################

import sys
import os

from osgeo import ogr

###############################################################################
#

def search_all_features(lyr):

    has_null_geoms = False
    geoms = []
    lyr.SetSpatialFilter(None)
    extents = lyr.GetExtent()
    fc_ref = lyr.GetFeatureCount()

    feat = lyr.GetNextFeature()
    while feat is not None:
        geom = feat.GetGeometryRef()
        if geom is None:
            has_null_geoms = True
        else:
            geoms.append(geom.Clone())
        feat = lyr.GetNextFeature()

    # Test getting each geom 1 by 1
    for geom in geoms:
        bbox = geom.GetEnvelope()
        lyr.SetSpatialFilterRect(bbox[0], bbox[2], bbox[1], bbox[3])
        lyr.ResetReading()
        found_geom = False
        feat = lyr.GetNextFeature()
        while feat is not None and found_geom is False:
            got_geom = feat.GetGeometryRef()
            if got_geom is not None and got_geom.Equals(geom) == 1:
                found_geom = True
            else:
                feat = lyr.GetNextFeature()
        if not found_geom:
            print('did not find geometry for %s' % (geom.ExportToWkt()))
            return 'fail'

    if has_null_geoms:
        return 'success'

    # Get all geoms in a single gulp. We do not use exactly the extent bounds, because
    # there is an optimization in the shapefile driver to skip the spatial index in that
    # case.
    eps = 0.0001
    lyr.SetSpatialFilterRect(extents[0]+eps, extents[2]+eps, extents[1]-eps, extents[3]-eps)
    lyr.ResetReading()
    fc = lyr.GetFeatureCount()

    # For point layers, we need a special case since there may be points on the border
    # of the extent
    if lyr.GetGeomType() == ogr.wkbPoint:
        lyr.SetSpatialFilterRect(extents[0], extents[2]+eps, extents[0]+eps, extents[3]-eps)
        lyr.ResetReading()
        fc = fc + lyr.GetFeatureCount()

        lyr.SetSpatialFilterRect(extents[1]-eps, extents[2]+eps, extents[1], extents[3]-eps)
        lyr.ResetReading()
        fc = fc + lyr.GetFeatureCount()

        lyr.SetSpatialFilterRect(extents[0], extents[2], extents[1], extents[2]+eps)
        lyr.ResetReading()
        fc = fc + lyr.GetFeatureCount()

        lyr.SetSpatialFilterRect(extents[0], extents[3]-eps, extents[1], extents[3])
        lyr.ResetReading()
        fc = fc + lyr.GetFeatureCount()

    if fc != fc_ref:
        print('layer %s: expected %d. got %d' % (lyr.GetName(), fc_ref, fc))
        return 'fail'

    return 'success'

if __name__ == '__main__':

    if len(sys.argv) != 2:
        print('Usage: python testsbn.py a_shapefile_with_a_sbn.shp')
        sys.exit(1)

    name = sys.argv[1]
    ext = name[-3:]
    if ext != 'shp' and ext != 'SHP':
        print('Usage: python testsbn.py a_shapefile_with_a_sbn.shp')
        sys.exit(1)

    basename = name[0:-3]

    sbn_filename = basename + 'sbn'
    try:
        os.stat(sbn_filename)
    except:
        sbn_filename = basename + 'SBN'
        try:
            os.stat(sbn_filename)
        except:
            sbn_filename = None

    qix_filename = basename + 'qix'
    try:
        os.stat(qix_filename)
    except:
        qix_filename = None

    if sbn_filename is None and qix_filename is None:
        print('%s has no accompaying sbn file' % name)
        sys.exit(1)

    if sbn_filename is not None and qix_filename is not None:
        print('%s has both an accompaying sbn and qix file. Only the qix will be used' % name)

    if sbn_filename is None and qix_filename is not None:
        print('%s has only a qix file. Testing it' % name)

    if sbn_filename is not None and qix_filename is None:
        print('Testing accompaying sbn file.')

    ds = ogr.Open(name)
    if ds is None:
        print('Cannot open ' + name)
        sys.exit(1)

    lyr = ds.GetLayer(0)
    print(search_all_features(lyr))

