2

Assume I have recorded the following values during a bike trip. One such dataset has been recorded every 5 seconds:

timestamp, longitute, latitude, pulse rate, pedal frequency

What is the best way to store them in a GeoDjango application? I thought about using a model similar to the following.

class Measurement(models.Model):
    comment = models.CharField()

class MeasurementPoint(models.Model):
    measurement = models.ForeignKey(Measurement)
    timestamp = models.DateTimeField()
    point = models.PointField(srid=4326)
    pulse = models.IntegerField()
    pedal = models.IntegerField()

This works and I can query the measurement points. But now I want to display the bike trip on a map, so I have to convert the individual points to a LineString. How is this done? And how would it bit possible to use different colors to highlight for example the pulse rate with different colors?

1

Finally I came to the following solution using the geojson python package. Surely not the best way, but working...

The example below shows a view to get the measurement of the pulse as an array of LineStrings. Regions where the pulse is at the same level are aggregated to LineStrings.

@api_view(['GET'])
def getPulseLineString(request):
    request_data = request.query_params
    measurementID = request_data['measurementID']
    points = MeasurementPoint.objects.filter(measurement=measurementID).
    features = []
    pulse = -1
    segments = []
    for p in points:
        if pulse == -1:
            pulse = p.pulse
            segments.append((p.geom.x, p.geom.y))
        elif p.pulse == pulse:
            segments.append((p.geom.x, p.geom.y))
        else:
            features.append(geojson.Feature(geometry=geojson.LineString(segments), properties={'pulse': pulse}))
        segments= []
        pulse = -1
    return Response(features, status=status.HTTP_200_OK, content_type='application/json')

Maybe it would be better to use the MakeLine functionality, but as I want to use a SQLite database this is not supported at the moment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.