lane_merger module¶
- class lane_merger.SingleLane¶
Bases:
object
The class of lane
Definition:
Each lane consists of 2 lane boundaries. In general, along with the direction of the road (or lane), the right_laneboundary is on the right side and left_lane boundary is on the left side, which is defined to be SingleLane.right_lane and SingleLane.left_lane respectively. By the way, the judgement of lane direction is yet to be implemented for no necessity.
Each lane belongs to a road with a specific road_id and a lanegroup with a specific lanegroup_id.
Any attribute that comes with the suffix _m is converted to unit of meter, e.g., SingleLane.left_lane_m and SingleLane.area_m
Here is the description of some attributes:
- left_lane: The geometry features of a laneboundary, probably
a list of points in order
- left_lane_m: Same as above, but the unit of each point is
converted to meter
- area: Also a list of points, consists of left_lane and
right_lane. Be awared that the sequence of left_lane is reversed, to apply to the definition of polygon that the points are arranged counter clockwise
- area_m: Same as above, but the unit of each point is converted
to meter
left_geoline: shapely geometry features, shapely.Linestring type
geo_area: Also shapely geometry features, shapely.Polygon type
- lane_merger.close_db(cur, con)¶
Check if the cursor of database and connection with database are closed, if not, close them.
- lane_merger.convert_str_to_float(str_list)¶
This function is designed for geometry field of table.
Note that the information of z-axis is ignored, convert those digits from type str to float, then store to a list.
- Parameters
str_list – A str type string, contains the message of geometry.
Examples
>>> str = 'SRID=4326;LINESTRING(116.6264870390295 39.79060979560018 0, 116.6265049763024 39.79063259437680 0)' >>> out = convert_str_to_float(str) >>> out [(116.6264870390295 39.79060979560018), (116.6265049763024 39.79063259437680)]
- lane_merger.get_args_from_terminal()¶
Get arguments from terminal
- Parameters
road_id – The road_id you want
lanegroup_id – The lanegroup_id you want
- Returns
Arguments you get from terminal
- lane_merger.get_field_from_table(cur, table, **kwargs)¶
Returns a Dict, whose keys are related fields and values are corresponding to each key.
- Parameters
cur – Cursor of SQLite.
table – The table you want to retrieve from.
kwargs – dict of field, order and orderby,where field is the key and order, orderby are elements of a tuple.
field: denotes field or attribute of a table.
order: defines if you want to SELECT by order.
orderby: the field you want to order by.
- Returns
A Dict, whose keys are related fields and values are corresponding to each key.
Examples
>>> msg_dict = get_field_from_table( cur, 'laneboundary', lane_roadid = ('roadid', True, 'ogc_fid'), lane_id = ('lane_id', True, 'ogc_fid'), ) >>> msg_dict['lane_roadid'] [0, 0, 1, 2, 2, ...]
- lane_merger.get_only_lanegroup(lane_msg)¶
When there are several duplicated elements in a row within lane_msg[‘lanegroup_id’], as an example, [1, 1, 1, 3, 3, 1], drop redundant ones, so that it comes to [1, 3, 1]. It should be noticed that the output only_lanegroup is allowed to have duplicated elements which are not neighbors.
- Parameters
lane_msg – The dict of fields get from laneboundary table
Examples
>>> lane_msg['lanegroup_id'] [12, 12, 12, 3, 3, 4, 12, 12] >>> lst = get_only_lanegroup(lane_msg) >>> lst [12, 3, 4, 12]
- lane_merger.main()¶
- relation_info: Information of the relative relation of a
laneboundary, which road_id and lanegroup_id does it belong to, for example, [0, 12] means road_id is 0 and lanegroup_id is 12.
- lane_class: A list same as all_merge, to save the instances
of class SingleLane.
- group_geo: A list of points features, contains the boundary of
all lanegroup. The first point is the same as the last point, which means when those points are formed into lines, the figure is closed.
group_geo_m: meter as unit, same as above.
Main process of lane_merger
Get fields from table road and laneboundary through get_field_from_table() function, which returns a dict type interface, and the keys of dict are defined by yourself.
Then remove all the records of laneboundary that have a boundarytype of GORE through remove_type() function.
As a matter of fact, a single laneboundary might be cut into several segments, which have the same roadid, lane_id, lanegroup_id and different boundary_element_id. Merging those segments can be implemented by merge_segment() function.
The core of the process is merging all laneboundary, which is implemented by merge_lane() function.
An interface is added so that one can see the geometry feature more intuitive, meter as unit. See any attribute has the suffix _m.
One can generate an example/case throuth case_generator().
The MAIN interface comes to a dict, all_message, here is the definition.
- lane_merger.merge_lane(lane_msg)¶
Merge all laneboundary
This function outs a list, all_merge, and lists under all_merge, refer to different lanegroup. Each lanegroup contains mutiple instances of SingleLane class, which refer to different lane (each lane has two lane boundaries).
only_lanegroup stores the information of langroup_id. So only_lanegroup and lane_msg[‘road_id’] are used to traverse the table, with a specific logic.
There is a certain mapping relation between the defined only_lanegroup and lane_msg[‘lanegroup_id’], which you can see in the definition of get_only_lanegroup().
A certain road_id and a corresponding lanegroup_id specify a single lane, and the left and right lane boundaries are exactly two neighbors in the laneboundary table or more precisely, lane_msg. Therefore the traversal and retrieval can be more intuitive to operate.
- lane_merger.merge_segment(lane_msg)¶
In laneboundary table, there are some records that has the same roadid, lane_id, lanegroup_id and different boundary_element_id, which means these are actually several segments of one road.
This function is designed to merge these segments, in every field of the table, including asewkt(GEOMETRY). As one can see, the length of each field list is the same.
Here is the logic of the merging. Assume that record-1 and record-2 are two segments of one lane, then after merging, record-1 is going to be dumped and geometry record of it is concatenated to the top of geometry of record-2. Therefore, two segments are merged geometrically.
- Parameters
lane_msg – The dict of several fields get from laneboundary table.
- Returns
Two interfaces, one is the information showing the relation between laneid and lanegroup_id in each record, another is the dict after the merge operation.
- Raises
RuntimeError – Exception handling to be implemented.
- lane_merger.remove_type(msg_dict, *args)¶
Check boundarytype field of each record in laneboundary table, delete the record when boundarytype is in args, e.g., GORE.
- Parameters
msg_dict – The dict of fields get from laneboundary table
args – Any type you want to remove from the table
- Returns
New dict after dumping args
- Return type
dict
- lane_merger.case_generator(msg_dict, group_geometry)¶
Generate a case that can be visualize in QGis
The case is a whole lanegroup with all its single lanes. Call get_args_from_terminal() to get parameters from the terminal and search for the corresponding geometry features, finally export to a .shp file which can be displayed and processed in QGis.
The information of generating case will prompt on the terminal.
- Parameters
msg_dict – Contains all features, is defined in main()
group_geometry – The geometry infomation which is also defined in main()