Ejemplo n.º 1
0
    def readfile(self):
        # Open file
        try:
            with open(self.filename) as f:
                rows = sum(1 for _ in f)
            fil = open(self.filename, 'r')
        except FileNotFoundError:
            raise FileNotFoundError('File not found...')

        line = fil.readline()
        row = 1
        while line:
            progress = row / rows * 100
            print('Progress: {:.1f}%'.format(progress), end="\r", flush=True)

            segment = []
            if line.startswith('LINESTRING'):
                pts = line.strip('LINESTRING(')\
                          .strip(')\n')\
                          .split(',')
                for pt in pts:
                    geom = pt.split(' ')
                    geom = [float(coord) for coord in geom]
                    geom.reverse()
                    segment.append(tuple(geom))
                self.segments.append(segment)

            # proceed
            row += 1
            line = fil.readline()

        # CLOSE file
        fil.close()
Ejemplo n.º 2
0
 def execute(self, query):
     try:
         self.cursor.execute(query)
         self.connection.commit()
         try:
             return self.cursor.fetchall()
         except:
             return
     except (Exception, pg.Error) as error:
         print('Error occured...', error)
         self.close_connection()  # KILL CONNECTION!
         exit()  # exit program
Ejemplo n.º 3
0
    def readfile(self):
        # Open file
        try:
            with open(self.filename, "rb") as f:
                rows = sum(1 for _ in f)
            print(f'Total: {rows} lines')
        except FileNotFoundError:
            raise FileNotFoundError('File not found...')

        # Process file in two passes
        # first pass: process ways
        # second pass: process nodes
        for mode in range(2):
            msg = 'Processing nodes...' if mode else 'Processing ways...'
            print(msg)

            if not mode:
                self.render_tag_menu()

            self.file = open(self.filename, 'r', encoding="utf8")
            line = self.file.readline()
            row = 1
            while line:
                # progress
                if row % 100000 == 0:  # progress bar slow...
                    progress = row / rows * 100
                    print('Progress: {:.1f}%'.format(progress),
                          end="\r",
                          flush=True)

                # mode 0: process ways
                # mode 1: process nodes
                if mode:
                    self.process_node(line)
                else:
                    row = self.process_way(line, row)

                # proceed
                row += 1
                line = self.file.readline()

            # CLOSE file
            self.file.close()
        msg = 'Result: {ways} ways, {nodes} nodes'.format(ways=len(self.ways),
                                                          nodes=len(
                                                              self.nodes))
        print(msg)
Ejemplo n.º 4
0
 def open_connection(self):
     try:
         self.connection = pg.connect(database=self.database,
                                      user=self.user,
                                      password=self.password,
                                      host=self.host,
                                      port=self.port)
         self.cursor = self.connection.cursor()
         print('Connection established...\n')
     except (Exception, pg.Error) as error:
         print('Connection failed...')
         print(error)
         exit()
Ejemplo n.º 5
0
 def close_connection(self):
     self.cursor.close()
     self.connection.close()
     print('Connection closed...')
Ejemplo n.º 6
0
    def create_network(self, segments):
        # start forming network
        print('Update pgRouting database...')

        # option to clear existent database
        resp = ask_input('- clear database')
        if resp:
            resp = ask_input('- are you sure')
            if resp:
                self.drop_table('ways')
                self.drop_table('ways_vertices_pgr')

        # create ways table
        self.create_ways()

        # count existent rows
        query = 'SELECT COUNT(*) FROM ways'
        count = self.execute(query)[0][0]

        # if network already exists
        # snap simplified to it
        if count > 0:
            # do not recover all the network!
            # only the part close to the new addition

            # recover bbox of simplified
            proj = Transformer.from_crs(self.epsg, 4326)
            bbox = extent(segments)
            if self.epsg != 4326:
                xmin, ymin, xmax, ymax = bbox
                ymin, xmin = proj.transform(ymin, xmin)
                ymax, xmax = proj.transform(ymax, xmax)
                bbox = (xmin, ymin, xmax, ymax)

            # recover network intersecting the bbox
            query = \
                '''
                SELECT ST_AsText(the_geom)
                FROM ways
                WHERE ST_Intersects(
                    the_geom,
                    (SELECT ST_MakeEnvelope(%f, %f, %f, %f, 4326))
                )
                ''' % bbox
            ways = self.execute(query)

            # convert to geometry
            # reference to snap the new addition
            reference = []
            proj = Transformer.from_crs(4326, self.epsg)
            for way in ways:
                wkt = way[0]
                coords = wkt.strip('LINESTRING(')\
                            .strip(')') \
                            .replace(',', ' ') \
                            .split(' ')
                coords = [float(coord) for coord in coords]
                # lat, lon ordering
                edge = []
                for i in range(0, len(coords) - 1, 2):
                    point = proj.transform(coords[i + 1], coords[i])
                    edge.append(point)
                reference.append(edge)

            # if not reference, do not snap
            # the addition is irrelevant to existing network
            if len(reference) > 0:
                snapper = Snapper(segments, reference, self.threshold)
                snapper.snap()
                segments = snapper.segments

        # insert segments
        proj = Transformer.from_crs(self.epsg, 4326)
        for segment in segments:
            # row number
            count += 1

            # form wkt
            wkt = 'LINESTRING('
            for point in segment:
                # ALWAYS project to EPSG:4326
                point = proj.transform(point[0], point[1])
                lat, lon = point
                wkt += '{} {},'.format(lon, lat)
            wkt = wkt[:-1] + ')'

            # insert query
            query = """
                    INSERT INTO ways (id, source, target, cost, the_geom)
                    VALUES ({count}, NULL, NULL,
                            ST_Length(ST_GeomFromText('{wkt}', 4326)),
                            ST_GeomFromText('{wkt}', 4326))
                    """.format(count=count, wkt=wkt)
            self.execute(query)

        # create topology
        query = """
                SELECT
                pgr_createTopology('ways', 0.0001);
                """
        self.execute(query)
Ejemplo n.º 7
0
 def export(name):
     print('Simplification complete...')
     print('Network exported in desktop...\n')
     export_lines(path, name, simplify.segments, epsg)
Ejemplo n.º 8
0
                                           filetypes=(("OSM", "*.osm"),
                                                      (("CSV (Comma Separated Values"), "*.csv"),
                                                      ("all files", "*.*")))
############################################################################


############################################################################
# OSM PARSER
############################################################################
# retrieve filename
filename = root.filename

if filename.endswith('.osm'):
    # parse file
    parser = OSMparser(filename)
    print('File: {}'.format(filename))
    print('Parsing file...', end="\r", flush=True)
    parser.readfile()
    print('Parsing complete...\n')

    # recover network
    network = {}
    network['ways'] = parser.ways
    network['nodes'] = parser.nodes
############################################################################


############################################################################
# SIMPLIFICATION
############################################################################
    # define epsg