This question is similar to the "points in polygon" analysis, but I want to keep the attribute data for multiple lines within a single polygon. The data source is multiple line segments representing weather data, and I want to add the attributes of each line inside a geographic region (country shapefile). For example, the resulting polygon might have 10 rows for "India" and each row represents a unique set of weather data. Each row is the actual data from a line shapefile that falls within the "India" geography.

I've tried spatial join (and several other options), but this summarizes the datasets by polygon and does not preserve original attributes (unless I am missing something).

  • No, you're not missing something, that is what the tool does: for each polygon arbitrarily add the attributes of a line within. You might need to do some python code to get what you want.. start with lines, iterate, select the polygon(s) that intersect then add the attributes of the line to the polygon... your only other realistic choice is spatial joining your polygons to each and every line one at a time, which can be done interactively but will take a long time. – Michael Stimson Jan 10 '19 at 22:59

While point in polygon means one-to-many connection, e.g. a point is inside 0 or 1 polygon. The polyline in polygon problem is more complex, a polyline may intersect several polygons. So to do the job you should use overlay operation, not spatial join.

The overlay intersection operation creates a new layer in which the polylines are broken at the polygon borders and polygon attributes are added to the result layer. For example, the input data: enter image description here

Vector/Geoprocessing Tools/Intersection:

enter image description here

The result of intersection: enter image description here

Update Other solution to preserve polygon geometry: You can use use spatial join to create a new layer from Vector/Data Management Tools/Join Attributes by Location menu.

enter image description here

In the result layer you get repeated polygons.

  • That is true but the OP wants each polygon duplicated for every line within... can this be done interactively with attribute joining the lines to the polygons and exporting with the polygon shape? Perhaps a bit of python from here might be needed to get what the OP desires: the attributes of the intersected lines with the shape of the matching polygon. – Michael Stimson Jan 10 '19 at 23:50
  • Thanks for the help on this. However, while this produces an attribute table that is of use, the resulting layer is an "intersection" set of polylines. I'm hoping to result in a shapefile that is polygons, where each polygon has the polyline(s) that intersect with it and their resulting data. I'm not sure this is possible in QGIS. The ultimate use of this data includes spatial query where polygon shapefiles are preferable to polylines or points, but all of the original polyline(s) data needs to be included in the polygon. – ab09 Jan 11 '19 at 0:18
  • The "and their resulting data" piece is where your goal falls apart. It's trivial to invert the line-polygon list to be a polygon-line list, but it's somewhere between "not best practice" and antipattern to start adding lists to the attributes of singleton features. – Vince Jan 11 '19 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.