# Forum

### Author Topic: Select Camera by polygon  (Read 1099 times)

#### masopiq

• Newbie
• Posts: 8
##### Select Camera by polygon
« on: June 19, 2024, 08:43:28 AM »
Please help me to fix thos code to select camera inside polygon, i am running Metashape 2.1 :

#import Metashape

def point_inside(point, poly):

x, y = point.x, point.y
inside = False

p1x, p1y = poly[0]
for i in range(len(poly) + 1):
p2x, p2y = poly[i % len(poly)]
if y >= min(p1y, p2y):
if y <= max(p1y, p2y):
if x <= max(p1x, p2x):
if p1y != p2y:
xinters = (y - p1y) * (p2x - p1x) / (p2y - p1y) + p1x
if p1x == p2x or x <= xinters:
inside = not inside
p1x, p1y = p2x, p2y
return inside

doc = Metashape.app.document
chunk = doc.chunk
shapes = chunk.shapes
crs = shapes.crs
T = chunk.transform.matrix
polygons = dict()

for shape in shapes:
if not shape.selected: #skipping not selected shapes
continue
if shape.geometry.type == Metashape.Geometry.PolygonType:
polygons[shape] = [[v.x, v.y] for v in shape.geometry.coordinates[0]]

for camera in chunk.cameras:
camera.selected = False
if not camera.transform:
if camera.reference.location:
camera_coord = crs.project(chunk.crs.unproject(camera.reference.location))
else:
continue
else:
camera_coord = crs.project(T.mulp(camera.center))
for shape in polygons.keys():
if point_inside(Metashape.Vector([camera_coord.x, camera_coord.y]), polygons[shape]):
camera.selected = True

print("Script finished")

#### Alexey Pasumansky

• Agisoft Technical Support
• Hero Member
• Posts: 15029
##### Re: Select Camera by polygon
« Reply #1 on: June 20, 2024, 05:27:17 PM »
Hello masopiq,

What problem do you have using this script? It worked for me on some random project in 2.1 version.

If you are running it via Run Script command, make sure to uncomment the first line:
Code: [Select]
`import Metashape`
Best regards,
Alexey Pasumansky,
Agisoft LLC