[Qgis-user] Interpolation of a geometry
Asim al-sofi
asim56 at gmail.com
Sun Jan 9 15:41:13 PST 2022
Dear Ujaval
Thank you for your reply.
This is my code snippet:
def createPointsAt(distance, geom):# creates point at certain distance
saving the joint points of the original geometry
if distance > geom.length():
distance=geom.length()
polyline=[]
polyline=polyline+geom.asPolyline()
points=[]
interpolated_polyline=[]
for f in range (0,len(polyline)-1):
interpolated_polyline=geom.fromPolylineXY([polyline[f],polyline[f+1]])#line
section of a polyline
# set the first point at startpoint
startpoint=0
point = interpolated_polyline.interpolate(startpoint)
current_distance = distance
length = interpolated_polyline.length()
# convert 3D geometry to 2D geometry as OGR seems to have problems
with this
points.append((point.asPoint()[0],point.asPoint()[1]))
while startpoint + current_distance <= length:
# Get a point along the line at the current distance
point = interpolated_polyline.interpolate(startpoint +
current_distance)
points.append((point.asPoint()[0],point.asPoint()[1]))
# Increase the distance
current_distance = current_distance + distance
# set the last point at endpoint if wanted
end = interpolated_polyline.length()
point = interpolated_polyline.interpolate(end)
points.append((point.asPoint()[0],point.asPoint()[1]))
return points
On Sat, Jan 8, 2022 at 3:13 PM Ujaval Gandhi <ujaval at spatialthoughts.com>
wrote:
> There's a tool 'Points along geometry' in the Processing Toolbox that can
> do this. Can you use that?
>
> If you are trying to do this with PyQGIS, share your code snippet so folks
> can suggest how to do it.
> [image: Logo] <https://spatialthoughts.com/>
> Ujaval Gandhi
> Spatial Thoughts
> mobile: +91-8095684687
> email: ujaval at spatialthoughts.com
> [image: LinkedIn icon] <https://www.linkedin.com/in/spatialthoughts/> [image:
> Twitter icon] <https://twitter.com/spatialthoughts>
>
>
>
> On Sat, Jan 8, 2022 at 7:22 PM Asim al-sofi <asim56 at gmail.com> wrote:
>
>> Dear all,
>> I am asking for help with interpolating a geometry.
>> I have a geometry say "*geom*" and want to add new points to it at fixed
>> distances, say "*distance*".
>> Using the method "interpolate" i.e., *geom*.interpolate(*distance*)
>> returns only points at the interpolated distance(s), meaning the geometry
>> points between the interpolated points are unknown.
>> Is there a method to insert the interpolated points into the *geom*(of
>> course at the right positions)?
>> With other words, how to create a new geometry that contains the points
>> of the old geometry and the interpolated points?
>>
>> Kind regards,
>> Asim
>> _______________________________________________
>> Qgis-user mailing list
>> Qgis-user at lists.osgeo.org
>> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
>> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20220110/7afe3bba/attachment.html>
-------------- next part --------------
def createPointsAt(distance, geom):# creates point at certain distance saving the joint points of the original geometry
if distance > geom.length():
distance=geom.length()
polyline=[]
polyline=polyline+geom.asPolyline()
points=[]
interpolated_polyline=[]
for f in range (0,len(polyline)-1):
interpolated_polyline=geom.fromPolylineXY([polyline[f],polyline[f+1]])#line section of a polyline
# set the first point at startpoint
startpoint=0
point = interpolated_polyline.interpolate(startpoint)
current_distance = distance
length = interpolated_polyline.length()
# convert 3D geometry to 2D geometry as OGR seems to have problems with this
points.append((point.asPoint()[0],point.asPoint()[1]))
while startpoint + current_distance <= length:
# Get a point along the line at the current distance
point = interpolated_polyline.interpolate(startpoint + current_distance)
points.append((point.asPoint()[0],point.asPoint()[1]))
# Increase the distance
current_distance = current_distance + distance
# set the last point at endpoint if wanted
end = interpolated_polyline.length()
point = interpolated_polyline.interpolate(end)
points.append((point.asPoint()[0],point.asPoint()[1]))
return points
More information about the Qgis-user
mailing list