Here you go. A couple of utility functions and then the meat in one function (and no for
loops :))
islines <- function(g1, g2){
## return TRUE if geometries intersect as lines, not points
inherits(gIntersection(g1,g2),"SpatialLines")
}
sections <- function(sl){
## union and merge and disaggregate to make a
## set of non-overlapping line segments
disaggregate(gLineMerge(gUnion(sl,sl)))
}
aggit <- function(sldf, attr, fun=sum){
## simplify down to SpatialLines
sl = as(sldf, "SpatialLines")
## get the line sections that make the network
slu = sections(sl)
## overlay network with routes
overs = over(slu, sl, returnList=TRUE)
## overlay is true if end points overlay, so filter them out:
overs = lapply(1:length(overs), function(islu){
Filter(function(isl){
islines(sl[isl,],slu[islu,])
}, overs[[islu]])
})
## now aggregate the required attribute using fun():
aggs = sapply(overs, function(os){fun(sldf[[attr]][os])})
## make a SLDF with the named attribute:
sldf = SpatialLinesDataFrame(slu, data.frame(Z=aggs))
names(sldf)=attr
sldf
}
lineLabels <- function(sldf, attr){
text(coordinates(gCentroid(sldf,byid=TRUE)),labels=sldf[[attr]])
}
Usage:
> r <- readOGR("airport.geojson", layer = "OGRGeoJSON")
> ag = aggit(r,"All")
> plot(ag)
> lineLabels(ag,"All")
ag
is now a spatial lines data frame with the aggregated variable. The plot lets you check it all adds up. Because your routes are a bit like a river network, you can see how the "flows" add up at the junctions:
Seems right to me...
First, you don't really want to make the lines to overlap, but only to reflect in the attributes the number of flights that are using the same path. This can be done following these steps:
Make sure you create a new column for your lines, call it "flights" and use the value 1 for all of them. I used the name "Count" which is not the best.
Then, perform a buffer operation with the desired proximity, you will have to play with the distance that you consider appropiate for your analysis: (my QGIS is in Spanish but I am sure you can figure it out)
Then, you have buffered lines that are most likely touching each other, like sausages. Your last step is to perform a spatial join, under vector, data management menu. Use the options intersect, as follows:
Make sure you add the summary option with the sum. The result will be a new column in every attribute, adding the number of other flights that are intersecting. If the lines have an ID you can join this new column to your original lines, or you can turn the buffered polygons back into lines.
If you want to simplify and remove the extra lines is a different problem, but I hope I could have helped you to start at least.
Best Answer
CASE 1. When Layers (polylines) consist of segments, i.e. the polyline is split into sections at each vertex or intersection node, see the image below.
'Layer_2'
, as well as'Layer_1'
, includes two fields, namely"id"
and"Value"
.Vector -> Data Management Tools -> Merge vector layers
. Moreover, there are other solutions available, e.g. the MMQGIS plugin does the job (MMQGIS -> Combine -> Merge layers
) or SAGA GIS Merge Layers Module from the Processing Toolbox.It will provide you with a new polyline layer that includes geometries and attributes of polylines,
'Layer_1'
and'Layer_2'
accordingly, see the graphics below. 2. Then in a new shapefile create a column that defines the overlap value of your layers, e.g. string data type with a length of1
. 3. In Expression dialogue execute the following expression'Y'
will give you all overlapping lines, and'N'
lines that do not overlap.P.S. If there is a difference in any part of the geometry, this expression fails to match them. @Kazuhito comment
As you can notice from the image below, each Layer has only two line segments where exists an overlap. Moreover, you can easily distinguish between line segments based on values from the Attribute Table.
"Value"
field in case it plays a primary role in your project.rmdupl
option from the Processing Toolbox, or the MMQGIS plugin(MMQGIS -> Modify -> Delete Duplicate Geometries
).CASE 2. When your layers contain continuous lines, see the image below.
It's just a matter of running a few more tools.
Vector -> Geoprocessing Tools -> Union
) to split features into overlapping and non-overlapping parts. The non-overlapping parts of lines will still be one multipart feature so if you want each segment to be separated, you'll need to run the Multipart to Singleparts... tool (Vector -> Geometry Tools -> Multipart to singleparts
).P.S. Answer was completed with the help of @Gabriel C..
References: