|
| 1 | +from .dataset_provider import DatasetProvider, dataset_definition |
| 2 | + |
| 3 | + |
| 4 | +@dataset_definition(name="basic/telematics", |
| 5 | + summary="Telematics dataset for GPS tracking", |
| 6 | + autoRegister=True, |
| 7 | + supportsStreaming=True) |
| 8 | +class BasicTelematicsProvider(DatasetProvider.NoAssociatedDatasetsMixin, DatasetProvider): |
| 9 | + """ |
| 10 | + Basic Telematics Dataset |
| 11 | + ======================== |
| 12 | +
|
| 13 | + This is a basic telematics dataset with time-series `lat`, `lon`, and `heading` values. |
| 14 | +
|
| 15 | + It takes the following options when retrieving the table: |
| 16 | + - random: if True, generates random data |
| 17 | + - rows : number of rows to generate |
| 18 | + - partitions: number of partitions to use |
| 19 | + - numDevices: number of unique device IDs |
| 20 | + - startTimestamp: earliest timestamp for IOT time series data |
| 21 | + - endTimestamp: latest timestamp for IOT time series data |
| 22 | + - minLat: minimum latitude |
| 23 | + - maxLat: maximum latitude |
| 24 | + - minLon: minimum longitude |
| 25 | + - maxLon: maximum longitude |
| 26 | + - generateWKT: if `True`, generates the well-known text representation of the location |
| 27 | + |
| 28 | + As the data specification is a DataGenerator object, you can add further columns to the data set and |
| 29 | + add constraints (when the feature is available) |
| 30 | +
|
| 31 | + Note that this datset does not use any features that would prevent it from being used as a source for a |
| 32 | + streaming dataframe, and so the flag `supportsStreaming` is set to True. |
| 33 | +
|
| 34 | + """ |
| 35 | + MIN_DEVICE_ID = 1000000 |
| 36 | + MAX_DEVICE_ID = 9223372036854775807 |
| 37 | + DEFAULT_NUM_DEVICES = 1000 |
| 38 | + DEFAULT_START_TIMESTAMP = "2024-01-01 00:00:00" |
| 39 | + DEFAULT_END_TIMESTAMP = "2024-02-01 00:00:00" |
| 40 | + DEFAULT_MIN_LAT = -90.0 |
| 41 | + DEFAULT_MAX_LAT = 90.0 |
| 42 | + DEFAULT_MIN_LON = -180.0 |
| 43 | + DEFAULT_MAX_LON = 180.0 |
| 44 | + COLUMN_COUNT = 6 |
| 45 | + ALLOWED_OPTIONS = [ |
| 46 | + "numDevices", |
| 47 | + "startTimestamp", |
| 48 | + "endTimestamp", |
| 49 | + "minLat", |
| 50 | + "maxLat", |
| 51 | + "minLon", |
| 52 | + "maxLon", |
| 53 | + "generateWkt", |
| 54 | + "random" |
| 55 | + ] |
| 56 | + |
| 57 | + @DatasetProvider.allowed_options(options=ALLOWED_OPTIONS) |
| 58 | + def getTableGenerator(self, sparkSession, *, tableName=None, rows=-1, partitions=-1, |
| 59 | + **options): |
| 60 | + import dbldatagen as dg |
| 61 | + import warnings as w |
| 62 | + |
| 63 | + generateRandom = options.get("random", False) |
| 64 | + numDevices = options.get("numDevices", self.DEFAULT_NUM_DEVICES) |
| 65 | + startTimestamp = options.get("startTimestamp", self.DEFAULT_START_TIMESTAMP) |
| 66 | + endTimestamp = options.get("endTimestamp", self.DEFAULT_END_TIMESTAMP) |
| 67 | + minLat = options.get("minLat", self.DEFAULT_MIN_LAT) |
| 68 | + maxLat = options.get("maxLat", self.DEFAULT_MAX_LAT) |
| 69 | + minLon = options.get("minLon", self.DEFAULT_MIN_LON) |
| 70 | + maxLon = options.get("maxLon", self.DEFAULT_MAX_LON) |
| 71 | + generateWkt = options.get("generateWkt", False) |
| 72 | + |
| 73 | + assert tableName is None or tableName == DatasetProvider.DEFAULT_TABLE_NAME, "Invalid table name" |
| 74 | + if rows is None or rows < 0: |
| 75 | + rows = DatasetProvider.DEFAULT_ROWS |
| 76 | + if partitions is None or partitions < 0: |
| 77 | + partitions = self.autoComputePartitions(rows, self.COLUMN_COUNT) |
| 78 | + if minLat < -90.0: |
| 79 | + minLat = -90.0 |
| 80 | + w.warn("Received an invalid minLat value; Setting to -90.0") |
| 81 | + if minLat > 90.0: |
| 82 | + minLat = 89.0 |
| 83 | + w.warn("Recieved an invalid minLat value; Setting to 89.0") |
| 84 | + if maxLat < -90: |
| 85 | + maxLat = -89.0 |
| 86 | + w.warn("Recieved an invalid maxLat value; Setting to -89.0") |
| 87 | + if maxLat > 90.0: |
| 88 | + maxLat = 90.0 |
| 89 | + w.warn("Received an invalid maxLat value; Setting to 90.0") |
| 90 | + if minLon < -180.0: |
| 91 | + minLon = -180.0 |
| 92 | + w.warn("Received an invalid minLon value; Setting to -180.0") |
| 93 | + if minLon > 180.0: |
| 94 | + minLon = 179.0 |
| 95 | + w.warn("Received an invalid minLon value; Setting to 179.0") |
| 96 | + if maxLon < -180.0: |
| 97 | + maxLon = -179.0 |
| 98 | + w.warn("Received an invalid maxLon value; Setting to -179.0") |
| 99 | + if maxLon > 180.0: |
| 100 | + maxLon = 180.0 |
| 101 | + w.warn("Received an invalid maxLon value; Setting to 180.0") |
| 102 | + if minLon > maxLon: |
| 103 | + (minLon, maxLon) = (maxLon, minLon) |
| 104 | + w.warn("Received minLon > maxLon; Swapping values") |
| 105 | + if minLat > maxLat: |
| 106 | + (minLat, maxLat) = (maxLat, minLat) |
| 107 | + w.warn("Received minLat > maxLat; Swapping values") |
| 108 | + df_spec = ( |
| 109 | + dg.DataGenerator(sparkSession=sparkSession, rows=rows, |
| 110 | + partitions=partitions, randomSeedMethod="hash_fieldname") |
| 111 | + .withColumn("device_id", "long", minValue=self.MIN_DEVICE_ID, maxValue=self.MAX_DEVICE_ID, |
| 112 | + uniqueValues=numDevices, random=generateRandom) |
| 113 | + .withColumn("ts", "timestamp", begin=startTimestamp, end=endTimestamp, |
| 114 | + interval="1 second", random=generateRandom) |
| 115 | + .withColumn("base_lat", "float", minValue=minLat, maxValue=maxLat, step=0.5, |
| 116 | + baseColumn='device_id', omit=True) |
| 117 | + .withColumn("base_lon", "float", minValue=minLon, maxValue=maxLon, step=0.5, |
| 118 | + baseColumn='device_id', omit=True) |
| 119 | + .withColumn("unv_lat", "float", expr="base_lat + (0.5-format_number(rand(), 3))*1e-3", omit=True) |
| 120 | + .withColumn("unv_lon", "float", expr="base_lon + (0.5-format_number(rand(), 3))*1e-3", omit=True) |
| 121 | + .withColumn("lat", "float", expr=f"""CASE WHEN unv_lat > {maxLat} THEN {maxLat} |
| 122 | + ELSE CASE WHEN unv_lat < {minLat} THEN {minLat} |
| 123 | + ELSE unv_lat END END""") |
| 124 | + .withColumn("lon", "float", expr=f"""CASE WHEN unv_lon > {maxLon} THEN {maxLon} |
| 125 | + ELSE CASE WHEN unv_lon < {minLon} THEN {minLon} |
| 126 | + ELSE unv_lon END END""") |
| 127 | + .withColumn("heading", "integer", minValue=0, maxValue=359, step=1, random=generateRandom) |
| 128 | + .withColumn("wkt", "string", expr="concat('POINT(', lon, ' ', lat, ')')", omit=not generateWkt) |
| 129 | + ) |
| 130 | + |
| 131 | + return df_spec |
0 commit comments