Forum

Author Topic: Get Shape coordinates in python  (Read 4452 times)

ashalota

  • Jr. Member
  • **
  • Posts: 93
  • Forest orthomosaics, long transects (300m agl)
    • View Profile
    • NASA: G-LiHT (Public orthomosaics)
Get Shape coordinates in python
« on: April 16, 2020, 12:36:44 AM »
I'm trying to extract the coordinates of the Points/Vertices of a Shape in python. Using 1.6.2 Pro

I can access the individual shape (Polygon). From here, I'm not sure how to get useful data from this.


Code: [Select]
In [119]: chunk.shapes.shapes[0].vertex_ids
Out[119]: 2020-04-15 17:34:31 Shape.Vertices([95, 96, 97, 98, 99, 100, 101, 102])

In [120]: chunk.shapes.shapes[0].vertices

In [121]:

What do I do with the vertex id to access the data in that vertex?

My goal is to be able to draw a bounding rectangle around the shapes that I drew in. So I need to get the contents of my shape and be able to pull the values. I know there is the exportShape option that will then export it as geoJson which I can later read, but I'd rather skip that in between step.
« Last Edit: April 16, 2020, 12:52:35 AM by ashalota »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14855
    • View Profile
Re: Get Shape coordinates in python
« Reply #1 on: April 16, 2020, 01:23:48 AM »
Hello ashalota,

I think that this shape has markers attached to its vertices. So the vertex locations are defined by the markers' projections on the individual images.

In this case to get the coordinates to should check the .position of the related markers with the required .key value. The following code should give you the coordinates of the vertices, providing that the chunk is georeferenced:
Code: [Select]
vertices =  [chunk.crs.project(chunk.transform.matrix.mulp(i.position)) for i in chunk.markers if i.key in s.vertex_ids]
Best regards,
Alexey Pasumansky,
Agisoft LLC

ashalota

  • Jr. Member
  • **
  • Posts: 93
  • Forest orthomosaics, long transects (300m agl)
    • View Profile
    • NASA: G-LiHT (Public orthomosaics)
Re: Get Shape coordinates in python
« Reply #2 on: April 16, 2020, 01:28:29 AM »
Worked perfectly. Would be helpful if this was explained more in the next python API manual. I think the section for "Vertex" doesn't lead anywhere right now.

ashalota

  • Jr. Member
  • **
  • Posts: 93
  • Forest orthomosaics, long transects (300m agl)
    • View Profile
    • NASA: G-LiHT (Public orthomosaics)
Re: Get Shape coordinates in python
« Reply #3 on: April 16, 2020, 01:30:00 AM »
What would be the steps to then re-transform my output into the format needed to create a new shape?

So, if I did some math on my end and have a new list of Vector([x,y,z]), and want to use that to make a shape in the same chunk?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14855
    • View Profile
Re: Get Shape coordinates in python
« Reply #4 on: April 16, 2020, 11:50:13 AM »
Hello ashalota,

If you have the list of vectors (v_list), then you can create a new shape in the following way:

Code: [Select]
     if not chunk.shapes:
          chunk.shapes = Metashape.Shapes()
          chunk.shapes.crs = chunk.crs
     shape = chunk.shapes.addShape()
     shape.type = Metashape.Polygon
     shape.has_z = True
     shape.vertices = [v for v in v_list]
Best regards,
Alexey Pasumansky,
Agisoft LLC

ashalota

  • Jr. Member
  • **
  • Posts: 93
  • Forest orthomosaics, long transects (300m agl)
    • View Profile
    • NASA: G-LiHT (Public orthomosaics)
Re: Get Shape coordinates in python
« Reply #5 on: April 17, 2020, 12:50:39 AM »
What are the steps to de-project my vertex coordinates back to the chunk crs?

i.e. the reverse of

Code: [Select]
[utmCrs.project(chunk.transform.matrix.mulp(i.position))

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14855
    • View Profile
Re: Get Shape coordinates in python
« Reply #6 on: April 17, 2020, 02:06:08 AM »
Hello ashalota,

If shape vertices do not have attached coordinates, then the shape.vertices list should contain the coordinates in the geographic coordinate system.
Best regards,
Alexey Pasumansky,
Agisoft LLC

ashalota

  • Jr. Member
  • **
  • Posts: 93
  • Forest orthomosaics, long transects (300m agl)
    • View Profile
    • NASA: G-LiHT (Public orthomosaics)
Re: Get Shape coordinates in python
« Reply #7 on: April 17, 2020, 02:19:05 AM »
With the code you gave me, I receive my coordinates then in chunk.crs (in my case as lat/lon):


Code: [Select]
[Vector([-150.24736455162093, 62.6811634594868, 223.03704190194733])]

Then I do some math on my end to convert it to my UTM zone (so I can do math in meters on it):

Code: [Select]
Vector([640915.0846823752, 6953074.436939923, 223.03704190194733])
I make some changes to the value (in this case creating a bounding coordinate, shown below is one of those points:

Code: [Select]
Vector([640716.2281598446, 6952876.404872894, 224.2336939337642])
Now I want to add that last item back into my chunk as a shape. I'm unclear on what projection math I have to do to put it back into chunk.crs

I have a reference to my Metashape.CoordinateSystem objects:

chunk.crs ==  == <CoordinateSystem 'WGS84 (EPSG::4326)'>
utmCrs == <CoordinateSystem 'WGS 84 / UTM zone 5N (EPSG::32605)'>


----

I think I just figured this part out. For reference, this worked for reprojecting my values (assume my vertices from utm zone 5n are stored in bbox_vertices:

Code: [Select]
bbox_latlon_vertices = [chunk.crs.project(utmCrs.unproject(i)) for i in bbox_vertices]
« Last Edit: April 17, 2020, 02:24:43 AM by ashalota »

ashalota

  • Jr. Member
  • **
  • Posts: 93
  • Forest orthomosaics, long transects (300m agl)
    • View Profile
    • NASA: G-LiHT (Public orthomosaics)
Re: Get Shape coordinates in python
« Reply #8 on: April 17, 2020, 02:33:00 AM »
Hello ashalota,

If you have the list of vectors (v_list), then you can create a new shape in the following way:

Code: [Select]
    [...]
     shape.type = Metashape.Polygon
    [...]

Just a note, 1.6.2 uses the format : Metashape.Shape.Type.Polygon , the above was not valid.