I have a vector layer of about 62km^2. I have a vector layer of roads within that area. How do I calculate the road density (km/km2)? I did a union between the road layer and the area layer, then I calculate the total line length in the area? I'm working in QGIS.
Assuming that the polygon and road layers cover the same extent, and are projected to the same Coordinate Reference System (that uses feet or meters as the unit of measure), no overlay operation, such as Union, is required. Simply (1) calculate the total road length (feet, miles, km, etc), and (2) calculate the total polygon area, using the same units of measure (square feet, miles, km). Then divide length by area to get road density.
In the menu
Vector > Analysis tools
there's function sum line lengths,
an algorithm that "takes a polygon layer and a line layer and measures the total length of lines and the total number of them that cross each polygon. The resulting layer has the same features as the input polygon layer, but with two additional attributes containing the length and count of the lines across each polygon. The names of these two fields can be configured in the algorithm parameters."
Then you just add an area column with the field calculator and divide the length column by it.