Skip to content

Commit

Permalink
Small performance improvements for GeomBuilder._buildCoords (#1462)
Browse files Browse the repository at this point in the history
* Lift ndims check out of loop.

* Instantiate list at declaration.
  • Loading branch information
groutr authored and sgillies committed Nov 10, 2024
1 parent dbb4e79 commit 8491f03
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions fiona/_geometry.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,19 @@ cdef class GeomBuilder:
cdef list _buildCoords(self, void *geom):
# Build a coordinate sequence
cdef int i
cdef int npoints
cdef list coords = []

if geom == NULL:
raise ValueError("Null geom")
npoints = OGR_G_GetPointCount(geom)
coords = []
for i in range(npoints):
values = [OGR_G_GetX(geom, i), OGR_G_GetY(geom, i)]
if self.ndims > 2:
values.append(OGR_G_GetZ(geom, i))
coords.append(tuple(values))

if self.ndims == 2:
for i in range(npoints):
coords.append((OGR_G_GetX(geom, i), OGR_G_GetY(geom, i)))
else:
for i in range(npoints):
coords.append((OGR_G_GetX(geom, i), OGR_G_GetY(geom, i), OGR_G_GetZ(geom, i)))
return coords

cdef dict _buildPoint(self, void *geom):
Expand Down

0 comments on commit 8491f03

Please sign in to comment.