def testStillSimple (self):
        # 147639834
        way= wkt.loads ('''
POLYGON ((662012.67         5329425.33,
          662013.5600000001 5329427.77,

          662020.5699999999 5329417.4,
          662024.14         5329413.74,
          662032.37         5329406.88,

          662034.6          5329405.05,
          662032.71         5329403.53,

          662027.59         5329407.64,
          662019.6800000001 5329414.2,
          662016.23         5329419.54,
          662012.67         5329425.33))''')
        skel= wkt.loads ('''
MULTILINESTRING ((662012.67 5329425.33, 662013.8453861615 5329425.448829351),
                 (662016.23 5329419.54, 662017.2442048641 5329420.179304471),
                 (662019.6800000001 5329414.2, 662020.7605526489 5329415.159279451),
                 (662027.59 5329407.64, 662028.3729275202 5329408.599534022),
                 (662032.71 5329403.53, 662032.7093037122 5329405.059336644),
                 (662034.6 5329405.05, 662032.7093037122 5329405.059336644),
                 (662032.37 5329406.88, 662031.6052956476 5329405.955395874),
                 (662024.14 5329413.74, 662023.2924827472 5329412.823571924),
                 (662020.5699999999 5329417.4, 662019.6022560901 5329416.611499771),
                 (662013.5600000001 5329427.77, 662013.8453861615 5329425.448829351),
                 (662013.8453861615 5329425.448829351, 662017.2442048641 5329420.179304471),
                 (662032.7093037122 5329405.059336644, 662031.6052956476 5329405.955395874),
                 (662017.2442048641 5329420.179304471, 662019.6022560901 5329416.611499771),
                 (662031.6052956476 5329405.955395874, 662028.3729275202 5329408.599534022),
                 (662028.3729275202 5329408.599534022, 662023.2924827472 5329412.823571924),
                 (662019.6022560901 5329416.611499771, 662020.7605526489 5329415.159279451),
                 (662023.2924827472 5329412.823571924, 662020.7605526489 5329415.159279451))''')
        medials= wkt.loads ('''
MULTILINESTRING ((662032.7093037122 5329405.059336644,
                  662031.6052956476 5329405.955395874,
                  662028.3729275202 5329408.599534022,
                  662023.2924827472 5329412.823571924,
                  662020.7605526489 5329415.159279451,
                  662019.6022560901 5329416.611499771,
                  662017.2442048641 5329420.179304471,
                  662013.8453861615 5329425.448829351))''')

        result= centerlines.extend_medials (way, skel, medials)

        # honestly, painstakingly calculated from the shape's coords!
        expected= wkt.loads ('''
MULTILINESTRING ((662033.655        5329404.29,
                  662032.7093037122 5329405.059336644,
                  662031.6052956476 5329405.955395874,
                  662028.3729275202 5329408.599534022,
                  662023.2924827472 5329412.823571924,
                  662020.7605526489 5329415.159279451,
                  662019.6022560901 5329416.611499771,
                  662017.2442048641 5329420.179304471,
                  662013.8453861615 5329425.448829351,
                  662013.115        5329426.55))''')
        self.assertEqual (result, expected, '\n%s\n%s' % (result, expected))
    def testRectangle (self):
        # 147639890
        way= wkt.loads ('''
POLYGON ((661994.3 5329434.18, 661995.1899999999 5329436.47,
          662006.21 5329433.42, 662005.66 5329431.28,
          661994.3 5329434.18))''')
        skel= wkt.loads ('''
MULTILINESTRING ((661994.3 5329434.18, 661995.9207244834 5329435.013669997),
                 (662005.66 5329431.28, 662004.8565612 5329432.636764912),
                 (662006.21 5329433.42, 662004.8565612 5329432.636764912),
                 (661995.1899999999 5329436.47, 661995.9207244834 5329435.013669997),
                 (662004.8565612 5329432.636764912, 661995.9207244834 5329435.013669997))''')
        medials= wkt.loads ('''
MULTILINESTRING ((662004.8565612 5329432.636764912,
                  661995.9207244834 5329435.013669997))''')

        result= centerlines.extend_medials (way, skel, medials)

        # honestly, painstakingly calculated from the shape's coords!
        expected= wkt.loads ('''
MULTILINESTRING ((662005.935 5329432.35,
                  662004.8565612 5329432.636764912,
                  661995.9207244834 5329435.013669997,
                  661994.745 5329435.324999999))''')
        self.assertEqual (result, expected, '\n%s\n%s' % (result, expected))
    def testTShape (self):

        medials= centerlines.extend_medials (t_shape, t_skel, t_medials)

        expected= MultiLineString ( [( (0, 1), (1, 1), (3, 1) ),
                                     ( (3, 1), (3, 4), (3, 5) ),
                                     ( (3, 1), (5, 1), (6, 1) )] )
        self.assertEqual (medials, expected)
