Coder Social home page Coder Social logo

Comments (4)

sgillies avatar sgillies commented on July 24, 2024

Can you be more specific? Do you mean an attribute filter or something else?

from fiona.

bekozi avatar bekozi commented on July 24, 2024

Below is a code example. It amounts to an attribute filter (there may be a better way to do it). Fiona is perfect for my application except for iteration over large shapefiles when only a small subset of the geometries are actually needed.

from osgeo import ogr
from shapely import wkb

shp_path = 'foo.shp'
select_ugid = [1,2,3]
ds = ogr.Open(shp_path)
try:
    lyr = ds.GetLayerByIndex(0)
    lyr.ResetReading()
    if select_ugid is not None:
        lyr_name = lyr.GetName()
        ## format where statement different for singletons
        if len(select_ugid) == 1:
            sql_where = 'UGID = {0}'.format(select_ugid[0])
        else:
            sql_where = 'UGID IN {0}'.format(tuple(select_ugid))
        sql = 'SELECT * FROM {0} WHERE {1}'.format(lyr_name,sql_where)
        features = ds.ExecuteSQL(sql)
    else:
        features = lyr

    geoms = [None]*len(features)
    for idx,feature in enumerate(features):
        attrs = feature.items()
        attrs.update({'geom':wkb.loads(feature.geometry().ExportToWkb())})
        geoms[idx] = attrs
finally:
    ds.Destroy()

from fiona.

sgillies avatar sgillies commented on July 24, 2024

Understood. According to http://www.gdal.org/ogr/drv_shapefile.html unless you've made an attribute index for UGID, your code above is functionally equivalent to this:

import fiona

def select_ugid(f):
    return f['properties']['UGID'] in [1, 2, 3]

with fiona.open('foo.shp') as c:
    features = filter(select_ugid, c)

Because of that and because I don't want to add any SQL to Fiona interfaces, I won't expose OGR's ExecuteSQL in Fiona.

from fiona.

bekozi avatar bekozi commented on July 24, 2024

Thanks, Sean. This is very helpful! I went ahead and closed the ticket...

from fiona.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.