function remap_all_tags(&$input_osm_ways)
{
    $input_nodes = $input_osm_ways->nodes;
    $input_ways = $input_osm_ways->ways;
    $result = new OSMWays();
    $state_name_map = array();
    foreach ($input_ways as $input_way) {
        $tags = $input_way['tags'];
        $new_tags = remap_way_tags($tags, $state_name_map);
        if (!isset($new_tags)) {
            continue;
        }
        $result->begin_way();
        foreach ($input_way['nds'] as $nd_ref) {
            if (!isset($input_nodes[$nd_ref])) {
                continue;
            }
            $node = $input_nodes[$nd_ref];
            $result->add_vertex($node['lat'], $node['lon']);
        }
        foreach ($new_tags as $key => $value) {
            $result->add_tag($key, $value);
        }
        $result->end_way();
    }
    return $result;
}
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
require_once 'cliargs.php';
require_once 'osmways.php';
ini_set('memory_limit', '-1');
$cliargs = array('osminput' => array('short' => 'i', 'type' => 'required', 'description' => 'The file to read the OSM ways from'), 'phpoutput' => array('short' => 'o', 'type' => 'optional', 'description' => 'The file to write the translation table to - if unset, will write to stdout', 'default' => 'php://stdout'), 'osmoutput' => array('short' => 'u', 'type' => 'optional', 'description' => 'The file to write the fixed-up OSM geometry to - if unset, will write to stdout', 'default' => 'php://stdout'));
$options = cliargs_get_options($cliargs);
$osm_input = $options['osminput'];
$php_output = $options['phpoutput'];
$osm_output = $options['osmoutput'];
$input_osm_ways = new OSMWays();
$input_contents = file_get_contents($osm_input) or die("Couldn't read file '{$osm_input}'");
$input_osm_ways->deserialize_from_xml($input_contents);
$output_osm_ways = new OSMWays();
$input_nodes = $input_osm_ways->nodes;
$district_map = array();
$constituency_map = array();
foreach ($input_osm_ways->ways as &$way) {
    $tags =& $way['tags'];
    $district_name = $tags['name_0'];
    $district_name = ucwords($district_name);
    $constituency_name = $tags['name_1'];
    if (!isset($district_map[$district_name])) {
        $district_map[$district_name] = count($district_map);
    }
    $district_code = $district_map[$district_name];
    if (!isset($constituency_map[$constituency_name])) {
        $constituency_map[$constituency_name] = count($constituency_map);
    }
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
require_once 'cliargs.php';
require_once 'osmways.php';
ini_set('memory_limit', '-1');
$cliargs = array('inputfile' => array('short' => 'i', 'type' => 'optional', 'description' => '', 'default' => 'php://stdin'), 'outputfile' => array('short' => 'o', 'type' => 'optional', 'description' => '', 'default' => 'php://stdout'));
$options = cliargs_get_options($cliargs);
$input_file = $options['inputfile'];
$output_file = $options['outputfile'];
$input_osm_ways = new OSMWays();
$input_contents = file_get_contents($input_file) or die("Couldn't read file '{$input_file}'");
$input_osm_ways->deserialize_from_xml($input_contents);
$output = array();
$input_nodes = $input_osm_ways->nodes;
$input_ways = $input_osm_ways->ways;
foreach ($input_ways as $way) {
    $name_variants = $way['tags']['name_variants'];
    $state_parts = explode('|', $name_variants);
    $state_code = $state_parts[0];
    if (!isset($output[$state_code])) {
        $output[$state_code] = array();
    }
    $polygon = array();
    foreach ($way['nds'] as $nd_ref) {
        if (!isset($input_nodes[$nd_ref])) {
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
require_once 'cliargs.php';
require_once 'osmways.php';
ini_set('memory_limit', '-1');
$cliargs = array('osminput' => array('short' => 'i', 'type' => 'required', 'description' => 'The file to read the OSM ways from'), 'csvinput' => array('short' => 'c', 'type' => 'required', 'description' => 'The file to read the constituency code data from'), 'phpoutput' => array('short' => 'o', 'type' => 'optional', 'description' => 'The file to write the translation table to - if unset, will write to stdout', 'default' => 'php://stdout'), 'osmoutput' => array('short' => 's', 'type' => 'required', 'description' => 'The file to write the updated OSM data to'));
$options = cliargs_get_options($cliargs);
$osm_input = $options['osminput'];
$csv_input = $options['csvinput'];
$php_output = $options['phpoutput'];
$osm_output = $options['osmoutput'];
$input_osm_ways = new OSMWays();
$input_contents = file_get_contents($osm_input) or die("Couldn't read file '{$osm_input}'");
$input_osm_ways->deserialize_from_xml($input_contents);
$csv_input_handle = fopen($csv_input, 'rb') or die("Couldn't read file '{$csv_input}'");
$code_map = array();
$line_index = 0;
while (!feof($csv_input_handle)) {
    $current_parts = fgetcsv($csv_input_handle);
    if (empty($current_parts)) {
        continue;
    }
    $line_index += 1;
    if ($line_index < 2) {
        continue;
    }
    $code = $current_parts[0];
function close_ways($input_osm_ways, $verbose, $tolerance, $debug)
{
    if ($verbose) {
        error_log("Starting close_ways()");
    }
    $bucket_debug = $debug && false;
    $bucket_grid = new BucketGrid($tolerance * 2);
    $input_ways =& $input_osm_ways->ways;
    $input_nodes =& $input_osm_ways->nodes;
    $way_index = 0;
    foreach ($input_ways as $way_id => &$way) {
        $way_index += 1;
        if ($verbose && $way_index % 100 === 0) {
            error_log("Bucketed {$way_index}/" . count($input_ways));
        }
        if ($way['is_closed'] === true) {
            continue;
        }
        $nds = $way['nds'];
        $nds_count = count($nds);
        if ($nds_count < 2) {
            continue;
        }
        $start_index = 0;
        $end_index = $nds_count - 1;
        $start_nd_ref = $nds[$start_index];
        $end_nd_ref = $nds[$end_index];
        $start_node = $input_nodes[$start_nd_ref];
        $end_node = $input_nodes[$end_nd_ref];
        $start_lat = $start_node['lat'];
        $start_lon = $start_node['lon'];
        $end_lat = $end_node['lat'];
        $end_lon = $end_node['lon'];
        $start_data = array('way_id' => $way_id, 'nd_ref' => $start_nd_ref, 'index' => $start_index, 'lat' => $start_lat, 'lon' => $start_lon);
        $end_data = array('way_id' => $way_id, 'nd_ref' => $end_nd_ref, 'index' => $end_index, 'lat' => $end_lat, 'lon' => $end_lon);
        if ($bucket_debug) {
            error_log("insert_point({$start_lon}, {$start_lat}, " . print_r($start_data, true) . ")");
        }
        $bucket_grid->insert_point($start_lon, $start_lat, $start_data, $bucket_debug);
        if ($bucket_debug) {
            error_log("insert_point({$end_lon}, {$end_lat}, " . print_r($end_data, true) . ")");
        }
        $bucket_grid->insert_point($end_lon, $end_lat, $end_data, $bucket_debug);
    }
    if ($bucket_debug) {
        error_log("bucket_grid: " . print_r($bucket_grid->buckets, true));
    }
    $result = new OSMWays();
    $way_index = 0;
    foreach ($input_ways as $way_id => &$way) {
        $way_index += 1;
        if ($verbose && $way_index % 100 === 0) {
            error_log("Closing {$way_index}/" . count($input_ways));
        }
        if ($way['is_closed'] === true) {
            if ($debug) {
                error_log("Found pre-closed way");
            }
            $result->copy_way($way, $input_osm_ways);
            continue;
        }
        if (!empty($way['is_used'])) {
            if ($debug) {
                error_log("Found already used way");
            }
            continue;
        }
        $input_tags = $way['tags'];
        if (count($way['nds']) < 2) {
            if ($debug) {
                error_log("Too few nodes found for {$way_id}");
            }
            continue;
        }
        $start_index = 0;
        $start_way_id = $way_id;
        $start_nd_ref = $way['nds'][$start_index];
        $start_node = $input_nodes[$start_nd_ref];
        $follow_way_id = $way_id;
        $nds = $way['nds'];
        if ($debug) {
            error_log("Looking at {$follow_way_id}");
        }
        $output_nds = array();
        $loop_count = 0;
        while (true) {
            $input_ways[$follow_way_id]['is_used'] = true;
            $nds_count = count($nds);
            $end_index = $nds_count - 1;
            $output_nds = array_merge($output_nds, $nds);
            $end_nd_ref = $nds[$end_index];
            $end_node = $input_nodes[$end_nd_ref];
            if ($debug) {
                error_log("End node is {$end_nd_ref} at (" . $end_node['lat'] . "," . $end_node['lon'] . ")");
            }
            $distance_to_start = get_node_distance($end_node, $start_node);
            // Have we looped back around to the start?
            if ($distance_to_start < $tolerance) {
                if ($debug) {
                    error_log("Closed way starting with {$start_way_id}, ending with {$follow_way_id}");
                }
                $output_nds[] = $start_nd_ref;
                $result->begin_way();
                foreach ($output_nds as $nd_ref) {
                    $node = $input_nodes[$nd_ref];
                    $result->add_vertex($node['lat'], $node['lon'], $bucket_debug);
                }
                foreach ($input_tags as $key => $value) {
                    $result->add_tag($key, $value);
                }
                $result->end_way();
                break;
            }
            // Figure out which lines we can connect to the end of this one
            $end_lat = $end_node['lat'];
            $end_lon = $end_node['lon'];
            $nearby_points = $bucket_grid->find_points_near($end_lon, $end_lat, $tolerance, $bucket_debug);
            if ($bucket_debug) {
                error_log("find_points_near({$end_lon}, {$end_lat}, {$tolerance}) returned " . print_r($nearby_points, true));
            }
            $closest_distance = null;
            foreach ($nearby_points as $bucket_entry) {
                $point_data = $bucket_entry['data'];
                $point_lat = $point_data['lat'];
                $point_lon = $point_data['lon'];
                $point_way_id = $point_data['way_id'];
                $point_nd_ref = $point_data['nd_ref'];
                if ($point_nd_ref === $end_nd_ref && $point_way_id === $follow_way_id) {
                    if ($bucket_debug) {
                        error_log("{$point_nd_ref} was the same as the start");
                    }
                    continue;
                }
                $distance = get_node_distance($point_data, $end_node);
                if ($bucket_debug) {
                    error_log("{$point_nd_ref} was {$distance} away");
                }
                if ($closest_distance === null || $distance < $closest_distance) {
                    $closest_distance = $distance;
                }
            }
            // Have we reached the end of the line?
            if (!isset($closest_distance)) {
                if ($debug) {
                    error_log("No close points found for {$follow_way_id}");
                }
                break;
            }
            $found_nodes = array();
            foreach ($nearby_points as $bucket_entry) {
                $point_data = $bucket_entry['data'];
                $point_lat = $point_data['lat'];
                $point_lon = $point_data['lon'];
                $point_way_id = $point_data['way_id'];
                $point_nd_ref = $point_data['nd_ref'];
                if ($point_nd_ref === $end_nd_ref && $point_way_id === $follow_way_id) {
                    continue;
                }
                $distance = get_node_distance($point_data, $end_node);
                if ($distance < $closest_distance + EPSILON) {
                    $found_nodes[] = $point_data;
                }
            }
            // Figure out which way edge to follow
            $found_nodes_count = count($found_nodes);
            if ($found_nodes_count === 1) {
                if ($debug) {
                    error_log("A single close point found for {$follow_way_id}");
                }
                $follow_node_index = 0;
            } else {
                if ($debug) {
                    error_log("Too many close points found for {$follow_way_id}");
                }
                break;
            }
            $follow_node = $found_nodes[$follow_node_index];
            $follow_way_id = $follow_node['way_id'];
            $follow_way = $input_ways[$follow_way_id];
            $do_reverse = $follow_node['index'] > 0;
            $nds = $follow_way['nds'];
            if ($do_reverse) {
                $nds = array_reverse($nds);
            }
            $loop_count += 1;
            if ($loop_count > 1000) {
                die("Looped too many times\n");
            }
        }
    }
    if ($verbose) {
        error_log("Finished close_ways()");
    }
    return $result;
}
            error_log("Processed {$count} ways");
        }
        $tags = $input_way['tags'];
        if (!isset($tags['ISO'])) {
            die("Country code not found in {$count}: " . print_r($input_way, true));
        }
        $country_code = $tags['ISO'];
        if (!isset($result[$country_code])) {
            $result[$country_code] = new OSMWays();
        }
        $result[$country_code]->copy_way($input_way, $input_osm_ways);
    }
    return $result;
}
function save_country_maps($output_maps, $output_folder)
{
    foreach ($output_maps as $country_code => $output_osm_ways) {
        $output_file = $output_folder . '/' . strtolower($country_code) . '_state.osm';
        $output_contents = $output_osm_ways->serialize_to_xml();
        file_put_contents($output_file, $output_contents) or die("Couldn't write file '{$output_file}'");
    }
}
$cliargs = array('inputfile' => array('short' => 'i', 'type' => 'optional', 'description' => 'The file to read the input OSM XML data from - if unset, will read from stdin', 'default' => 'php://stdout'), 'outputfolder' => array('short' => 'o', 'type' => 'required', 'description' => 'The directory to write the state maps for each country to'));
$options = cliargs_get_options($cliargs);
$input_file = $options['inputfile'];
$output_folder = $options['outputfolder'];
$input_osm_ways = new OSMWays();
$input_contents = file_get_contents($input_file) or die("Couldn't read file '{$input_file}'");
$input_osm_ways->deserialize_from_xml($input_contents);
$output_maps = split_by_country($input_osm_ways);
save_country_maps($output_maps, $output_folder);
function merge_nodes_on_edges($input_osm_ways, $verbose, $tolerance, $debug)
{
    if ($verbose) {
        error_log("Starting merge_nodes_on_edges()");
    }
    $bucket_debug = $debug;
    $input_ways =& $input_osm_ways->ways;
    $input_nodes =& $input_osm_ways->nodes;
    $result = new OSMWays();
    $way_index = 0;
    foreach ($input_ways as $way_id => $way) {
        $way_index += 1;
        //        if ($verbose&&(($way_index%100)===0))
        error_log("Merged {$way_index}/" . count($input_ways));
        $nds = $way['nds'];
        $nds_count = count($nds);
        if ($nds_count < 2) {
            $result->copy_way($way, $input_osm_ways);
            if ($debug) {
                error_log("Skipping {$way_id}");
            }
            continue;
        }
        $result->begin_way($way_id);
        foreach ($way['tags'] as $key => $value) {
            $result->add_tag($key, $value);
        }
        $nds_map = array_count_values($nds);
        $node_index = 0;
        foreach ($nds as $nd_ref) {
            $is_last = $node_index == $nds_count - 1;
            $node_index += 1;
            if (!isset($input_nodes[$nd_ref])) {
                if ($debug) {
                    error_log("Missing node {$nd_ref} in {$way_id}");
                }
                continue;
            }
            $node = $input_nodes[$nd_ref];
            $start_x = $node['lat'];
            $start_y = $node['lon'];
            if ($debug) {
                error_log("Adding original {$nd_ref} ({$start_x}, {$start_y})");
            }
            $result->add_vertex($start_x, $start_y);
            if ($is_last) {
                continue;
            }
            $end_nd_ref = $nds[$node_index];
            $end_node = $input_nodes[$end_nd_ref];
            $end_x = $end_node['lat'];
            $end_y = $end_node['lon'];
            $coincident_points = $input_osm_ways->bucket_grid->find_points_near_line($start_x, $start_y, $end_x, $end_y, $tolerance, $bucket_debug);
            $sortfunction = create_function('$a, $b', 'if ($a["output_s"]>$b["output_s"]) return 1; else return -1;');
            usort($coincident_points, $sortfunction);
            foreach ($coincident_points as $point) {
                $s = $point['output_s'];
                if ($s < 0.0 || $s > 1.0) {
                    continue;
                }
                $point_nd_ref = $point['data']['id'];
                if (isset($nds_map[$point_nd_ref])) {
                    continue;
                }
                if ($debug) {
                    error_log("Adding {$point_nd_ref}");
                }
                $point_x = $point['x'];
                $point_y = $point['y'];
                $result->add_vertex($point_x, $point_y);
            }
        }
        $result->end_way();
    }
    if ($verbose) {
        error_log("Finished close_ways()");
    }
    return $result;
}
function decimate_ways($input_osm_ways, $decimate)
{
    if ($decimate == 0) {
        return $input_osm_ways;
    }
    $frequency = 100.0 / $decimate;
    $result = new OSMWays();
    $input_ways = $input_osm_ways->ways;
    $input_nodes = $input_osm_ways->nodes;
    foreach ($input_ways as $way) {
        $result->begin_way();
        foreach ($way['tags'] as $key => $value) {
            $result->add_tag($key, $value);
        }
        $nds_count = count($way['nds']);
        $nd_index = 0;
        foreach ($way['nds'] as $nd_ref) {
            $is_first = $nd_index == 0;
            $is_last = $nd_index == $nds_count - 1;
            $nd_index += 1;
            if (!isset($input_nodes[$nd_ref])) {
                continue;
            }
            $mod = fmod($nd_index, $frequency);
            $is_keeper = $mod < 0.998;
            $use_vertex = $is_first || $is_last || $is_keeper;
            if ($use_vertex) {
                $node = $input_nodes[$nd_ref];
                $result->add_vertex($node['lat'], $node['lon']);
            }
        }
        $result->end_way();
    }
    return $result;
}
        $current_parts = fgetcsv($file_handle);
        $line_index += 1;
        if ($line_index < 2) {
            continue;
        }
        $bla_code = $current_parts[0];
        $fips_code = $current_parts[1];
        $series_code = $current_parts[2];
        if (!isset($result[$bla_code])) {
            $result[$bla_code] = array();
        }
        $result[$bla_code][] = array('fips_code' => $fips_code, 'series_code' => $series_code);
    }
    fclose($file_handle);
    return $result;
}
$cliargs = array('inputfile' => array('short' => 'i', 'type' => 'optional', 'description' => 'The folder containing the unemployment data from the BLS - if unset, will read from stdin', 'default' => 'php://stdin'), 'blatofipsfile' => array('short' => 'b', 'type' => 'required', 'description' => 'The file containing the mapping of BLA codes to FIPS'), 'outputfile' => array('short' => 'o', 'type' => 'optional', 'description' => 'The file to write the output csv data to - if unset, will write to stdout', 'default' => 'php://stdout'), 'waysfile' => array('short' => 'w', 'type' => 'optional', 'description' => 'A file containing the way shapes that this data will be drawn onto', 'default' => ''));
$options = cliargs_get_options($cliargs);
$input_file = $options['inputfile'];
$output_file = $options['outputfile'];
$bla_to_fips_file = $options['blatofipsfile'];
$ways_file = $options['waysfile'];
if (!empty($ways_file)) {
    $osm_ways = new OSMWays();
    $osm_xml_string = file_get_contents($ways_file) or die("Couldn't open '{$ways_file}' for reading");
    $osm_ways->deserialize_from_xml($osm_xml_string);
} else {
    $osm_ways = null;
}
$bla_to_fips = load_bla_to_fips($bla_to_fips_file);
convert_unemployment_file($input_file, $output_file, $bla_to_fips, $osm_ways);
foreach ($g_input_files as $input_file => $transform_info) {
    error_log('Working on ' . $input_file . "\n");
    $input_file_path = $input_path . $input_file;
    $input_osm_ways = new OSMWays();
    $input_contents = file_get_contents($input_file_path) or die("Couldn't read file '{$input_file_path}'");
    $input_osm_ways->deserialize_from_xml($input_contents);
    $country_code = $transform_info['country_code'];
    $code = $transform_info['code'];
    $type = $transform_info['type'];
    if (isset($transform_info['other_code'])) {
        $other_code = $transform_info['other_code'];
    } else {
        $other_code = NULL;
    }
    if (!isset($output_osm_ways)) {
        $output_osm_ways = new OSMWays();
    }
    $input_ways = $input_osm_ways->ways;
    foreach ($input_ways as $way) {
        $input_nodes = $input_osm_ways->nodes;
        $output_osm_ways->begin_way();
        $tags = $way['tags'];
        if ($input_file == 'us_congress.osm') {
            $state_code = $tags['state_code'];
            $district_code = $tags['district_code'];
            if (empty($state_code) || $district_code == '98') {
                continue;
            }
            if (!isset($g_fips_to_postal[$state_code])) {
                error_log('No postal for ' . $state_code);
            }
            continue;
        }
        $name = $tags['name'];
        if (!isset($country_translation_table[$name])) {
            error_log("No code found for '{$name}'");
            continue;
        }
        $tags['country_code'] = $country_translation_table[$name];
    }
    if ($verbose) {
        error_log("Finished add_country_tags()");
    }
}
ini_set('memory_limit', '-1');
$cliargs = array('inputfile' => array('short' => 'i', 'type' => 'optional', 'description' => 'The file to read the input OSM XML data from - if unset, will read from stdin', 'default' => 'php://stdout'), 'outputfile' => array('short' => 'o', 'type' => 'optional', 'description' => 'The file to write the output OSM XML data to - if unset, will write to stdout', 'default' => 'php://stdout'), 'verbose' => array('short' => 'v', 'type' => 'switch', 'description' => 'Whether to show extra debugging information about the processing as it happens'));
$options = cliargs_get_options($cliargs);
$input_file = $options['inputfile'];
$output_file = $options['outputfile'];
$verbose = $options['verbose'];
if ($verbose) {
    error_log("Starting load of '{$input_file}'");
}
$input_osm_ways = new OSMWays();
$input_contents = file_get_contents($input_file) or die("Couldn't read file '{$input_file}'");
$input_osm_ways->deserialize_from_xml($input_contents);
add_country_tags($input_osm_ways, $verbose);
if ($verbose) {
    error_log("Starting save of '{$output_file}'");
}
$output_contents = $input_osm_ways->serialize_to_xml();
file_put_contents($output_file, $output_contents) or die("Couldn't write file '{$output_file}'");
    $normalizeChars = array('Š' => 'S', 'š' => 's', 'Ð' => 'Dj', 'Ž' => 'Z', 'ž' => 'z', 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'A', 'Ç' => 'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ý' => 'Y', 'Þ' => 'B', 'ß' => 'Ss', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'a', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'o', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ý' => 'y', 'ý' => 'y', 'þ' => 'b', 'ÿ' => 'y', 'ƒ' => 'f');
    return strtr($name, $normalizeChars);
}
//$new_name = clean_name("Finström");
//die($new_name."\n");
ini_set('memory_limit', '-1');
$cliargs = array('inputfolder' => array('short' => 'i', 'type' => 'required', 'description' => 'The folder containing the input state maps'), 'phpoutput' => array('short' => 'o', 'type' => 'optional', 'description' => 'The file to write the translation table to - if unset, will write to stdout', 'default' => 'php://stdout'));
$options = cliargs_get_options($cliargs);
$input_folder = $options['inputfolder'];
$php_output = $options['phpoutput'];
$name_to_state_code = array();
$state_code_info = array();
$country_bounding_boxes = array();
foreach (glob($input_folder . '/*.osm') as $input_file) {
    error_log("Looking at '{$input_file}'");
    $input_osm_ways = new OSMWays();
    $input_contents = file_get_contents($input_file) or die("Couldn't read file '{$osm_input}'");
    $input_osm_ways->deserialize_from_xml($input_contents);
    $code_map = array();
    $code_key = null;
    foreach ($input_osm_ways->ways as &$way) {
        $tags =& $way['tags'];
        if (empty($tags['name'])) {
            continue;
        }
        $names = explode('|', $tags['name_variants']);
        $main_name = $tags['name'];
        //iconv("utf-8", "us-ascii//TRANSLIT", $tags['name']);
        $main_name = clean_name($main_name);
        if (preg_match('@[^a-zA-Z.\\-\\/ \',()]@', $main_name)) {
            //            error_log("Foreign characters in main name: ".$main_name);
                continue;
            }
            $ref = $member['ref'];
            $way =& $osm_ways->ways[$ref];
            foreach ($relation['tags'] as $key => $value) {
                $way['tags'][$key] = $value;
            }
        }
    }
    if ($verbose) {
        error_log("Finished flatten_relationships()");
    }
}
ini_set('memory_limit', '-1');
$cliargs = array('inputfile' => array('short' => 'i', 'type' => 'optional', 'description' => 'The file to read the input OSM XML data from - if unset, will read from stdin', 'default' => 'php://stdout'), 'outputfile' => array('short' => 'o', 'type' => 'optional', 'description' => 'The file to write the output OSM XML data to - if unset, will write to stdout', 'default' => 'php://stdout'), 'verbose' => array('short' => 'v', 'type' => 'switch', 'description' => 'Whether to show extra debugging information about the processing as it happens'));
$options = cliargs_get_options($cliargs);
$input_file = $options['inputfile'];
$output_file = $options['outputfile'];
$verbose = $options['verbose'];
if ($verbose) {
    error_log("Starting load of '{$input_file}'");
}
$input_osm_ways = new OSMWays();
$input_contents = file_get_contents($input_file) or die("Couldn't read file '{$input_file}'");
$input_osm_ways->deserialize_from_xml($input_contents);
flatten_relations($input_osm_ways, $verbose);
if ($verbose) {
    error_log("Starting save of '{$output_file}'");
}
$output_contents = $input_osm_ways->serialize_to_xml();
file_put_contents($output_file, $output_contents) or die("Couldn't write file '{$output_file}'");
function extract_ways_matching_keys(&$input_osm_ways, $match_expression, $verbose)
{
    if ($verbose) {
        error_log("Starting way filtering");
    }
    $input_nodes = $input_osm_ways->nodes;
    $input_ways = $input_osm_ways->ways;
    $result = new OSMWays();
    $count = 0;
    foreach ($input_ways as $input_way) {
        $tags = $input_way['tags'];
        if (evaluate_match_expression($tags, $match_expression)) {
            $result->copy_way($input_way, $input_osm_ways);
        }
        $count += 1;
        if ($verbose && $count % 1000 === 0) {
            error_log("Processed {$count}/" . count($input_ways));
        }
    }
    if ($verbose) {
        error_log("Finished way filtering");
    }
    return $result;
}
$convert_from_uk = $options['convertfromuk'];
$projection = $options['projection'];
if (!empty($projection)) {
    if ($projection == 'mex') {
        $projection_parameters = array('r_major' => 6378137.0, 'r_minor' => 6356752.314, 'false_easting' => 2500000.0, 'false_northing' => 0.0, 'center_lon' => -102.0, 'lat1' => 17.5, 'lat2' => 29.5, 'center_lat' => 12.0);
    } else {
        if ($projection == 'can') {
            $projection_parameters = array('r_major' => 6378137.0, 'r_minor' => 6356752.314, 'false_easting' => 6200000.0, 'false_northing' => 3000000.0, 'center_lon' => -91.87, 'lat1' => 49.0, 'lat2' => 77.0, 'center_lat' => 63.39);
        } else {
            error_log("Unknown projection '{$projection}'");
            cliargs_print_usage_and_exit();
        }
    }
    $proj_convert = new ProjConvert($projection_parameters);
}
$osm_ways = new OSMWays();
$geometry_string = file_get_contents($input_geometry) or die("Couldn't open {$input_geometry} for reading");
$geometry_string = utf8_encode($geometry_string);
$geometry_string = str_replace("\t", '\\t', $geometry_string);
$geometry = json_decode($geometry_string, true);
$attributes_string = file_get_contents($input_attributes) or die("Couldn't open {$input_attributes} for reading");
$attributes_string = utf8_encode($attributes_string);
$attributes_string = str_replace("\t", '\\t', $attributes_string);
$attributes = json_decode($attributes_string, true) or die("Couldn't decode {$attributes_string}");
$shapes = $geometry['shapes'];
foreach ($shapes as $shape) {
    $index = $shape['index'];
    $parts = $shape['parts'];
    $current_attributes = $attributes[$index]['attributes'];
    error_log("Processing {$index}");
    foreach ($parts as $part) {
Exemple #16
0
function crop_ways_to_box(&$input_osm_ways, $min_lat, $min_lon, $max_lat, $max_lon, $verbose)
{
    if ($verbose) {
        error_log("Starting way cropping");
    }
    $clipping_planes = get_clipping_planes_for_box($min_lat, $min_lon, $max_lat, $max_lon);
    $input_nodes = $input_osm_ways->nodes;
    $input_ways = $input_osm_ways->ways;
    $result = new OSMWays();
    $count = 0;
    foreach ($input_ways as $input_way) {
        $count += 1;
        if ($verbose && $count % 1000 === 0) {
            error_log("Processing {$count}/" . count($input_ways));
        }
        $nds = $input_way['nds'];
        if (empty($nds)) {
            continue;
        }
        $output_vertices = array();
        foreach ($nds as $nd_ref) {
            $current_vertex = $input_nodes[$nd_ref];
            $output_vertices[] = $current_vertex;
        }
        foreach ($clipping_planes as $plane) {
            $output_vertices = clip_vertices_against_plane($output_vertices, $plane, $verbose);
        }
        if (empty($output_vertices)) {
            continue;
        }
        $result->begin_way();
        foreach ($input_way['tags'] as $key => $value) {
            $result->add_tag($key, $value);
        }
        foreach ($output_vertices as $vertex) {
            $result->add_vertex($vertex['lat'], $vertex['lon']);
        }
        if ($input_way['is_closed']) {
            $result->force_closed();
        }
        $result->end_way();
    }
    if ($verbose) {
        error_log("Finished way cropping");
    }
    return $result;
}
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
require_once 'cliargs.php';
require_once 'osmways.php';
ini_set('memory_limit', '-1');
$cliargs = array('outputfile' => array('short' => 'o', 'type' => 'optional', 'description' => 'The file to write the output OSM XML data to - if unset, will write to stdout', 'default' => 'php://stdout'));
$options = cliargs_get_options($cliargs);
$input_files = $options['unnamed'];
$output_file = $options['outputfile'];
if (empty($input_files)) {
    print "You need to supply at least one file to merge\n";
    cliargs_print_usage_and_exit($cliargs);
}
$output_osm_ways = null;
foreach ($input_files as $input_file) {
    error_log("Looking at {$input_file}");
    $input_osm_ways = new OSMWays();
    $input_contents = file_get_contents($input_file) or die("Couldn't read file '{$input_file}'");
    $input_osm_ways->deserialize_from_xml($input_contents);
    if (!isset($output_osm_ways)) {
        $output_osm_ways = new OSMWays();
    }
    $output_osm_ways->copy_all_ways($input_osm_ways);
}
$output_contents = $output_osm_ways->serialize_to_xml();
file_put_contents($output_file, $output_contents) or die("Couldn't write file '{$output_file}'");