-1

I'm trying to create a function for an intersection where the input file is of buildings in some urban area, and a query box is used to create an intersection containing just the buildings found in that query box area.

import matplotlib.pyplot as plt
import matplotlib as mpl
from mpl_toolkits.basemap import Basemap
import fiona
import fiona.crs
import rtree


input_file = 'se_england_clean.shp'
out_file = 'se_england_out'
file_index = 'Rtree_index_east.idx'

query_box = [-0.0957870483,51.5134165224,-0.08664608,51.5192383994]

def write_clipped_file(name_file_in, out_file, file_index):
idx = rtree.index.Index(file_index)
idx.insert(0, (input_file))
list(idx.intersection((query_box)))[0]
with fiona.open(input_file, 'w') as out_file :
    for building in idx: 
        out_file.write(building)

I get an error

 RTreeError: Coordinates must be in the form (minx, miny, maxx, maxy) or (x, y) for 2D indexes

Not sure why it's not working, i've tried different combinations of coordinates. The format of the bounding box can be found here, just use csv form and make a box over London:

http://boundingbox.klokantech.com/

I want to use a small bounding box in London and then find how many buildings from the input_file intersect within that box. Later I plan to use basemap to visualize it. Besides the query box, do I have the right idea with my code?

Basically I tried to open a new index and selected all the buildings which intersect with the query_box. The returned value should be a list of indices of the buildings that I want to visualize.

I then opened with Fiona the input shapefile and the new (clipped) shapefile that I want to produce in output. By cycling on the list of indices I tried to select the desired buildings and write them in the new file 'out_file'.

Joe Gates
  • 9
  • 2
  • Duplicate of [(Python) Rtree intersection and fiona questions](https://stackoverflow.com/questions/47299216/python-rtree-intersection-and-fiona-questions) - don't repost your question without fixing even the basic bugs. – Has QUIT--Anony-Mousse Nov 18 '17 at 10:09

1 Answers1

0

Every time someone runs a line like this:

with fiona.open(input_file, 'w') as out_file :

God kills a kitten input file.

Fortunately, you forgot to read the data before, and didn't fix the error pointed out in your previous copy of this question, so it won't get to that line.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194