예제 #4
0
    data = json.loads(s)
except json.decoder.JSONDecodeError:
    print('Malformed JSON: >%s<' % s, file=sys.stderr)
else:
    ans = dict(type='FeatureCollection', features=[])
    conn = psycopg2.connect(dbname='gis')

    for feature in data['features']:
        shape = shapely.geometry.shape(feature['geometry'])

        start = time.perf_counter()
        shape = shape.simplify(tolerance, False)
        mid1 = time.perf_counter()
        skel, medials = centerlines.skeleton_medials_from_postgis(conn, shape)
        mid2 = time.perf_counter()
        medials = centerlines.extend_medials(shape, skel, medials)
        mid3 = time.perf_counter()
        medials = shapely.geometry.MultiLineString(
            [medial.simplify(tolerance, False) for medial in medials])
        end = time.perf_counter()

        print("simp1: %.6f; pg: %.6f; ext: %.6f; simp2: %.6f" %
              (mid1 - start, mid2 - mid1, mid3 - mid2, end - mid3),
              file=sys.stderr)

        ans['features'].append(
            dict(type='Feature', geometry=shapely.geometry.mapping(medials)))

    s = json.dumps(ans)
    # print (s, file=sys.stderr)
    print(s)
    data= json.loads(s)
except json.decoder.JSONDecodeError:
    print ('Malformed JSON: >%s<' % s, file=sys.stderr)
