forked from morb-au/CommonMap-importers---experimental
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.cm-convert-ogr-postgis-to-simpleosmosis-postgres.pl
2837 lines (2204 loc) · 92.9 KB
/
2.cm-convert-ogr-postgis-to-simpleosmosis-postgres.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
# Script to convert data
# from the PostGIS format understood by ogr2ogr,
# to the PostgreSQL simple schema format understood by osmosis
# Note:
# Run it with the nickname of your source schema
# as your command line parameter, e.g. "ca.nrn"
# for the Canadian National Road Network.
# Brendan Morley, 2009-12-30
# In the spirit of the CC BY licence used by CommonMap, this script is
# published under the BSD licence.
#
# The purpose of this script is to assist in getting OGR-readable databases
# into the OSM API Production Schema.
#
##
## TODOs
##
#### Coordinate conversion. e.g. GDA94 -> WGS84
##
#### Duplicate way detection (Duplicate node location and merging works OK)
##
##
## INCLUDES
##
use DBI;
use Math::Trig;
require '../connect/origin.pl';
require '../connect/destination.pl';
require 'schema/'.$ARGV[0].'/'.$ARGV[0].'.schema.pl';
##
## SETTINGS
##
# # useful for debugging and tracking versions
# $testbuild = '15';
$empty_destination_first = 1;
## OSM API upload limits
# Maximum nodes per way
$osmapi_way_chunk_size = 2000;
# Maximum elements per changeset
$osmapi_changeset_chunk_size = 50000;
# (50000 items takes ~3.2 ECU-hours on Amazon EC2 to import into API database
# Using "/upload" REST call.)
# experimental value for the "3a" script
$osmapi_changeset_chunk_size = 100000000; # Maximum elements per changeset
# Maximum elements per changeset - debugging sample
#$osmapi_changeset_chunk_size = 10000;
## PostgreSQL API memory limit heuristics
# Maximum items in a generate_series(low, high)
$pgsql_series_chunk_size = 200;
## Cartographic representation of addressing
# (based on OpenStreetMap's shape_to_osm-Tiger.py script)
# Sets the distance that the address ways should be from the main way, in metres.
# (Queensland surveying standards set the default parcel setback at 15 metres;
# we give a little less so that numbers typically appear on the road verge)
$address_distance = 18;
## For debugging, make the offset bigger
#$address_distance = 30;
# Sets the distance that the ends of the address ways should be pulled back from
# the ends of the main way, in metres.
# (A bit more than the address distance so that numbers do not clash
# at right angle intersections)
$address_pullback = $address_distance * 2.0;
##
## CODE
##
sub Empty_Destination_Tables
{
print "\nEMPTYING THE DESTINATION DATABASE TABLES...";
$dbh_destination->begin_work();
$dbh_destination->do('DELETE FROM relation_tags');
$dbh_destination->do('DELETE FROM relation_members');
$dbh_destination->do('DELETE FROM relations');
$dbh_destination->do('DELETE FROM way_tags');
$dbh_destination->do('DELETE FROM way_nodes');
$dbh_destination->do('DELETE FROM ways');
$dbh_destination->do('DELETE FROM node_tags');
$dbh_destination->do('DELETE FROM nodes');
$dbh_destination->do('DELETE FROM users');
$dbh_destination->commit();
print " DONE.\n\n";
}
sub XY_From_WKB_Point
{
my($wkb_point) = @_;
# Easiest to do this conversion on the database server
# (as long as it's your personal instance;
# if you intend to do this on a shared db
# maybe this function can be refactored to run locally.)
if (!$sth_xy_from_wkb_point)
{
$sth_xy_from_wkb_point =
$dbh_origin->prepare(
'SELECT ST_X(?), ST_Y(?)'
) or die "Can't prepare statement: $DBI::errstr";
}
my $rc = $sth_xy_from_wkb_point->execute
(
$wkb_point,
$wkb_point
)
or die "Can't execute statement: $DBI::errstr";
my @row = $sth_xy_from_wkb_point->fetchrow_array;
if ($row[0])
{
my($point_x) = $row[0];
my($point_y) = $row[1];
# print " Point is at $point_x, $point_y.\n";
# sleep 2;
return ($point_x, $point_y);
}
else
{
# NO-OP
return;
}
} # sub XY_From_WKB_Point
sub WKB_Point_From_XY
{
my($point_x, $point_y) = @_;
# Easiest to do this conversion on the database server
# (as long as it's your personal instance;
# if you intend to do this on a shared db
# maybe this function can be refactored to run locally.)
if (!$sth_wkb_point_from_xy)
{
$sth_wkb_point_from_xy =
$dbh_origin->prepare(
'SELECT ST_SetSRID( '.
'ST_Point(?, ?)'.
' , 4326) as srid_point'
) or die "Can't prepare statement: $DBI::errstr";
}
my $rc = $sth_wkb_point_from_xy->execute
(
$point_x,
$point_y
)
or die "Can't execute statement: $DBI::errstr";
my @row = $sth_wkb_point_from_xy->fetchrow_array;
if ($row[0])
{
my($wkb_point) = $row[0];
# print " WKB is $wkb_point.\n";
# sleep 2;
return ($wkb_point);
}
else
{
# NO-OP
return;
}
} # sub WKB_Point_From_XY
sub Line_Segment_Length
{
print "Entering Line_Segment_Length...\n";
my($from_x, $from_y, $to_x, $to_y) = @_;
# my($length) =
# math.sqrt((lat * lat_feet - firstpoint[0] * lat_feet)**2 + (lon * lon_feet - firstpoint[1] * lon_feet)**2) < pullback:
print "Returning from Line_Segment_Length...\n";
} # sub Line_Segment_Length
# Give this an array of WKB Points (representing a Way)
# and this will return 2 new arrays of node IDs (LHS, RHS)
# that is cartographically offset from the old Way
# TODO: PULLBACK HANDLING
# TODO: Remove small line segments
sub Offset_Way
{
my($distance, $pullback, $calc_lhs, $calc_rhs, @node_wkbs) = @_;
my(@offset_node_ids_lhs) = ();
my(@offset_node_ids_rhs) = ();
my($hashref_tags_lhs);
$hashref_tags_lhs->{'testbuild'} = $testbuild;
$hashref_tags_lhs->{'test'} = 'from-left';
my($hashref_tags_rhs);
$hashref_tags_rhs->{'testbuild'} = $testbuild;
$hashref_tags_rhs->{'test'} = 'from-right';
print "Entering Offset_Way...\n";
print "With ".@node_wkbs." nodes in the Way ...\n";
# The approximate number of metres in one degree of latitude
my($metres_per_geo_degree_latitude) = 111134;
my($metres_per_geo_degree_longitude) = undef; # calculate this later as we crawl the way
# # In this version, naively calculate both offsets all the time
# my($calc_lhs) = 1;
# my($calc_rhs) = 1;
my(@coords_lhs_x) = ();
my(@coords_lhs_y) = ();
my(@coords_rhs_x) = ();
my(@coords_rhs_y) = ();
my(@lengths_lhs) = ();
my(@lengths_rhs) = ();
my($length_lhs) = undef;
my($length_rhs) = undef;
my(@prev_point_lon_lat) = ();
my($prev_delta_xp, $prev_delta_yp) = undef;
my($first_line_segment) = 1;
foreach $node_wkb (@node_wkbs)
{
my($node_lon, $node_lat) = XY_From_WKB_Point($node_wkb);
if ($#prev_point_lon_lat >= 0)
{
#print " prev_point_lon_lat is ".$#prev_point_lon_lat." @prev_point_lon_lat.\n";
# Co-located nodes? If so, ignore and try next Node on the Way.
next if
(
($prev_point_lon_lat[0] == $node_lon) and
($prev_point_lon_lat[1] == $node_lat)
);
# Calculate the approximate number of metres in one degree of longitude
my($lat_rad) = deg2rad($node_lat);
# my($metres_per_geo_degree_longitude) =
$metres_per_geo_degree_longitude =
111412.88 * cos($lat_rad)
- 93.50 * cos(3 * $lat_rad)
+ 0.12 * cos(5 * $lat_rad);
# Original values in feet
# 365527.822 * math.cos(lat_rad)
# - 306.75853 * math.cos(3 * lat_rad)
# + 0.3937 * math.cos(5 * lat_rad)
# Get delta between cartesian coordinates of this line segment
#print "m/lon ".($metres_per_geo_degree_longitude)."\n";
#print "dx ".($node_lon - $prev_point_lon_lat[0])."\n";
#print "dy ".($node_lat - $prev_point_lon_lat[1])."\n";
my($delta_x) = ($node_lon - $prev_point_lon_lat[0]) * $metres_per_geo_degree_longitude;
my($delta_y) = ($node_lat - $prev_point_lon_lat[1]) * $metres_per_geo_degree_latitude;
# Get delta of the perpendicular vector of this line segment
# my($theta) = (pi/2) - atan2($delta_y, $delta_x);
my($theta) = atan2($delta_y, $delta_x); # yes, this is a completely different formula to the python version
my($delta_xp) = sin($theta) * $distance;
my($delta_yp) = cos($theta) * $distance;
# if ($delta_y > 0)
# {
$delta_xp = -$delta_xp;
# }
# else
# {
# $delta_yp = -$delta_yp;
# }
# print " delta_x is $delta_x.\n";
# print " delta_y is $delta_y.\n";
# print " Theta is $theta radians.\n";
# print " delta_xp is $delta_xp.\n";
# print " delta_yp is $delta_yp.\n";
if ($first_line_segment)
{
# Deal with offsets of the first node here.
$hashref_tags_lhs->{'test'} .= " theta ".rad2deg($theta);
$hashref_tags_rhs->{'test'} .= " theta ".rad2deg($theta);
my($pullback_delta_x) = 0; # TODO - pullback handling
my($pullback_delta_y) = 0; # TODO - pullback handling
if ($calc_lhs)
{
my($offsetted_x) = $prev_point_lon_lat[0]
+ ($delta_xp / $metres_per_geo_degree_longitude)
- $pullback_delta_x;
my($offsetted_y) = $prev_point_lon_lat[1]
+ ($delta_yp / $metres_per_geo_degree_latitude)
- $pullback_delta_y;
# print " LHS offsetted_x is $offsetted_x.\n";
# print " LHS offsetted_y is $offsetted_y.\n";
push (@coords_lhs_x, $offsetted_x);
push (@coords_lhs_y, $offsetted_y);
# my($offsetted_wkb) = WKB_Point_From_XY($offsetted_x, $offsetted_y);
# my($offsetted_node_id) = Node_Insert($offsetted_wkb);
# print " LHS offsetted_node_id is $offsetted_node_id.\n";
# Node_Tags_Insert($hashref_tags_lhs, $offsetted_node_id);
# push(@offset_node_ids_lhs, $offsetted_node_id);
}
if ($calc_rhs)
{
my($offsetted_x) = $prev_point_lon_lat[0]
- ($delta_xp
/ $metres_per_geo_degree_longitude)
- $pullback_delta_x;
my($offsetted_y) = $prev_point_lon_lat[1]
- ($delta_yp
/ $metres_per_geo_degree_latitude)
- $pullback_delta_y;
# print " RHS offsetted_x is $offsetted_x.\n";
# print " RHS offsetted_y is $offsetted_y.\n";
push (@coords_rhs_x, $offsetted_x);
push (@coords_rhs_y, $offsetted_y);
# my($offsetted_wkb) = WKB_Point_From_XY($offsetted_x, $offsetted_y);
# my($offsetted_node_id) = Node_Insert($offsetted_wkb);
# print " RHS offsetted_node_id is $offsetted_node_id.\n";
# Node_Tags_Insert($hashref_tags_rhs, $offsetted_node_id);
# push(@offset_node_ids_rhs, $offsetted_node_id);
}
#print "Dealt with offsets of first node.\n";
$first_line_segment = 0;
}
else
{
# Deal with offsets of the intermediate nodes here.
$hashref_tags_lhs->{'test'} = 'intermediate-left';
$hashref_tags_rhs->{'test'} = 'intermediate-right';
$theta = abs( atan2($prev_delta_xp, $prev_delta_yp) );
$theta -= abs( atan2( $delta_xp, $delta_yp) );
my($r) = 1 + abs( tan($theta/2) );
$hashref_tags_lhs->{'test'} .= " theta ".rad2deg($theta);
$hashref_tags_rhs->{'test'} .= " theta ".rad2deg($theta);
$hashref_tags_lhs->{'test'} .= " r ".rad2deg($r);
$hashref_tags_rhs->{'test'} .= " r ".rad2deg($r);
if ($calc_lhs)
{
my($offsetted_x) = $prev_point_lon_lat[0]
+ (
($delta_xp + $prev_delta_xp)
* $r
/ ($metres_per_geo_degree_longitude * 2)
);
my($offsetted_y) = $prev_point_lon_lat[1]
+ (
($delta_yp + $prev_delta_yp)
* $r
/ ($metres_per_geo_degree_latitude * 2)
);
# print " LHS offsetted_x is $offsetted_x.\n";
# print " LHS offsetted_y is $offsetted_y.\n";
my($line_segment_distance) =
sqrt(
( ($offsetted_x - $coords_lhs_x[-1]) * $metres_per_geo_degree_longitude ) ** 2
+
( ($offsetted_y - $coords_lhs_y[-1]) * $metres_per_geo_degree_latitude ) ** 2
);
$length_lhs += $line_segment_distance;
# print " LHS length is $line_segment_distance, total $length_lhs.\n";
push (@lengths_lhs, $line_segment_distance);
push (@coords_lhs_x, $offsetted_x);
push (@coords_lhs_y, $offsetted_y);
# my($offsetted_wkb) = WKB_Point_From_XY($offsetted_x, $offsetted_y);
# my($offsetted_node_id) = Node_Insert($offsetted_wkb);
# print " LHS offsetted_node_id is $offsetted_node_id.\n";
# Node_Tags_Insert($hashref_tags_lhs, $offsetted_node_id);
# push(@offset_node_ids_lhs, $offsetted_node_id);
}
if ($calc_rhs)
{
my($offsetted_x) = $prev_point_lon_lat[0]
- (
($delta_xp + $prev_delta_xp)
* $r
/ ($metres_per_geo_degree_longitude * 2)
);
my($offsetted_y) = $prev_point_lon_lat[1]
- (
($delta_yp + $prev_delta_yp)
* $r
/ ($metres_per_geo_degree_latitude * 2)
);
# print " RHS offsetted_x is $offsetted_x.\n";
# print " RHS offsetted_y is $offsetted_y.\n";
my($line_segment_distance) =
sqrt(
( ($offsetted_x - $coords_rhs_x[-1]) * $metres_per_geo_degree_longitude ) ** 2
+
( ($offsetted_y - $coords_rhs_y[-1]) * $metres_per_geo_degree_latitude ) ** 2
);
$length_rhs += $line_segment_distance;
# print " RHS length is $line_segment_distance, total $length_rhs.\n";
push (@lengths_rhs, $line_segment_distance);
push (@coords_rhs_x, $offsetted_x);
push (@coords_rhs_y, $offsetted_y);
# my($offsetted_wkb) = WKB_Point_From_XY($offsetted_x, $offsetted_y);
# my($offsetted_node_id) = Node_Insert($offsetted_wkb);
# print " RHS offsetted_node_id is $offsetted_node_id.\n";
# Node_Tags_Insert($hashref_tags_rhs, $offsetted_node_id);
# push(@offset_node_ids_rhs, $offsetted_node_id);
}
}
($prev_delta_xp, $prev_delta_yp) = ($delta_xp, $delta_yp);
}
@prev_point_lon_lat = ($node_lon, $node_lat);
}
# TODO: Deal with offsets of the final node here.
#print "Final Node Offset, with m/lon ".($metres_per_geo_degree_longitude)."\n";
$hashref_tags_lhs->{'test'} = 'to-left';
$hashref_tags_rhs->{'test'} = 'to-right';
if ($calc_lhs)
{
my($offsetted_x) = $prev_point_lon_lat[0]
+ ($prev_delta_xp
/ $metres_per_geo_degree_longitude)
+ $pullback_delta_x;
my($offsetted_y) = $prev_point_lon_lat[1]
+ ($prev_delta_yp
/ $metres_per_geo_degree_latitude)
+ $pullback_delta_y;
# print " LHS offsetted_x is $offsetted_x.\n";
# print " LHS offsetted_y is $offsetted_y.\n";
my($line_segment_distance) =
sqrt(
( ($offsetted_x - $coords_lhs_x[-1]) * $metres_per_geo_degree_longitude ) ** 2
+
( ($offsetted_y - $coords_lhs_y[-1]) * $metres_per_geo_degree_latitude ) ** 2
);
$length_lhs += $line_segment_distance;
# print " LHS length is $line_segment_distance, total $length_lhs.\n";
push (@lengths_lhs, $line_segment_distance);
push (@coords_lhs_x, $offsetted_x);
push (@coords_lhs_y, $offsetted_y);
# my($offsetted_wkb) = WKB_Point_From_XY($offsetted_x, $offsetted_y);
# my($offsetted_node_id) = Node_Insert($offsetted_wkb);
# print " LHS offsetted_node_id is $offsetted_node_id.\n";
# Node_Tags_Insert($hashref_tags_lhs, $offsetted_node_id);
# push(@offset_node_ids_lhs, $offsetted_node_id);
}
if ($calc_rhs)
{
my($offsetted_x) = $prev_point_lon_lat[0]
- ($prev_delta_xp / $metres_per_geo_degree_longitude)
+ $pullback_delta_x;
my($offsetted_y) = $prev_point_lon_lat[1]
- ($prev_delta_yp / $metres_per_geo_degree_latitude)
+ $pullback_delta_y;
# print " RHS offsetted_x is $offsetted_x.\n";
# print " RHS offsetted_y is $offsetted_y.\n";
my($line_segment_distance) =
sqrt(
( ($offsetted_x - $coords_rhs_x[-1]) * $metres_per_geo_degree_longitude ) ** 2
+
( ($offsetted_y - $coords_rhs_y[-1]) * $metres_per_geo_degree_latitude ) ** 2
);
$length_rhs += $line_segment_distance;
# print " RHS length is $line_segment_distance, total $length_rhs.\n";
push (@lengths_rhs, $line_segment_distance);
push (@coords_rhs_x, $offsetted_x);
push (@coords_rhs_y, $offsetted_y);
# my($offsetted_wkb) = WKB_Point_From_XY($offsetted_x, $offsetted_y);
# my($offsetted_node_id) = Node_Insert($offsetted_wkb);
# print " RHS offsetted_node_id is $offsetted_node_id.\n";
# Node_Tags_Insert($hashref_tags_rhs, $offsetted_node_id);
# push(@offset_node_ids_rhs, $offsetted_node_id);
}
# Trim the ends off the 2 offset lines
# Also smooth them out a bit - no less than a few metres between points
if ($calc_lhs)
{
# print " LHS is of length $length_lhs\n";
@offset_node_ids_lhs = ();
# TODO: remove all earlier assignments to @offset_node_ids_lhs in this function
my($measured_length_last_node) = undef;
# Is it long enough to bother building a cartographic object?
if ($length_lhs > (3 * $address_pullback) ) # heuristic
{
# Apply pullback at start and end of this Way
my($pullback_from) = $address_pullback;
my($pullback_to) = $length_lhs - $address_pullback;
my($measured_length_0) = 0;
my($measured_length_1) = 0;
foreach $i (0..$#lengths_lhs)
{
$measured_length_1 = $measured_length_0 + $lengths_lhs[$i];
# print " LHS segment $i from $measured_length_0 to $measured_length_1\n";
my($offsetted_wkb) = undef;
if ($measured_length_0 < $pullback_from)
{
if ($measured_length_1 > $pullback_from)
{
# Apply pullback at start of Way
# print " LHS pullback starts at $pullback_from\n";
my($invalid_proportion) = ($pullback_from - $measured_length_0)
/ ($measured_length_1 - $measured_length_0);
# print " LHS invalid proportion is $invalid_proportion\n";
my($delta_x) = ($coords_lhs_x[$i+1] - $coords_lhs_x[$i]);
my($delta_y) = ($coords_lhs_y[$i+1] - $coords_lhs_y[$i]);
my($pulled_back_x) = $coords_lhs_x[$i]
+ ($invalid_proportion * $delta_x);
my($pulled_back_y) = $coords_lhs_y[$i]
+ ($invalid_proportion * $delta_y);
# print " LHS delta x is $delta_x\n";
# print " LHS delta y is $delta_y\n";
# print " LHS pulled back x is $pulled_back_x\n";
# print " LHS pulled back y is $pulled_back_y\n";
$offsetted_wkb = WKB_Point_From_XY($pulled_back_x, $pulled_back_y);
$measured_length_last_node = $pullback_from;
}
}
elsif ($measured_length_1 > $pullback_to)
{
if ($measured_length_0 < $pullback_to)
{
# Apply pullback at end of way
# print " LHS pullback ends at $pullback_to\n";
my($valid_proportion) = ($pullback_to - $measured_length_0)
/ ($measured_length_1 - $measured_length_0);
# print " LHS valid proportion is $valid_proportion\n";
my($delta_x) = ($coords_lhs_x[$i+1] - $coords_lhs_x[$i]);
my($delta_y) = ($coords_lhs_y[$i+1] - $coords_lhs_y[$i]);
my($pulled_back_x) = $coords_lhs_x[$i]
+ ($valid_proportion * $delta_x);
my($pulled_back_y) = $coords_lhs_y[$i]
+ ($valid_proportion * $delta_y);
# print " LHS delta x is $delta_x\n";
# print " LHS delta y is $delta_y\n";
# print " LHS pulled back x is $pulled_back_x\n";
# print " LHS pulled back y is $pulled_back_y\n";
$offsetted_wkb = WKB_Point_From_XY($pulled_back_x, $pulled_back_y);
$measured_length_last_node = $pullback_to;
}
}
else
# if (
# ($measured_length_0 > $pullback_from)
# and
# ($measured_length_0 < $pullback_to)
# )
{
# a coordinate not affected by pullback
# check it for smoothness though
if (($measured_length_0
- $measured_length_last_node)
> 3) # heuristic
# > 50) # debugging - make it obvious
{
$offsetted_wkb = WKB_Point_From_XY($coords_lhs_x[$i], $coords_lhs_y[$i]);
$measured_length_last_node = $measured_length_0;
}
}
if ($offsetted_wkb)
{
# Insert point into finalised offset Way
my($offsetted_node_id) = Node_Insert($offsetted_wkb);
# print " LHS offsetted_node_id is $offsetted_node_id.\n";
push(@offset_node_ids_lhs, $offsetted_node_id);
}
$measured_length_0 = $measured_length_1;
}
}
}
# TODO: Consider refactoring LHS and RHS into its own function
if ($calc_rhs)
{
# print " RHS is of length $length_lhs\n";
@offset_node_ids_rhs = ();
# TODO: remove all earlier assignments to @offset_node_ids_rhs in this function
my($measured_length_last_node) = undef;
# Is it long enough to bother building a cartographic object?
if ($length_rhs > (3 * $address_pullback) ) # heuristic
{
# Apply pullback at start and end of this Way
my($pullback_from) = $address_pullback;
my($pullback_to) = $length_rhs - $address_pullback;
my($measured_length_0) = 0;
my($measured_length_1) = 0;
foreach $i (0..$#lengths_rhs)
{
$measured_length_1 = $measured_length_0 + $lengths_rhs[$i];
# print " RHS segment $i from $measured_length_0 to $measured_length_1\n";
my($offsetted_wkb) = undef;
if ($measured_length_0 < $pullback_from)
{
if ($measured_length_1 > $pullback_from)
{
# Apply pullback at start of Way
# print " RHS pullback starts at $pullback_from\n";
my($invalid_proportion) = ($pullback_from - $measured_length_0)
/ ($measured_length_1 - $measured_length_0);
# print " RHS invalid proportion is $invalid_proportion\n";
my($delta_x) = ($coords_rhs_x[$i+1] - $coords_rhs_x[$i]);
my($delta_y) = ($coords_rhs_y[$i+1] - $coords_rhs_y[$i]);
my($pulled_back_x) = $coords_rhs_x[$i]
+ ($invalid_proportion * $delta_x);
my($pulled_back_y) = $coords_rhs_y[$i]
+ ($invalid_proportion * $delta_y);
# print " RHS delta x is $delta_x\n";
# print " RHS delta y is $delta_y\n";
# print " RHS pulled back x is $pulled_back_x\n";
# print " RHS pulled back y is $pulled_back_y\n";
$offsetted_wkb = WKB_Point_From_XY($pulled_back_x, $pulled_back_y);
$measured_length_last_node = $pullback_from;
}
}
elsif ($measured_length_1 > $pullback_to)
{
if ($measured_length_0 < $pullback_to)
{
# Apply pullback at end of way
# print " RHS pullback ends at $pullback_to\n";
my($valid_proportion) = ($pullback_to - $measured_length_0)
/ ($measured_length_1 - $measured_length_0);
# print " RHS valid proportion is $valid_proportion\n";
my($delta_x) = ($coords_rhs_x[$i+1] - $coords_rhs_x[$i]);
my($delta_y) = ($coords_rhs_y[$i+1] - $coords_rhs_y[$i]);
my($pulled_back_x) = $coords_rhs_x[$i]
+ ($valid_proportion * $delta_x);
my($pulled_back_y) = $coords_rhs_y[$i]
+ ($valid_proportion * $delta_y);
# print " RHS delta x is $delta_x\n";
# print " RHS delta y is $delta_y\n";
# print " RHS pulled back x is $pulled_back_x\n";
# print " RHS pulled back y is $pulled_back_y\n";
$offsetted_wkb = WKB_Point_From_XY($pulled_back_x, $pulled_back_y);
$measured_length_last_node = $pullback_to;
}
}
else
# if (
# ($measured_length_0 > $pullback_from)
# and
# ($measured_length_0 < $pullback_to)
# )
{
# a coordinate not affected by pullback
# check it for smoothness though
if (($measured_length_0
- $measured_length_last_node)
> 3) # heuristic
# > 50) # debugging - make it obvious
{
$offsetted_wkb = WKB_Point_From_XY($coords_rhs_x[$i], $coords_rhs_y[$i]);
$measured_length_last_node = $measured_length_0;
}
}
if ($offsetted_wkb)
{
# Insert point into finalised offset Way
my($offsetted_node_id) = Node_Insert($offsetted_wkb);
# print " RHS offsetted_node_id is $offsetted_node_id.\n";
push(@offset_node_ids_rhs, $offsetted_node_id);
}
$measured_length_0 = $measured_length_1;
}
}
}
print "Returning from Offset_Way...\n";
return (\@offset_node_ids_lhs,
\@offset_node_ids_rhs);
} # sub Offset_Way
# give this the coordinates to insert, returns the corresponding node id
# for feeding into ways, tags, relations.
sub Node_Insert
{
my($geom) = @_;
my($node_id);
if (!$sth_destination_node)
{
$sth_destination_node =
$dbh_destination->prepare(
'INSERT INTO nodes '.
'('.
'id, '.
'version, '.
'user_id, '.
'tstamp, '.
'changeset_id, '.
'geom'.
') '.
'VALUES '.
'(?,?,?,?,?,?)'
) or die "Can't prepare statement: $DBI::errstr";
}
if (!$sth_destination_node_test)
{
$sth_destination_node_test =
$dbh_destination->prepare(
'SELECT id '.
'FROM nodes '.
# 'WHERE geom = ?' # Causes seq scan
'WHERE geom && ?' # Causes index scan
) or die "Can't prepare statement: $DBI::errstr";
}
# Test for existing destination point and merge
my $rc = $sth_destination_node_test->execute
(
$geom
)
or die "Can't execute statement: $DBI::errstr";
my @row = $sth_destination_node_test->fetchrow_array;
if ($row[0])
{
print " Existing id for this point is ".$row[0].".\n";
$node_id = $row[0];
# sleep 2;
}
else
{
$node_id = $destination_node_lw;
my $rc = $sth_destination_node->execute
(
$node_id,
1,
1,
'NOW()',
1,
$geom
)
or die "Can't execute statement: $DBI::errstr";
$destination_node_lw--;
$changeset_element_count++;
}
# Test for excessive changeset element count here as well
# to save processing very large geometries that won't fit anyway.
if ($changeset_element_count > $osmapi_changeset_chunk_size)
{
# uh oh, roll back the last transaction,
# tell the user what to do next and wrap it up.
# TODO
$dbh_destination->rollback();
print "\n\nCHANGESET LIMIT REACHED!\n\n";
print
print "\n".
"Destination database is now ready for use by\n".
" 'osmosis --read-pgsql ... --dataset-dump ... --derive-change'.\n\n";
print "After you have used osmosis on the destination database,\n".
"change this script so that:\n".
' $start_origin_from_table = '."'".$table_name."'"."\n".
' $start_origin_from_row = '.$origin_row_number."\n\n".
"Then run this script again for the next pass.\n";
exit;
}
print ".";
return $node_id;
} # sub Node_Insert
# give this the Node IDs to insert, returns the corresponding way id
# for feeding into tags, relations.
sub Way_Insert
{
my(@node_ids) = @_;
my($way_id);
my(@way_ids) = ();
if (!$sth_destination_way)
{
$sth_destination_way =
$dbh_destination->prepare(
'INSERT INTO ways '.
'('.
'id, '.
'version, '.
'user_id, '.
'tstamp, '.
'changeset_id '.
') '.
'VALUES '.
'(?,?,?,?,?)'
) or die "Can't prepare statement: $DBI::errstr";
}
if (!$sth_destination_way_nodes)
{
$sth_destination_way_nodes =
$dbh_destination->prepare(
'INSERT INTO way_nodes '.
'('.