# Buffering in pyQGIS? Task: Find all countries that are in the range of missiles

I am beginning user of pyQGIS and I have task to do. I need to find all cities outside country called R, which are in range of missiles based in city K. The range of missiles is 250 km. Missiles located 15 km inside the boundary of the city K. I loaded Layers:

I. Cities:

``````layer = QgsVectorLayer("C:\cities.shp","miasta", "ogr")
layer.isValid()
``````

II. Countries:

``````layer2 = QgsVectorLayer("C:\country.shp","panstwa","ogr")
layer.isValid()
``````

Then, I reaveled all cities outside R:

``````for f in layer.getFeatures():
geom = f.geometry()
if f['CNTRYNAME'] != 'R':
print f['NAME']
``````

And their coordinates:

``````for f in layer.getFeatures():
geom = f.geometry()
if f['CNTRYNAME'] != 'R':
print geom.asPoint()
``````

Now I don't know how to find out how many cities (except ones in R) are in the range of missiles.

How to create buffer which is 15 km from the border and how can i find all cities 250 km from that buffer? Maybe there is other way of doing that task?

I also have to find time that the missile travels from boarder of Kaliningrad to Lublin, Poland and ellipsoidal distance. Missile's speed = 2.5 Ma (850 m/s) I have no idea what to do next.

• So...you want help from the online community to identify which parts of the world can be blown up by these missiles? I think I'm going to flag this question. – user35594 Jun 12 '17 at 19:35
• Actually my professor seemed suspicious form the beggining, but i never tought he could be terrorist. ;) – Joanna Jun 12 '17 at 19:45
• @user35594 Why do you think this is for people to have cities blown up, and not the reverse of identifying those that may be at risk? – Midavalo Jun 12 '17 at 20:03
• I'm voting to close this question as off-topic because it is provocative. – FelixIP Jun 12 '17 at 20:45
• @FelixIP Honestely I haven't seen anything provocative in this question, it's just simple task from my university and I need to solve it. You can change missiles for planes if that bothers you. ;) – Joanna Jun 12 '17 at 20:58

Whatever the reactions that this issue causes, the following code should do the trick (test in the python console):

``````selection = []
for f in country.getFeatures(): #get the russian country
geom = f.geometry()
buffer = geom.buffer(235000,25) #buffer the russian country by 235 kms
diff_buffer = buffer.difference(geom) #create an hole to exclude russian country
for c in city.getFeatures(): # get the cities
if c.geometry().intersects(diff_buffer): # perform the intersection
selection.append(c.id()) # add the city which intersects to a list
city.setSelectedFeatures(selection) # select the cities which intersects
``````

where country is your layer with russian country as a polygon and city is your layer containing your cities. Here I just select the matching cities but you can do what you want as they are stored in a list!