1

I'm trying to intersect a buffer with a point shapefile using OGR, but when I run it, I get this error:

Traceback (most recent call last):

File "C:\Users\xxx\eclipse-workspace\xxx\Final.py", line 40, in <module>

buff_lyr.Intersection (shp2, result_lyr)

File "C:\Python27\ArcGIS10.5\lib\site-packages\osgeo\ogr.py", line 2263, in Intersection

return _ogr.Layer_Intersection(self, *args, **kwargs)

 TypeError: in method 'Layer_Intersection', argument 2 of type 'OGRLayerShadow *'

Here is my script:

import ogr

import os

import sys

path = 'C:/Users/xxx/Desktop/xxx/xxx/xxx/archivos'

os.chdir(path)

driverName = "ESRI Shapefile"

driver = ogr.GetDriverByName(driverName)

shp = 'avereproj.shp'

dataSource = driver.Open(shp, 0)

shp2 = 'Cober_point3.shp' 

dataSource2 = driver.Open(shp2, 0)

numLayers = dataSource.GetLayerCount() 

print 'Numero de capas: ', numLayers 

layer = dataSource.GetLayer(0) 

numFeatures = layer.GetFeatureCount() 

print 'Numero de registros: ',numFeatures

spatialRef = layer.GetSpatialRef()

print spatialRef

memory_driver = ogr.GetDriverByName ('memory')

memory_ds = memory_driver.CreateDataSource ('temp')

buff_lyr = memory_ds.CreateLayer ('buffer')

buff_feat = ogr.Feature (buff_lyr.GetLayerDefn())

for ave_feat in layer:

    buff_geom = ave_feat.geometry().Buffer(100)

    tmp = buff_feat.SetGeometry(buff_geom)

    tmp = buff_lyr.CreateFeature (buff_feat)

result_lyr = memory_ds.CreateLayer('result')

buff_lyr.Intersection (shp2, result_lyr)

print ('Polygons: []'.format(result_lyr.GetFeatureCount()))

I have been trying to figure out how to fix this for a while but I can't find the problem.

Can you help me?

1

OGRGeometry::Intersection method takes a geometry object as input, but you're passing it a string.

You passed shp2 to dataSource2, but you're not using it anywhere in your code. Extract the layer from it, then the desired feature from that, and finally the feature's geometry, and pass that to Intersection. Also, Intersection takes only one arguement, assign the result to a variable outside it:

result_lyr = buff_lyr.Intersection(geom2)
  • Thank you. That got rid of that error. I extracted the geometry following these steps: - shp2 = 'Cober_point2.shp' - dataSource2 = driver.Open(shp2, 0) - lyr2 = dataSource2.GetLayer(0) - feature2 = lyr2.GetFeature(0) - geometry2 = feature2.GetGeometryRef() But I get - 'NoneType' object has no attribute 'GetGeometryRef'. I have read that that could be because the geometry was empty, but when I import it to arcGIS, there are points in it. So I don't know what could be wrong now. I will keep searching. Thank you for your quick answers. – A.T. Jan 12 '18 at 10:27
  • Is your Layer being imported correctly? When you do lyr2.GetFeatureCount(), how many features it returns? Is the feature 0 really the one you want? It may be that your data has geom-less features that you are unaware of. – Roberto Ribeiro Jan 12 '18 at 11:35
3

The intersection method is expecting a layer but you are giving a string containing the Shapefile name.

You can try buff_lyr.Intersection (dataSource2.GetLayer(0) , result_lyr)

  • Close, but not quite right. Intersection does not take a Layer object, but a Geometry one. And it only takes one input argument. – Roberto Ribeiro Jan 12 '18 at 5:08
  • @RobertoRibeiro ... though the solution I have provided do work. – JGH Jan 12 '18 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.