Let's say there's point layer A with 300 points and point layer B with one point
How to create a line layer that connects all points in layer A to the one point in layer B?
Using QGIS and/or PostGIS.
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.Sign up to join this community
In QGIS, you can use the Connect Points plugin which you can download from:
Plugins > Manage and Install Plugins...
Here are a couple of layers,
layer_A has a bunch of points;
layer_B has one. Make sure both layers contain an integer field where the values are exactly the same (e.g. both my layers have an
id field where all values are
1). The plugin uses this to connect your points. When your plugin is enabled, go to its settings:
Note that this plugin is experimental so you will need to enable the
Show also experimental plugins option (thanks to @blue_chip):
Plugins > Manage and Install Plugins > Settings
if both of your layers are points this below query should work just plug in your own data. I did a test with creating lines from 1 layer with 150+ point rows and a layer with 1 point
drop table if exists line; create table line as select layer1.id ,st_makeline(layer1.geom,point.geom) as geom from layer1,point
Assuming to start from this situation (one point layer with one feature and one point layer with 300 features):
you may run this code from the Python Console (after having loaded the two layers of interest in QGIS):
from qgis.core import * from qgis.PyQt.QtCore import QVariant layer1 = QgsMapLayerRegistry.instance().mapLayersByName('1point') crs = layer1.crs().toWkt() layer2 = QgsMapLayerRegistry.instance().mapLayersByName('300points') outLayer = QgsVectorLayer('Linestring?crs='+ crs, 'line_output' , 'memory') prov = outLayer.dataProvider() fields = layer1.pendingFields() for field in layer2.pendingFields(): fields.append(field) prov.addAttributes(fields) outLayer.updateFields() for feature in layer1.getFeatures(): coords = feature.geometry().asPoint() attr1 = feature.attributes() for feat in layer2.getFeatures(): seg_start = coords seg_end = feat.geometry().asPoint() attr2 = feat.attributes() attrs = attr1 + attr2 outGeom = QgsFeature() outGeom.setGeometry(QgsGeometry.fromPolyline([seg_start, seg_end])) outGeom.setAttributes(attrs) prov.addFeatures([outGeom]) QgsMapLayerRegistry.instance().addMapLayer(outLayer)
for obtaining this result:
You only need to adapt the names for the layers (
'300points') to the names with which they are loaded in the Layers Panel.
My approach will work independently from the number of features in both layers.