cvts package

Subpackages

Submodules

cvts.models module

Module providing an ORM for objects of interest.

class cvts.models.Base(**kwargs)

Bases: sqlalchemy.orm.decl_api.Base

The location a vehicle is garraged.

id
lat
lon
vehicle
vehicle_id
class cvts.models.Stop(**kwargs)

Bases: sqlalchemy.orm.decl_api.Base

The location a vehicle is garraged.

end_lat
end_lon
end_time
id
n_stationary
start_end_dist
start_lat
start_lon
start_time
vehicle
vehicle_id
class cvts.models.Traversal(**kwargs)

Bases: sqlalchemy.orm.decl_api.Base

A traversal of a Segment.

count
dow
doy
edge
hour
id
speed
timestamp
trip
trip_id
vehicle
vehicle_id
woy
class cvts.models.Trip(**kwargs)

Bases: sqlalchemy.orm.decl_api.Base

The location a vehicle is garraged.

end
end_id
id
start
start_id
vehicle
vehicle_id
class cvts.models.Vehicle(**kwargs)

Bases: sqlalchemy.orm.decl_api.Base

Data on a vehicle.

etl_id
id
rego
vehicle_type
vehicle_type_id
class cvts.models.VehicleType(**kwargs)

Bases: sqlalchemy.orm.decl_api.Base

A list of vehicle types.

code
group
id
type_en
type_vn

cvts.settings module

cvts.settings.BOUNDARIES_PATH = '/home/kna012/.cvts/boundaries'

Directory for shape files for geographies.

cvts.settings.CONFIG_PATH = '/home/kna012/.cvts/config'

Directory containing VALHALLA_CONFIG_FILE

cvts.settings.DEBUG = False

Are we debugging. Can be set via the environment variable CVTS_DEBUG.

cvts.settings.EARTH_RADIUS = 6371000

Radius of the Earth in meters.

cvts.settings.LAKE_FLAG = 'lake'

String used to denote stuff from the lake

cvts.settings.MIN_DISTANCE_BETWEEN_STOPS = 50

The distance between two stops below which they are considered to be the same location.

cvts.settings.MIN_MOVE_DISTANCE = 50

The minimum distance a vehicle can move between two (potentially non-adjacent) GPS points to not be considered ‘moving’ in meters.

cvts.settings.MIN_MOVING_SPEED = 4

The minimum speed a vehicle can be moving to be considered ‘moving’ in kilometers per hour.

cvts.settings.MIN_STOP_TIME = 480

The minimum time a vehicle must not move for to be considered ‘stopped’ in seconds.

cvts.settings.OUT_PATH = '~/.cvts/output'

Root directory for outputs.

cvts.settings.POSTGRES_CONNECTION_STRING = None

The connection string for PostGRE. Read from the environment variable CVTS_POSTGRES_CONNECTION_STRING.

cvts.settings.RAW_DATA_FILE_EXTENSIONS = ['.csv']

Extensions for the input files to keep. Only used if RAW_DATA_FORMAT is RawDataFormat.CSV or RawDataFormat.GZIP.

cvts.settings.RAW_DATA_FORMAT = RawDataFormat.CSV

The format the raw data is stored in.

cvts.settings.RAW_PATH = '/home/kna012/.cvts/raw'

Root directory for input files.

class cvts.settings.RawDataFormat(value)

Bases: enum.Enum

An enumeration.

CSV = 1
GZIP = 3
cvts.settings.SEQ_PATH = '~/.cvts/output/seq'

Output directory for trip outputs.

cvts.settings.SPEED_PATH = '~/.cvts/output/speed'

Output directory for speed outputs.

cvts.settings.SRC_DEST_PATH = '~/.cvts/output/src_dest'

Output directory for source/destination outputs.

cvts.settings.STOP_PATH = '~/.cvts/output/stop'

Output directory for stop points.

cvts.settings.VALHALLA_CONFIG_FILE = '/home/kna012/.cvts/config/valhalla.json'

Path to Valhalla configuration file.

cvts.settings.WORK_PATH = '~/.cvts'

Default root directory

cvts.settings.setup_logging(level=20)

Module contents

exception cvts.DataLakeError

Bases: Exception

exception cvts.NoRawDataException

Bases: Exception

class cvts.RawDataFormat(value)

Bases: enum.Enum

An enumeration.

CSV = 1
GZIP = 3
cvts.distance(x0, y0, x1, y1)

Quick and adequate distance calculation for for small distances in meters between two lon/lats.

This should be adequate for the purpose of measuring points between points in the kinds of GPS traces we are working with, where the typical sampling interval is around 15 seconds (i.e., differences in latitude between y0 and y1 are (very) small).

Parameters
  • x0 (float) – First longitude.

  • y0 (float) – First latitude.

  • x1 (float) – Second longitude.

  • y1 (float) – Second latitude.

Returns

Approximate distance between the two points in meters.

cvts.json2geojson(data, shape_only)

Convert the output from Valhalla to a GeoJSON object.

On the command line, the call would look like:

valhalla_service <config-file> trace_attributes <input-file>
Parameters
  • data (Dict[str, Any]) – Output of a call to Valhalla (as described above).

  • shape_only (bool) – Should the returned GeoJSON only include the shape element from data?

Return type

Dict[str, Any]

Returns

TODO

cvts.jsonfile2geojsonfile(infile, outfile)
cvts.points_to_polys(points, shapedata)

Calculate which geometry each point in points lie within.

Parameters
  • points (NDArray) – Two column numpy array containing the longitudes and latitudes of the address points.

  • shapedata (str) – tuple containing geometry IDs, polygions and bounding boxes for a geography.

Return type

NDArray

Returns

The geometry ID for each point.

cvts.rawfiles2jsonchunks(input_descriptor, split_trips, dates)

Create a generator over all the data for a single vehicle from data in a csv or iterable of csvs.

Parameters
  • input_descriptor (Union[str, Iterable[str]]) – Either the name of a GPS data file or an iterable of names of such files.

  • split_trips (bool) – if True, then split into trips, otherwise return all points as a single ‘trip’.

  • dates (Iterable[datetime.date]) –

Return type

Generator[Dict[str, Any], None, None]

Returns

A geenerator over a dicts that contain information about each GPS point. Each dict looks like:

{
    'lat':     float(row['Longitude']),
    'lon':     float(row['Latitude']),
    'time':    float(row['Time']),
    'heading': float(row['Orientation']),
    'speed':   float(row['speed']),
    'heading_tolerance': 45,
    'type':    'via'
}

cvts.rawfiles2jsonfile(csv_file, out_file)

Call rawfiles2jsonchunks(), passing csv_files and False, and write the result to out_file.

Parameters
  • csv_file (Union[str, Iterable[str]]) – Either the name of a GPS data file or an iterable of names of such files.

  • out_file (str) – The path of the file to write the trip to.

cvts.read_shapefile(filename, geometry_id_field)

Load the data from a shapefile.

Parameters
  • filename (str) – The name of the shapefile.

  • geometry_id_field (str) – The field to extract from the shapefile. This should contain the identifier for the geometries.

Return type

str

Returns

The name of the file the data in the shapefile was saved to.

cvts.vehicle_ids()