ported recent changes from master and postgis branch