<commit_before>'''
module for gpx conversion tasks
'''
from django.contrib.gis.geos import MultiLineString
from django.contrib.gis.geos.geometry import GEOSGeometry
from django.contrib.gis.geos.linestring import LineString
from osgeo import ogr
import os

class GPXImportError(Exception):
    'Exception to be raised when gpx files cannot be read.'
    pass


class GPXReader(object):
    '''
    Utility class to extract waypoints, routes or tracks from gpx files.
    '''
    
    def __init__(self, file_path, dim3 = True):
        if(dim3):
            # tell OGR to use the elevation attribute as the Z coordinate
            # value on the geometries.  See: http://www.gdal.org/ogr/drv_gpx.html
            os.environ['GPX_ELE_AS_25D'] = 'YES'
        ogr.UseExceptions()
        self.gpx_file = file_path
        self.ds = ogr.Open(self.gpx_file)
        # Unset the environment variable, so it doesn't affect other
        # GPX DataSource objects.
        if(dim3):
            os.environ.pop('GPX_ELE_AS_25D')
        
        if self.ds is None:
            raise GPXImportError("Cant use %s as datasource" % file_path)
    
    def _guess_layer(self):
        '''
        Guess which layer contains the relevant trail data.
        It is most likely that the route or track layer contains the complete trail.
        If those are not present, it is checked whether there are enough waypoints
        that could make a complete trail.
        '''
        for lay in (2, 1, 0): # track, route, waypoints
            if self.ds.GetLayer(lay).GetFeatureCount() > 0:
                return lay
        raise GPXImportError("Could not guess which GPX layer to use.")
    
    def to_linestring(self, layer=None):
        '''
        Returns the waypoints/track/route as GEOS LineString. Tries
        to automatically detect which layer to use when no layer is provided.
        
        The following types are not handled:
        - polygon (typeid 3)
        - multipoint (typeid 4)
        - multipolygon (typeid 6)
        '''
        if not layer:
            layer = self._guess_layer()
        geom = self.get_layer_geometry(layer)
        if(geom is None or len(geom) == 0):
            return None<commit_msg>simplify linestring before returning geojson representation to client.<commit_after>'''
module for gpx conversion tasks
'''
from django.contrib.gis.geos import MultiLineString
from django.contrib.gis.geos.geometry import GEOSGeometry
from django.contrib.gis.geos.linestring import LineString
from osgeo import ogr
import os

class GPXImportError(Exception):
    'Exception to be raised when gpx files cannot be read.'
    pass


class GPXReader(object):
    '''
    Utility class to extract waypoints, routes or tracks from gpx files.
    '''
    
    def __init__(self, file_path, dim3 = True):
        if(dim3):
            # tell OGR to use the elevation attribute as the Z coordinate
            # value on the geometries.  See: http://www.gdal.org/ogr/drv_gpx.html
            os.environ['GPX_ELE_AS_25D'] = 'YES'
        ogr.UseExceptions()
        self.gpx_file = file_path
        self.ds = ogr.Open(self.gpx_file)
        # Unset the environment variable, so it doesn't affect other
        # GPX DataSource objects.
        if(dim3):
            os.environ.pop('GPX_ELE_AS_25D')
        
        if self.ds is None:
            raise GPXImportError("Cant use %s as datasource" % file_path)
    
    def _guess_layer(self):
        '''
        Guess which layer contains the relevant trail data.
        It is most likely that the route or track layer contains the complete trail.
        If those are not present, it is checked whether there are enough waypoints
        that could make a complete trail.
        '''
        for lay in (2, 1, 0): # track, route, waypoints
            if self.ds.GetLayer(lay).GetFeatureCount() > 0:
                return lay
        raise GPXImportError("Could not guess which GPX layer to use.")
    
    def to_linestring(self, layer=None):
        '''
        Returns the waypoints/track/route as GEOS MultiLineString. Tries
        to automatically detect which layer to use when no layer is provided.
        
        The following types are not handled:
        - polygon (typeid 3)
        - multipoint (typeid 4)
        - multipolygon (typeid 6)
        '''
        if not layer:
            layer = self._guess_layer()
        geom = self.get_layer_geometry(layer)
        if(geom is None or len(geom) == 0):
            return None
{'fraction_non_alphanumeric': 0.07779255319148937, 'fraction_numerical': 0.006648936170212766, 'mean_word_length': 2.2491000719942407, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4696254', 'n_tokens_mistral': 1327, 'n_tokens_neox': 1280, 'n_words': 454}