else:
    ans= dict (type='FeatureCollection',features=[])
    conn= psycopg2.connect (dbname='gis')

    for feature in data['features']:
        shape= shapely.geometry.shape (feature['geometry'])

        start= time.perf_counter ()
        shape= shape.simplify (tolerance, False)
        mid1= time.perf_counter ()
        skel, medials= centerlines.skeleton_medials_from_postgis (conn, shape)
        mid2= time.perf_counter ()
        medials= centerlines.extend_medials (shape, skel, medials)
        mid3= time.perf_counter ()
        medials= shapely.geometry.MultiLineString ([ medial.simplify (tolerance, False)
                                                     for medial in medials ])
        end= time.perf_counter ()

        print ("simp1: %.6f; pg: %.6f; ext: %.6f; simp2: %.6f" %
               (mid1-start, mid2-mid1, mid3-mid2, end-mid3),
               file=sys.stderr)

        ans['features'].append (dict (type='Feature',
                                      geometry=shapely.geometry.mapping (medials)))

    s= json.dumps (ans)
    # print (s, file=sys.stderr)
    print (s)
    def testTShape (self):
        # 147639866
        way= wkt.loads ('''
POLYGON ((662625.37         5329242.34,
          662625.48         5329244.78,

          662633.61         5329244.17,
          662646.1899999999 5329243.41,
          662709.53         5329239.75,

          662714.2          5329239.44,
          662713.54         5329236.55,

          662664.67         5329239.75,
          662662.78         5329207.88,
          662662.33         5329207.73,

          662659.77         5329172.2,
          662658.77         5329172.35,

          662659.99         5329190.49,
          662660.1          5329192.48,
          662661.11         5329208.34,
          662661.33         5329211.23,
          662662.11         5329224.04,
          662662.33         5329226.79,
          662662.4399999999 5329230.14,
          662663.11         5329239.75,
          662633.5          5329241.73,
          662625.37         5329242.34))''')
        skel= wkt.loads ('''
MULTILINESTRING ((662625.37 5329242.34, 662626.6428160192 5329243.46862635),
                 (662633.5 5329241.73, 662633.586420889 5329242.948073145),
                 (662663.11 5329239.75, 662663.9367766941 5329240.471068127),
                 (662662.4399999999 5329230.14, 662663.2675881328 5329230.097577795),
                 (662662.33 5329226.79, 662663.1132017276 5329226.745837877),
                 (662662.11 5329224.04, 662662.9207781861 5329223.982889948),
                 (662661.33 5329211.23, 662662.1509925085 5329211.173759311),
                 (662661.11 5329208.34, 662661.8064050304 5329208.291320801),
                 (662660.1 5329192.48, 662660.6631719584 5329192.446503516),
                 (662659.99 5329190.49, 662660.5364903673 5329190.456520172),
                 (662658.77 5329172.35, 662659.3053100435 5329172.780566629),
                 (662659.77 5329172.2, 662659.3053100435 5329172.780566629),
                 (662662.33 5329207.73, 662661.7319315083 5329208.194630414),
                 (662662.78 5329207.88, 662661.9700256903 5329208.501053851),
                 (662664.67 5329239.75, 662663.4600676692 5329241.12095668),
                 (662713.54 5329236.55, 662712.3903596955 5329238.09265873),
                 (662714.2 5329239.44, 662712.3903596955 5329238.09265873),
                 (662709.53 5329239.75, 662709.4391896345 5329238.28723081),
                 (662646.1899999999 5329243.41, 662646.1155780922 5329242.150709221),
                 (662633.61 5329244.17, 662633.5275230983 5329242.952061612),
                 (662625.48 5329244.78, 662626.6428160192 5329243.46862635),
                 (662659.3053100435 5329172.780566629, 662660.5364903673 5329190.456520172),
                 (662660.5364903673 5329190.456520172, 662660.6631719584 5329192.446503516),
                 (662660.6631719584 5329192.446503516, 662661.7319315083 5329208.194630414),
                 (662661.7319315083 5329208.194630414, 662661.8064050304 5329208.291320801),
                 (662661.8064050304 5329208.291320801, 662661.9700256903 5329208.501053851),
                 (662663.9367766941 5329240.471068127, 662663.4062238659 5329241.073068604),
                 (662663.9367766941 5329240.471068127, 662663.2675881328 5329230.097577795),
                 (662663.1132017276 5329226.745837877, 662663.2675881328 5329230.097577795),
                 (662663.1132017276 5329226.745837877, 662662.9207781861 5329223.982889948),
                 (662662.9207781861 5329223.982889948, 662662.1509925085 5329211.173759311),
                 (662662.1509925085 5329211.173759311, 662661.9700256903 5329208.501053851),
                 (662633.5275230983 5329242.952061612, 662633.586420889 5329242.948073145),
                 (662633.5275230983 5329242.952061612, 662626.6428160192 5329243.46862635),
                 (662633.586420889 5329242.948073145, 662646.1155780922 5329242.150709221),
                 (662646.1155780922 5329242.150709221, 662663.4062238659 5329241.073068604),
                 (662663.4600676692 5329241.12095668, 662663.4062238659 5329241.073068604),
                 (662663.4600676692 5329241.12095668, 662709.4391896345 5329238.28723081),
                 (662712.3903596955 5329238.09265873, 662709.4391896345 5329238.28723081))''')
        medials= wkt.loads ('''
MULTILINESTRING ((662626.6428160192 5329243.46862635,
                  662633.5275230983 5329242.952061612,
                  662633.586420889 5329242.948073145,
                  662646.1155780922 5329242.150709221,
                  662663.4062238659 5329241.073068604),

                 (662659.3053100435 5329172.780566629,
                  662660.5364903673 5329190.456520172,
                  662660.6631719584 5329192.446503516,
                  662661.7319315083 5329208.194630414,
                  662661.8064050304 5329208.291320801,
                  662661.9700256903 5329208.501053851,
                  662662.1509925085 5329211.173759311,
                  662662.9207781861 5329223.982889948,
                  662663.1132017276 5329226.745837877,
                  662663.2675881328 5329230.097577795,
                  662663.9367766941 5329240.471068127,
                  662663.4062238659 5329241.073068604),

                 (662712.3903596955 5329238.09265873,
                  662709.4391896345 5329238.28723081,
                  662663.4600676692 5329241.12095668,
                  662663.4062238659 5329241.073068604))''')

        result= centerlines.extend_medials (way, skel, medials)

        # honestly, painstakingly calculated from the shape's coords!
        expected= wkt.loads ('''
MULTILINESTRING ((662625.425        5329243.5600000005,
                  662626.6428160192 5329243.46862635,
                  662633.5275230983 5329242.952061612,
                  662633.586420889  5329242.948073145,
                  662646.1155780922 5329242.150709221,
                  662663.4062238659 5329241.073068604),

                 (662659.27         5329172.275,
                  662659.3053100435 5329172.780566629,
                  662660.5364903673 5329190.456520172,
                  662660.6631719584 5329192.446503516,
                  662661.7319315083 5329208.194630414,
                  662661.8064050304 5329208.291320801,
                  662661.9700256903 5329208.501053851,
                  662662.1509925085 5329211.173759311,
                  662662.9207781861 5329223.982889948,
                  662663.1132017276 5329226.745837877,
                  662663.2675881328 5329230.097577795,
                  662663.9367766941 5329240.471068127,
                  662663.4062238659 5329241.073068604),

                 (662713.87         5329237.995,
                  662712.3903596955 5329238.09265873,
                  662709.4391896345 5329238.28723081,
                  662663.4600676692 5329241.12095668,
                  662663.4062238659 5329241.073068604))''')
        self.assertEqual (result, expected, '\n%s\n%s' % (result, expected))
    def testRectangle (self):

        medials= centerlines.extend_medials (rectangle, rect_skel, rect_medials)

        expected= MultiLineString ( [( (0, 1), (1, 1), (3, 1), (4, 1) )] )
        self.assertEqual (medials, expected)