Home > qgis > Make many lines in one layer (Python)

Make many lines in one layer (Python)

May 21Hits:1
Advertisement

I what to create many lines with this code:

v_layer = QgsVectorLayer("LineString", "cable", "memory") pr = v_layer.dataProvider() seg = QgsFeature() seg.setGeometry(QgsGeometry.fromPolyline([line_start, geomPoint])) pr.addFeatures( [ seg ] ) v_layer.updateExtents() QgsMapLayerRegistry.instance().addMapLayers([v_layer]) 

Buth this create one layer for each of this code.

How can I create all in one layer.

Answers

you need to add all the geometries before the addMapLayers

v_layer = QgsVectorLayer("LineString", "cable", "memory")
pr = v_layer.dataProvider()
# first element
seg = QgsFeature()
seg.setGeometry(QgsGeometry.fromPolyline([line_start, geomPoint]))
pr.addFeatures( [ seg ] )
v_layer.updateExtents()
# second element
seg = QgsFeature()
seg.setGeometry(QgsGeometry.fromPolyline([geomPoint, newgeomPoint]))
pr.addFeatures( [ seg ] )
v_layer.updateExtents()
# etc
......
QgsMapLayerRegistry.instance().addMapLayers([v_layer])

so use a for loop

Related Articles

  • Make many lines in one layer (Python)May 21

    I what to create many lines with this code: v_layer = QgsVectorLayer("LineString", "cable", "memory") pr = v_layer.dataProvider() seg = QgsFeature() seg.setGeometry(QgsGeometry.fromPolyline([line_start, geomPoint])) pr.addFea

  • Can I run command-line commands within a python script?June 12

    Sorry I'm kind of a noob at this: Basically I've downloaded this package that deals with text files that is run from the terminal, but the command I need takes in two arguments. I also need to do this many times (5000+). I need to get these arguments

  • Pass information in external command line software from a python plugin fileOctober 30

    it would be quite difficult to explain, I'll do my best.. I'm writing a plugin for QGIS (with the plugin builder, so the folder structure is straightforward). This plugin calls other software (that of course have to be installed on the computer) that

  • Why does Python in Linux require the line #!/usr/bin/python?November 4

    Pretty simple question: In Linux, why does Python require the line #!/usr/bin/python at the start of a python file, since Windows doesn't? What does it do? 'cause the description "Links to Python" is a bit vague... --------------Solutions-------

  • How to print multiple lines of text with pythonJanuary 24

    If I wanted to print multiple lines of text in python without typing print('') for every line, is there a way to do that? (python 3.5.1) Thank you. --------------Solutions------------- You can use Triple quotes (single ' or double "): a = ""

  • Can't Find the Split selected features with selected line from another layer tool

    Can't Find the Split selected features with selected line from another layer toolFebruary 14

    I'm trying to split a selected polygon features with a selected line from another feature layer. I, however, can't seem to get the tool for this to show up in my advanced digitize toolbar. In other words, I need someone to tell me how to get my advan

  • no viewable feature when creating line with ogr in pythonMay 18

    the python code below produces the expected line shapefile viewable in QGIS import os from osgeo import ogr drv=ogr.GetDriverByName('ESRI Shapefile') if os.path.exists('test.shp'): os.remove('test.shp') ds=drv.CreateDataSource('test.shp') lyr=ds.Crea

  • Using Z and M feature geometry to create line graph in ArcPy/Python?June 18

    I am a Python (v2.6.5) noob trying to plot a series of 3D polylines into graphs. So far I've come up with: # Import system modules import arcpy, os, sys, string from arcpy import env print "Modules imported." # Set environment settings arcpy.env

  • How to populate a record based on the sum of selected features on a different layer, python? December 7

    So basically here is my outlined workflow. I have a point layer and a line layer. My points have a cost value associated, and my lines represent highways. All points fall on lines, therefore I would like to iterate my selection so that I can select a

  • ERROR 000800 when trying to snap points layer to line or buffer layerOctober 31

    I am very new to GIS, so go easy on me. I am using ArcCatalog 10.0 and trying to snap a set of points to another layer that has roads in it. The points represent 10th miles along the road. Here are the general steps for what i am doing: 1) create a n

  • How to type lines of command for python?February 7

    I want to encode a python command in the terminal but it executes them as I press enter to go to the next line of command. How can I prevent the terminal from executing the command until I finish typing all the command lines? --------------Solutions-

  • How can i add contrasted lines from a layer to another?

    How can i add contrasted lines from a layer to another?March 31

    Hi i have this layer : And this layer : And what i want to do is add those black lines to the red layer but i dont want the white parts , how can i do this ? --------------Solutions------------- When you put the pavement image in a new layer above th

  • Reducing lines in methods of Python class to manage tweetsApril 3

    How could I reduce the length of my functions restricting them to max 10 lines? Other comments are welcome on the code in general. import config_files import math from datetime import datetime import importlib import sys from scan_twittosphere import

  • Go to Line keyboard shortcut in Python editor IDLE?April 25

    In the Python editor IDLE I can do "Edit > Go to Line", but when I try cmd+j (alt+g on Windows), then IDLE freezes. How can I go to line in IDLE on Mac? --------------Solutions------------- IDLE > Preferences (cmd+,) > Keys > goto-li

  • Split Lines with Points Layer ToolMay 21

    I asked a question a while ago to figure out how to split a line layer with a points layer using QGIS, but cannot, however, get the accepted answer to work due to limitations of my knowledge and the fact that i'm working on Windows 7. I noticed this

  • How to merge overlapping and duplicate lines from vector layer to one single line?

    How to merge overlapping and duplicate lines from vector layer to one single line?September 24

    I used the "polygone to line" tool from QGis, to get a line-layer from this polygon-layer: unfortunatly this causes overlapping and duplicate lines, after I broke the qgis-export via GRASS into primitive lines (2 vertices). I need to have just o

  • QGIS - line between ordered layer of points - take attributes of both pointsJanuary 5

    I have a series of locations in time in a point layer. I want to make a line layer of lines between consecutive points that takes the attributes (in my case, a timestamp) of the 1 points it connects. Is there an existing plugin or script that does th

  • How to merge MANY lines within same layer that meet (join at some point)?

    How to merge MANY lines within same layer that meet (join at some point)? February 2

    I am trying to dissolve multiple lines for stream networks into individual features. The dataset contains many stream networks so a complete dissolve of the whole dataset wont do as I would like to only dissolve streams that are in the same network (

  • How to disolve lines from same layer?

    How to disolve lines from same layer?February 5

    How can I dissolve lines from the same layer so there are no borders in the junction? For example, a crossroad shouldn't have any border (not as seen in the picture). Can you help? --------------Solutions------------- You can select the option to Dis

  • Adding elevation data from contour lines to point layer using QGIS?September 14

    I want to calculate the number of vacant lots within 300m from a given building. My source file contains point layers of buildings and vacant lots. Here's a short description of my workflow. I've successfully completed steps 1 and 2. How do I complet

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.454 s.