I have a shapefile of a city map and I have created vector grid as polygons.

After intersecting the two shapefiles, a building will map to all the grids it overlaps.

However, if I just want it to map to the one grid which contains the majority of the building (>50%), how could I achieve it?

Is there a function or plugin in gqis for this task? Or do I have to write my own code to do this?

I am new to QGIS.

  • If you want to find out which grid square has the most buildings you will need to perform some type of summary, which you might be more comfortable doing in Excel (just don't save the dbf) with a pivot table, after you have intersected your buildings with your grid then get a count of the unique grid FID values in the intersection, the largest count in the table intersects the most buildings... however, if you're trying to move your grid such that you maximise the number of buildings intersected by the centre you will need to judge that by eye. May 26 '15 at 3:36
  • @MichaelMiles-Stimson Thanks for your prompt response! But i guess my description was not so clear. The thing is that now after intersection one building maps to whichever grids it overlaps. However, my goal is to map one building to only one grid which contains more than half the area of the building. So it is like a one-to-one mapping and the criteria is more than 50% overlap.
    – Sven Wang
    May 26 '15 at 3:58
  • 1
    Ah, I see, 1 building is in 1 grid ONLY, use the centroids: make buildings into points gis.stackexchange.com/questions/24442/…, intersect points, then use those points to mark buildings as to what grid they fit in. See an answer I gave to someone else gis.stackexchange.com/questions/124374/… about buildings. In cases like these it helps to draw a picture showing what you want, it doesn't have to be a good drawing to get your intentions known. May 26 '15 at 4:05
  • @MichaelMiles-Stimson Centroids is a good solution and a quick one, but does carry the risk that either the centroid doesn't fall within the building or the grid cell with the majority of the building. And I see we already pointed this out at the question you linked to. :) In this case, it might be simpler to intersect the two layers and then extract the piece of the building with the largest area and use that grid cell.
    – Chris W
    May 26 '15 at 5:13
  • @ChrisW, that would be the safest, to find the largest piece of the building and allocate the building to that grid, however I don't know an easy way to do that in QGIS. Centroids are a quick way of ensuring that a building is assigned to only one grid but does not, as you said, ensure that it goes to the largest part. Do you know of a quick way of finding the largest part in QGIS? May 26 '15 at 5:26
  1. Intersect your building layer and grid layer (Vector > Geoprocessing Tools > Intersect). The result should be your buildings, cut up wherever they cross a grid line. Those pieces should have both the building id attribute and the grid cell id attribute.
  2. Open the attribute table use the Field Calculator to create a new field and calculate the area of each cut up building piece. The calculation is simple: $area. This method also ensures that the largest piece gets the grid ID, even if that piece is less than 50% of the total.
  3. Once you have the areas added to the table, you'll need the Group Stats plugin. With that you can use the building ID as a row (see the tutorial) and the area field as a column with the value max. Should generate a table with just one row per building, showing only the maximum area. Theoretically you'll have a grid ID in there as well that will tell you which one that building falls in. You can save out the resulting table and join it back to the buildings based on building ID to transfer the chosen grid ID.

Note this is untested, and I'm not well versed on the finer points of QGIS (particularly that plugin). This is based on how I would do it in ArcGIS and looking up comparable tools in QGIS.

  • Thanks Chris, finding the largest section was the sticking point.. I know how to do this in ArcGis but didn't know how to translate that to QGIS (or if was even possible).. I had visions of doing step 1 and 2 then using Excel to find the largest piece, save as CSV and join. 100% fits the requirement +1 from me! May 26 '15 at 21:37
  • @MichaelMiles-Stimson Going out to Excel had also come to my mind, as for some reason I keep thinking I'd read a comment somewhere that GroupStats wasn't working correctly after an update or something. I can never find it though. And must admit to not having attempted to test it myself yet.
    – Chris W
    May 27 '15 at 1:14
  • @ChrisW, you have to be careful which version of Excel you use, the 2002/2003 version has a maximum 65535 rows, the newer versions have 4 billion rows max.. and you don't find out that the table is truncated until you get bad results. There is no shame in using Excel (or equivalent), it's just another tool, though I admit I'm not an expert at it, I can usually get it to do what I want (so long as I don't ask too much). May 27 '15 at 1:23
  • @ChrisW Exactly why I could not upvote lol. But I clicked the check mark alr. Thanks again!
    – Sven Wang
    May 27 '15 at 3:38

This is not QGIS but if you don't consider OpenJUMP Plus totally off-topic, then you can do it like this:

Create a grid which has some unique ID as an attribute. Open grid layer and building layer as separate layers into a project.

enter image description here

Use the Aggregate function of OpenJUMP Plus.

enter image description here

Define that ID attribute will be aggregated by the "majority" criteria.

enter image description here

The building layer will get a new attribute which contains the data you want.

enter image description here

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.