Example #1
0
 def save(self, user, story):
     if(self.cleaned_data["jsonPoints"]):
         convertedPoints = json.loads(self.cleaned_data['jsonPoints'])
         if(len(convertedPoints) > 1):
             start_time = None
             end_time = None
             
             if (hasattr(convertedPoints[0],"timestamp")):
                 try:
                     start_time = datetime.strptime(convertedPoints[0]["timestamp"], "%Y-%m-%d %H:%M:%S")
                 except:
                     pass
             
             if(hasattr(convertedPoints[-1], "timestamp")):
                 try:
                     end_time = datetime.strptime(convertedPoints[-1]["timestamp"], "%Y-%m-%d %H:%M:%S")
                 except:
                     pass
             
             privacy = 1
             if (story.privacy):
                 privacy = story.privacy
                 
             gpxTrack = MGPXTrack(title="IPhone Tracking", description="",
                                 safetylevel=privacy,
                                 start_time=start_time,
                                 end_time=end_time,
                                 creator=user)
             gpxTrack.save()
             track = convert_mobile_app_to_linestring(convertedPoints)
             trackSegment = MGPXTrackSegment(title="IPhone Tracking",
                                             description="IPhone Tracking",
                                             content_object=story,
                                             raw_track=convertedPoints,
                                             creator=user,
                                             track=track,
                                             parent=gpxTrack,
                                             distance=track.length,
                                             start_time = start_time,
                                             end_time = end_time)
             gpxTrack.set_story_reference(story)
             gpxTrack.save()
             trackSegment.save()
             
             return gpxTrack
         else:
             return None
     else:
         return None
Example #2
0
def convert_gpx_file(gpx_file, gpxTrack):
    from geo.models import MGPXTrackSegment
    gpx = GPX(gpx_file)
    for trk in gpx.tracks:
        if not gpxTrack.title:
            gpxTrack.title = trk.name
        gpxTrack.distance = trk.distance() / 1000.0
        gpxTrack.start_time = trk.start_time()
        gpxTrack.end_time = trk.end_time()
        gpxTrack.save()
        for segment in trk.trksegs:
            track = convert_gpx_segment_to_linestring(segment.trkpts)
            gpxTrackSegment = MGPXTrackSegment(raw_track=segment.trkpts,
                                               creator=gpxTrack.creator,
                                               track=track, parent=gpxTrack,
                                               distance=track.length,
                                               start_time = segment.trkpts[0].time,
                                               end_time = segment.trkpts[-1].time)
            gpxTrackSegment.save()
    gpxTrack.save()
    return gpxTrack