I have two polygons that consist of two separate polygons/lines in QGIS). How can I make this into a multipart polygon, which consists of 1 line in QGIS?
[GIS] How to create a multipart polygon
polygonpolyline-creationqgis
Related Solutions
As adding null point as it was suggested here gave a point with (0,0) cordinates, I searched existing scripts in ESRI repository and I've found another way of separating polygon rings as well as polyline parts.
The solution is based on creating two arrays: featureVertexArray
for storing whole geometry and partVertexArray
for storing separate rings:
try:
rows = gp.SearchCursor(inFC) #open SearchCursor on input FC
row = rows.next()
outRows = gp.InsertCursor(outFC) #open InsertCursor on output FC
#create Array object which will contain features vertices
featureVertexArray = gp.CreateObject("Array")
partVertexArray = gp.CreateObject("Array")
while row:
feature = row.getValue(shapeField) #get the SHAPE field into variable
vertex = gp.CreateObject("Point") #empty Point object to store geometry
##For point geometry there is another way of reading coordinates than for polyline/polygon
if shapeType.upper() == "POINT" or shapeType.upper() == "MULTIPOINT":
#this part works right
else:
#feature can have multiple parts - first goes iteration through parts
partNum = 0
partCount = feature.PartCount
while partNum < partCount:
part = feature.GetPart(partNum) #the output is Array of points
pnt = part.next() #take first point from Array of points
#iterate through all points in array
while pnt:
#for each geometry create new POINT obj and assign swapped Y, X. Then add vertices to ARRAY
vertex.X, vertex.Y = pnt.Y, pnt.X
partVertexArray.add(vertex)
pnt = part.next()
#If pnt is null, either the part is finished or there is an interior ring
if not pnt:
pnt = part.next()
featureVertexArray.add(partVertexArray)
partVertexArray.removeAll()
#if pnt:
#print "Interior:"
featureVertexArray.add(partVertexArray)
partNum += 1 #increment part number to run loop
newFeature = outRows.newRow() #create new row in InsCur
newFeature.shape = featureVertexArray #assign ARRAY filled with points to shape field
outRows.insertRow(newFeature) #insert new row
featureVertexArray.removeAll() #clear ARRAYS before processing new geometry
partVertexArray.removeAll()
row = rows.next()
[QGIS 2.8] Use the Intersect tool menu : "Vector / Geoprocessing tools / Intersect" (from Miro's answer in comments)
[QGIS 2.18] Use the Intersection Tool in the processing toolbox/panel :
This algorithm extracts the overlapping portions of features in the Input and Intersect layers. Features in the Intersection layer are assigned the attributes of the overlapping features from both the Input and Intersect layers. Attributes are not modified.
Best Answer
For anyone coming to this with newer versions of QGIS, the function to do it is now called "Collect Geometries" rather than "Single parts to Multipart" so it's now Vector > Geometry Tools > Collect Geometries. You can then choose a field (or fields) to use as your grouping variable (Unique ID Fields). I used it to fix up some statistical areas which included non-contiguous polygons (islands in fact) where I'd ended up with 1 record for each separate polygon, but wanted 1 record for each statistical area. The reason I needed THAT was because I'm joining it to data in Tableau and ended up with a whole bunch of duplication happening around the coast due to the islands.