Reformat whole codebase using Android Studio

This commit is contained in:
Manuel Fuhr 2022-07-11 06:30:17 +02:00
parent d5322667d5
commit c15913c1ab
161 changed files with 15124 additions and 18537 deletions

View file

@ -11,8 +11,7 @@ import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class ConvertLidarTile
{
public class ConvertLidarTile {
public static int NROWS;
public static int NCOLS;
@ -21,78 +20,63 @@ public class ConvertLidarTile
static short[] imagePixels;
private static void readHgtZip( String filename, int rowOffset, int colOffset ) throws Exception
{
ZipInputStream zis = new ZipInputStream( new BufferedInputStream( new FileInputStream( filename ) ) );
try
{
for ( ;; )
{
private static void readHgtZip(String filename, int rowOffset, int colOffset) throws Exception {
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(filename)));
try {
for (; ; ) {
ZipEntry ze = zis.getNextEntry();
if ( ze.getName().endsWith( ".hgt" ) )
{
readHgtFromStream( zis, rowOffset, colOffset );
if (ze.getName().endsWith(".hgt")) {
readHgtFromStream(zis, rowOffset, colOffset);
return;
}
}
}
finally
{
} finally {
zis.close();
}
}
private static void readHgtFromStream( InputStream is, int rowOffset, int colOffset )
throws Exception
{
DataInputStream dis = new DataInputStream( new BufferedInputStream( is ) );
for ( int ir = 0; ir < 1201; ir++ )
{
private static void readHgtFromStream(InputStream is, int rowOffset, int colOffset)
throws Exception {
DataInputStream dis = new DataInputStream(new BufferedInputStream(is));
for (int ir = 0; ir < 1201; ir++) {
int row = rowOffset + ir;
for ( int ic = 0; ic < 1201; ic++ )
{
for (int ic = 0; ic < 1201; ic++) {
int col = colOffset + ic;
int i1 = dis.read(); // msb first!
int i0 = dis.read();
if ( i0 == -1 || i1 == -1 )
throw new RuntimeException( "unexcepted end of file reading hgt entry!" );
if (i0 == -1 || i1 == -1)
throw new RuntimeException("unexcepted end of file reading hgt entry!");
short val = (short) ( ( i1 << 8 ) | i0 );
short val = (short) ((i1 << 8) | i0);
if ( val == NODATA2 )
{
if (val == NODATA2) {
val = NODATA;
}
setPixel( row, col, val );
setPixel(row, col, val);
}
}
}
private static void setPixel( int row, int col, short val )
{
if ( row >= 0 && row < NROWS && col >= 0 && col < NCOLS )
{
private static void setPixel(int row, int col, short val) {
if (row >= 0 && row < NROWS && col >= 0 && col < NCOLS) {
imagePixels[row * NCOLS + col] = val;
}
}
private static short getPixel( int row, int col )
{
if ( row >= 0 && row < NROWS && col >= 0 && col < NCOLS )
{
private static short getPixel(int row, int col) {
if (row >= 0 && row < NROWS && col >= 0 && col < NCOLS) {
return imagePixels[row * NCOLS + col];
}
return NODATA;
}
public static void doConvert( String inputDir, int lonDegreeStart, int latDegreeStart, String outputFile ) throws Exception
{
public static void doConvert(String inputDir, int lonDegreeStart, int latDegreeStart, String outputFile) throws Exception {
int extraBorder = 0;
NROWS = 5 * 1200 + 1 + 2 * extraBorder;
@ -101,34 +85,27 @@ public class ConvertLidarTile
imagePixels = new short[NROWS * NCOLS]; // 650 MB !
// prefill as NODATA
for ( int row = 0; row < NROWS; row++ )
{
for ( int col = 0; col < NCOLS; col++ )
{
for (int row = 0; row < NROWS; row++) {
for (int col = 0; col < NCOLS; col++) {
imagePixels[row * NCOLS + col] = NODATA;
}
}
for ( int latIdx = -1; latIdx <= 5; latIdx++ )
{
for (int latIdx = -1; latIdx <= 5; latIdx++) {
int latDegree = latDegreeStart + latIdx;
int rowOffset = extraBorder + ( 4 - latIdx ) * 1200;
int rowOffset = extraBorder + (4 - latIdx) * 1200;
for ( int lonIdx = -1; lonIdx <= 5; lonIdx++ )
{
for (int lonIdx = -1; lonIdx <= 5; lonIdx++) {
int lonDegree = lonDegreeStart + lonIdx;
int colOffset = extraBorder + lonIdx * 1200;
String filename = inputDir + "/" + formatLat( latDegree ) + formatLon( lonDegree ) + ".zip";
File f = new File( filename );
if ( f.exists() && f.length() > 0 )
{
System.out.println( "exist: " + filename );
readHgtZip( filename, rowOffset, colOffset );
}
else
{
System.out.println( "none : " + filename );
String filename = inputDir + "/" + formatLat(latDegree) + formatLon(lonDegree) + ".zip";
File f = new File(filename);
if (f.exists() && f.length() > 0) {
System.out.println("exist: " + filename);
readHgtZip(filename, rowOffset, colOffset);
} else {
System.out.println("none : " + filename);
}
}
}
@ -142,79 +119,71 @@ public class ConvertLidarTile
raster.halfcol = halfCol5;
raster.noDataValue = NODATA;
raster.cellsize = 1 / 1200.;
raster.xllcorner = lonDegreeStart - ( 0.5 + extraBorder ) * raster.cellsize;
raster.yllcorner = latDegreeStart - ( 0.5 + extraBorder ) * raster.cellsize;
raster.xllcorner = lonDegreeStart - (0.5 + extraBorder) * raster.cellsize;
raster.yllcorner = latDegreeStart - (0.5 + extraBorder) * raster.cellsize;
raster.eval_array = imagePixels;
// encode the raster
OutputStream os = new BufferedOutputStream( new FileOutputStream( outputFile ) );
new RasterCoder().encodeRaster( raster, os );
OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile));
new RasterCoder().encodeRaster(raster, os);
os.close();
// decode the raster
InputStream is = new BufferedInputStream( new FileInputStream( outputFile ) );
SrtmRaster raster2 = new RasterCoder().decodeRaster( is );
InputStream is = new BufferedInputStream(new FileInputStream(outputFile));
SrtmRaster raster2 = new RasterCoder().decodeRaster(is);
is.close();
short[] pix2 = raster2.eval_array;
if ( pix2.length != imagePixels.length )
throw new RuntimeException( "length mismatch!" );
if (pix2.length != imagePixels.length)
throw new RuntimeException("length mismatch!");
// compare decoding result
for ( int row = 0; row < NROWS; row++ )
{
for (int row = 0; row < NROWS; row++) {
int colstep = halfCol5 ? 2 : 1;
for ( int col = 0; col < NCOLS; col += colstep )
{
for (int col = 0; col < NCOLS; col += colstep) {
int idx = row * NCOLS + col;
short p2 = pix2[idx];
if ( p2 != imagePixels[idx] )
{
throw new RuntimeException( "content mismatch: p2=" + p2 + " p1=" + imagePixels[idx] );
if (p2 != imagePixels[idx]) {
throw new RuntimeException("content mismatch: p2=" + p2 + " p1=" + imagePixels[idx]);
}
}
}
}
private static String formatLon( int lon )
{
if ( lon >= 180 )
private static String formatLon(int lon) {
if (lon >= 180)
lon -= 180; // TODO: w180 oder E180 ?
String s = "E";
if ( lon < 0 )
{
if (lon < 0) {
lon = -lon;
s = "E";
}
String n = "000" + lon;
return s + n.substring( n.length() - 3 );
return s + n.substring(n.length() - 3);
}
private static String formatLat( int lat )
{
private static String formatLat(int lat) {
String s = "N";
if ( lat < 0 )
{
if (lat < 0) {
lat = -lat;
s = "S";
}
String n = "00" + lat;
return s + n.substring( n.length() - 2 );
return s + n.substring(n.length() - 2);
}
public static void main( String[] args ) throws Exception
{
String filename90 = args[0];
String filename30 = filename90.substring( 0, filename90.length() - 3 ) + "bef";
public static void main(String[] args) throws Exception {
String filename90 = args[0];
String filename30 = filename90.substring(0, filename90.length() - 3) + "bef";
int srtmLonIdx = Integer.parseInt( filename90.substring( 5, 7 ).toLowerCase() );
int srtmLatIdx = Integer.parseInt( filename90.substring( 8, 10 ).toLowerCase() );
int srtmLonIdx = Integer.parseInt(filename90.substring(5, 7).toLowerCase());
int srtmLatIdx = Integer.parseInt(filename90.substring(8, 10).toLowerCase());
int ilon_base = ( srtmLonIdx - 1 ) * 5 - 180;
int ilat_base = 150 - srtmLatIdx * 5 - 90;
doConvert( args[1], ilon_base, ilat_base, filename30 );
int ilon_base = (srtmLonIdx - 1) * 5 - 180;
int ilat_base = 150 - srtmLatIdx * 5 - 90;
doConvert(args[1], ilon_base, ilat_base, filename30);
}
}

View file

@ -3,8 +3,7 @@ package btools.mapcreator;
import java.io.*;
import java.util.zip.*;
public class ConvertSrtmTile
{
public class ConvertSrtmTile {
public static int NROWS;
public static int NCOLS;
@ -16,44 +15,33 @@ public class ConvertSrtmTile
public static int[] diffs = new int[100];
private static void readBilZip( String filename, int rowOffset, int colOffset, boolean halfCols ) throws Exception
{
ZipInputStream zis = new ZipInputStream( new BufferedInputStream( new FileInputStream( filename ) ) );
try
{
for ( ;; )
{
private static void readBilZip(String filename, int rowOffset, int colOffset, boolean halfCols) throws Exception {
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(filename)));
try {
for (; ; ) {
ZipEntry ze = zis.getNextEntry();
if ( ze.getName().endsWith( ".bil" ) )
{
readBilFromStream( zis, rowOffset, colOffset, halfCols );
if (ze.getName().endsWith(".bil")) {
readBilFromStream(zis, rowOffset, colOffset, halfCols);
return;
}
}
}
finally
{
} finally {
zis.close();
}
}
private static void readBilFromStream( InputStream is, int rowOffset, int colOffset, boolean halfCols )
throws Exception
{
DataInputStream dis = new DataInputStream( new BufferedInputStream( is ) );
for ( int ir = 0; ir < 3601; ir++ )
{
private static void readBilFromStream(InputStream is, int rowOffset, int colOffset, boolean halfCols)
throws Exception {
DataInputStream dis = new DataInputStream(new BufferedInputStream(is));
for (int ir = 0; ir < 3601; ir++) {
int row = rowOffset + ir;
for ( int ic = 0; ic < 3601; ic++ )
{
for (int ic = 0; ic < 3601; ic++) {
int col = colOffset + ic;
if ( ( ic % 2 ) == 1 && halfCols )
{
if ( getPixel( row, col ) == NODATA )
{
setPixel( row, col, SKIPDATA );
if ((ic % 2) == 1 && halfCols) {
if (getPixel(row, col) == NODATA) {
setPixel(row, col, SKIPDATA);
}
continue;
}
@ -61,42 +49,36 @@ public class ConvertSrtmTile
int i0 = dis.read();
int i1 = dis.read();
if ( i0 == -1 || i1 == -1 )
throw new RuntimeException( "unexcepted end of file reading bil entry!" );
if (i0 == -1 || i1 == -1)
throw new RuntimeException("unexcepted end of file reading bil entry!");
short val = (short) ( ( i1 << 8 ) | i0 );
short val = (short) ((i1 << 8) | i0);
if ( val == NODATA2 )
{
if (val == NODATA2) {
val = NODATA;
}
setPixel( row, col, val );
setPixel(row, col, val);
}
}
}
private static void setPixel( int row, int col, short val )
{
if ( row >= 0 && row < NROWS && col >= 0 && col < NCOLS )
{
private static void setPixel(int row, int col, short val) {
if (row >= 0 && row < NROWS && col >= 0 && col < NCOLS) {
imagePixels[row * NCOLS + col] = val;
}
}
private static short getPixel( int row, int col )
{
if ( row >= 0 && row < NROWS && col >= 0 && col < NCOLS )
{
private static short getPixel(int row, int col) {
if (row >= 0 && row < NROWS && col >= 0 && col < NCOLS) {
return imagePixels[row * NCOLS + col];
}
return NODATA;
}
public static void doConvert( String inputDir, String v1Dir, int lonDegreeStart, int latDegreeStart, String outputFile, SrtmRaster raster90 ) throws Exception
{
public static void doConvert(String inputDir, String v1Dir, int lonDegreeStart, int latDegreeStart, String outputFile, SrtmRaster raster90) throws Exception {
int extraBorder = 10;
int datacells = 0;
int mismatches = 0;
@ -107,95 +89,74 @@ public class ConvertSrtmTile
imagePixels = new short[NROWS * NCOLS]; // 650 MB !
// prefill as NODATA
for ( int row = 0; row < NROWS; row++ )
{
for ( int col = 0; col < NCOLS; col++ )
{
for (int row = 0; row < NROWS; row++) {
for (int col = 0; col < NCOLS; col++) {
imagePixels[row * NCOLS + col] = NODATA;
}
}
for ( int latIdx = -1; latIdx <= 5; latIdx++ )
{
for (int latIdx = -1; latIdx <= 5; latIdx++) {
int latDegree = latDegreeStart + latIdx;
int rowOffset = extraBorder + ( 4 - latIdx ) * 3600;
int rowOffset = extraBorder + (4 - latIdx) * 3600;
for ( int lonIdx = -1; lonIdx <= 5; lonIdx++ )
{
for (int lonIdx = -1; lonIdx <= 5; lonIdx++) {
int lonDegree = lonDegreeStart + lonIdx;
int colOffset = extraBorder + lonIdx * 3600;
String filename = inputDir + "/" + formatLat( latDegree ) + "_" + formatLon( lonDegree ) + "_1arc_v3_bil.zip";
File f = new File( filename );
if ( f.exists() && f.length() > 0 )
{
System.out.println( "exist: " + filename );
String filename = inputDir + "/" + formatLat(latDegree) + "_" + formatLon(lonDegree) + "_1arc_v3_bil.zip";
File f = new File(filename);
if (f.exists() && f.length() > 0) {
System.out.println("exist: " + filename);
boolean halfCol = latDegree >= 50 || latDegree < -50;
readBilZip( filename, rowOffset, colOffset, halfCol );
}
else
{
System.out.println( "none : " + filename );
readBilZip(filename, rowOffset, colOffset, halfCol);
} else {
System.out.println("none : " + filename);
}
}
}
boolean halfCol5 = latDegreeStart >= 50 || latDegreeStart < -50;
for ( int row90 = 0; row90 < 6001; row90++ )
{
for (int row90 = 0; row90 < 6001; row90++) {
int crow = 3 * row90 + extraBorder; // center row of 3x3
for ( int col90 = 0; col90 < 6001; col90++ )
{
for (int col90 = 0; col90 < 6001; col90++) {
int ccol = 3 * col90 + extraBorder; // center col of 3x3
// evaluate 3x3 area
if ( raster90 != null && (!halfCol5 || (col90 % 2) == 0 ) )
{
if (raster90 != null && (!halfCol5 || (col90 % 2) == 0)) {
short v90 = raster90.eval_array[row90 * 6001 + col90];
int sum = 0;
int nodatas = 0;
int datas = 0;
int colstep = halfCol5 ? 2 : 1;
for ( int row = crow - 1; row <= crow + 1; row++ )
{
for ( int col = ccol - colstep; col <= ccol + colstep; col += colstep )
{
for (int row = crow - 1; row <= crow + 1; row++) {
for (int col = ccol - colstep; col <= ccol + colstep; col += colstep) {
short v30 = imagePixels[row * NCOLS + col];
if ( v30 == NODATA )
{
if (v30 == NODATA) {
nodatas++;
}
else if ( v30 != SKIPDATA )
{
} else if (v30 != SKIPDATA) {
sum += v30;
datas++;
}
}
}
boolean doReplace = nodatas > 0 || v90 == NODATA || datas < 7;
if ( !doReplace )
{
if (!doReplace) {
datacells++;
int diff = sum - datas * v90;
if ( diff < -4 || diff > 4 )
{
if (diff < -4 || diff > 4) {
doReplace = true;
mismatches++;
}
if ( diff > -50 && diff < 50 && ( row90 % 1200 ) != 0 && ( col90 % 1200 ) != 0 )
{
if (diff > -50 && diff < 50 && (row90 % 1200) != 0 && (col90 % 1200) != 0) {
diffs[diff + 50]++;
}
}
if ( doReplace )
{
for ( int row = crow - 1; row <= crow + 1; row++ )
{
for ( int col = ccol - colstep; col <= ccol + colstep; col += colstep )
{
if (doReplace) {
for (int row = crow - 1; row <= crow + 1; row++) {
for (int col = ccol - colstep; col <= ccol + colstep; col += colstep) {
imagePixels[row * NCOLS + col] = v90;
}
}
@ -210,102 +171,90 @@ public class ConvertSrtmTile
raster.halfcol = halfCol5;
raster.noDataValue = NODATA;
raster.cellsize = 1 / 3600.;
raster.xllcorner = lonDegreeStart - ( 0.5 + extraBorder ) * raster.cellsize;
raster.yllcorner = latDegreeStart - ( 0.5 + extraBorder ) * raster.cellsize;
raster.xllcorner = lonDegreeStart - (0.5 + extraBorder) * raster.cellsize;
raster.yllcorner = latDegreeStart - (0.5 + extraBorder) * raster.cellsize;
raster.eval_array = imagePixels;
// encode the raster
OutputStream os = new BufferedOutputStream( new FileOutputStream( outputFile ) );
new RasterCoder().encodeRaster( raster, os );
OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile));
new RasterCoder().encodeRaster(raster, os);
os.close();
// decode the raster
InputStream is = new BufferedInputStream( new FileInputStream( outputFile ) );
SrtmRaster raster2 = new RasterCoder().decodeRaster( is );
InputStream is = new BufferedInputStream(new FileInputStream(outputFile));
SrtmRaster raster2 = new RasterCoder().decodeRaster(is);
is.close();
short[] pix2 = raster2.eval_array;
if ( pix2.length != imagePixels.length )
throw new RuntimeException( "length mismatch!" );
if (pix2.length != imagePixels.length)
throw new RuntimeException("length mismatch!");
// compare decoding result
for ( int row = 0; row < NROWS; row++ )
{
for (int row = 0; row < NROWS; row++) {
int colstep = halfCol5 ? 2 : 1;
for ( int col = 0; col < NCOLS; col += colstep )
{
for (int col = 0; col < NCOLS; col += colstep) {
int idx = row * NCOLS + col;
if ( imagePixels[idx] == SKIPDATA )
{
if (imagePixels[idx] == SKIPDATA) {
continue;
}
short p2 = pix2[idx];
if ( p2 > SKIPDATA )
{
if (p2 > SKIPDATA) {
p2 /= 2;
}
if ( p2 != imagePixels[idx] )
{
throw new RuntimeException( "content mismatch!" );
if (p2 != imagePixels[idx]) {
throw new RuntimeException("content mismatch!");
}
}
}
for(int i=1; i<100;i++) System.out.println( "diff[" + (i-50) + "] = " + diffs[i] );
System.out.println( "datacells=" + datacells + " mismatch%=" + (100.*mismatches)/datacells );
btools.util.MixCoderDataOutputStream.stats();
for (int i = 1; i < 100; i++) System.out.println("diff[" + (i - 50) + "] = " + diffs[i]);
System.out.println("datacells=" + datacells + " mismatch%=" + (100. * mismatches) / datacells);
btools.util.MixCoderDataOutputStream.stats();
// test( raster );
// raster.calcWeights( 50. );
// test( raster );
// 39828330 &lon=3115280&layer=OpenStreetMap
}
private static void test( SrtmRaster raster )
{
private static void test(SrtmRaster raster) {
int lat0 = 39828330;
int lon0 = 3115280;
for ( int iy = -9; iy <= 9; iy++ )
{
for (int iy = -9; iy <= 9; iy++) {
StringBuilder sb = new StringBuilder();
for ( int ix = -9; ix <= 9; ix++ )
{
for (int ix = -9; ix <= 9; ix++) {
int lat = lat0 + 90000000 - 100 * iy;
int lon = lon0 + 180000000 + 100 * ix;
int ival = (int) ( raster.getElevation( lon, lat ) / 4. );
int ival = (int) (raster.getElevation(lon, lat) / 4.);
String sval = " " + ival;
sb.append( sval.substring( sval.length() - 4 ) );
sb.append(sval.substring(sval.length() - 4));
}
System.out.println( sb );
System.out.println(sb);
System.out.println();
}
}
private static String formatLon( int lon )
{
if ( lon >= 180 )
private static String formatLon(int lon) {
if (lon >= 180)
lon -= 180; // TODO: w180 oder E180 ?
String s = "e";
if ( lon < 0 )
{
if (lon < 0) {
lon = -lon;
s = "w";
}
String n = "000" + lon;
return s + n.substring( n.length() - 3 );
return s + n.substring(n.length() - 3);
}
private static String formatLat( int lat )
{
private static String formatLat(int lat) {
String s = "n";
if ( lat < 0 )
{
if (lat < 0) {
lat = -lat;
s = "s";
}
String n = "00" + lat;
return s + n.substring( n.length() - 2 );
return s + n.substring(n.length() - 2);
}
}

View file

@ -4,57 +4,50 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class ConvertUrlList
{
public class ConvertUrlList {
public static final short NODATA = -32767;
public static void main( String[] args ) throws Exception
{
BufferedReader br = new BufferedReader( new FileReader( args[0] ) );
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(args[0]));
for ( ;; )
{
for (; ; ) {
String line = br.readLine();
if ( line == null )
{
if (line == null) {
break;
}
int idx1 = line.indexOf( "srtm_" );
if ( idx1 < 0 )
{
int idx1 = line.indexOf("srtm_");
if (idx1 < 0) {
continue;
}
String filename90 = line.substring( idx1 );
String filename30 = filename90.substring( 0, filename90.length() - 3 ) + "bef";
String filename90 = line.substring(idx1);
String filename30 = filename90.substring(0, filename90.length() - 3) + "bef";
if ( new File( filename30 ).exists() )
{
if (new File(filename30).exists()) {
continue;
}
// int srtmLonIdx = (ilon+5000000)/5000000; -> ilon = (srtmLonIdx-1)*5
// int srtmLatIdx = (154999999-ilat)/5000000; -> ilat = 155 - srtmLatIdx*5
int srtmLonIdx = Integer.parseInt( filename90.substring( 5, 7 ).toLowerCase() );
int srtmLatIdx = Integer.parseInt( filename90.substring( 8, 10 ).toLowerCase() );
int srtmLonIdx = Integer.parseInt(filename90.substring(5, 7).toLowerCase());
int srtmLatIdx = Integer.parseInt(filename90.substring(8, 10).toLowerCase());
int ilon_base = ( srtmLonIdx - 1 ) * 5 - 180;
int ilon_base = (srtmLonIdx - 1) * 5 - 180;
int ilat_base = 150 - srtmLatIdx * 5 - 90;
SrtmRaster raster90 = null;
File file90 = new File( new File( args[1] ), filename90 );
if ( file90.exists() )
{
System.out.println( "reading " + file90 );
raster90 = new SrtmData( file90 ).getRaster();
File file90 = new File(new File(args[1]), filename90);
if (file90.exists()) {
System.out.println("reading " + file90);
raster90 = new SrtmData(file90).getRaster();
}
ConvertSrtmTile.doConvert( args[2], args[3], ilon_base, ilat_base, filename30, raster90 );
ConvertSrtmTile.doConvert(args[2], args[3], ilon_base, ilat_base, filename30, raster90);
}
br.close();
}
}

View file

@ -9,76 +9,63 @@ import java.util.ArrayList;
import btools.util.CheapRuler;
public class DPFilter
{
public class DPFilter {
private static double dp_sql_threshold = 0.4 * 0.4;
/*
* for each node (except first+last), eventually set the DP_SURVIVOR_BIT
*/
public static void doDPFilter( ArrayList<OsmNodeP> nodes )
{
public static void doDPFilter(ArrayList<OsmNodeP> nodes) {
int first = 0;
int last = nodes.size()-1;
while( first < last && (nodes.get(first+1).bits & OsmNodeP.DP_SURVIVOR_BIT) != 0 )
{
int last = nodes.size() - 1;
while (first < last && (nodes.get(first + 1).bits & OsmNodeP.DP_SURVIVOR_BIT) != 0) {
first++;
}
while( first < last && (nodes.get(last-1).bits & OsmNodeP.DP_SURVIVOR_BIT) != 0 )
{
while (first < last && (nodes.get(last - 1).bits & OsmNodeP.DP_SURVIVOR_BIT) != 0) {
last--;
}
if ( last - first > 1 )
{
doDPFilter( nodes, first, last );
if (last - first > 1) {
doDPFilter(nodes, first, last);
}
}
public static void doDPFilter( ArrayList<OsmNodeP> nodes, int first, int last )
{
public static void doDPFilter(ArrayList<OsmNodeP> nodes, int first, int last) {
double maxSqDist = -1.;
int index = -1;
OsmNodeP p1 = nodes.get( first );
OsmNodeP p2 = nodes.get( last );
OsmNodeP p1 = nodes.get(first);
OsmNodeP p2 = nodes.get(last);
double[] lonlat2m = CheapRuler.getLonLatToMeterScales( (p1.ilat+p2.ilat) >> 1 );
double[] lonlat2m = CheapRuler.getLonLatToMeterScales((p1.ilat + p2.ilat) >> 1);
double dlon2m = lonlat2m[0];
double dlat2m = lonlat2m[1];
double dx = (p2.ilon - p1.ilon) * dlon2m;
double dy = (p2.ilat - p1.ilat) * dlat2m;
double d2 = dx * dx + dy * dy;
for ( int i = first + 1; i < last; i++ )
{
OsmNodeP p = nodes.get( i );
for (int i = first + 1; i < last; i++) {
OsmNodeP p = nodes.get(i);
double t = 0.;
if ( d2 != 0f )
{
t = ( ( p.ilon - p1.ilon ) * dlon2m * dx + ( p.ilat - p1.ilat ) * dlat2m * dy ) / d2;
t = t > 1. ? 1. : ( t < 0. ? 0. : t );
if (d2 != 0f) {
t = ((p.ilon - p1.ilon) * dlon2m * dx + (p.ilat - p1.ilat) * dlat2m * dy) / d2;
t = t > 1. ? 1. : (t < 0. ? 0. : t);
}
double dx2 = (p.ilon - ( p1.ilon + t*( p2.ilon - p1.ilon ) ) ) * dlon2m;
double dy2 = (p.ilat - ( p1.ilat + t*( p2.ilat - p1.ilat ) ) ) * dlat2m;
double dx2 = (p.ilon - (p1.ilon + t * (p2.ilon - p1.ilon))) * dlon2m;
double dy2 = (p.ilat - (p1.ilat + t * (p2.ilat - p1.ilat))) * dlat2m;
double sqDist = dx2 * dx2 + dy2 * dy2;
if ( sqDist > maxSqDist )
{
if (sqDist > maxSqDist) {
index = i;
maxSqDist = sqDist;
}
}
if ( index >= 0 )
{
if ( index - first > 1 )
{
doDPFilter( nodes, first, index );
if (index >= 0) {
if (index - first > 1) {
doDPFilter(nodes, first, index);
}
if ( maxSqDist >= dp_sql_threshold )
{
nodes.get( index ).bits |= OsmNodeP.DP_SURVIVOR_BIT;
if (maxSqDist >= dp_sql_threshold) {
nodes.get(index).bits |= OsmNodeP.DP_SURVIVOR_BIT;
}
if ( last - index > 1 )
{
doDPFilter( nodes, index, last );
if (last - index > 1) {
doDPFilter(nodes, index, last);
}
}
}

View file

@ -1,171 +1,158 @@
/**
* common base class for the map-filters
*
* @author ab
*/
package btools.mapcreator;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import btools.util.DiffCoderDataOutputStream;
public abstract class MapCreatorBase implements WayListener, NodeListener, RelationListener
{
private DiffCoderDataOutputStream[] tileOutStreams;
protected File outTileDir;
protected HashMap<String,String> tags;
public void putTag( String key, String value )
{
if ( tags == null ) tags = new HashMap<String,String>();
tags.put( key, value );
}
public String getTag( String key )
{
return tags == null ? null : tags.get( key );
}
public HashMap<String,String> getTagsOrNull()
{
return tags;
}
public void setTags( HashMap<String,String> tags )
{
this.tags = tags;
}
protected static long readId( DataInputStream is) throws IOException
{
int offset = is.readByte();
if ( offset == 32 ) return -1;
long i = is.readInt();
i = i << 5;
return i | offset;
}
protected static void writeId( DataOutputStream o, long id ) throws IOException
{
if ( id == -1 )
{
o.writeByte( 32 );
return;
}
int offset = (int)( id & 0x1f );
int i = (int)( id >> 5 );
o.writeByte( offset );
o.writeInt( i );
}
protected static File[] sortBySizeAsc( File[] files )
{
int n = files.length;
long[] sizes = new long[n];
File[] sorted = new File[n];
for( int i=0; i<n; i++ ) sizes[i] = files[i].length();
for(int nf=0; nf<n; nf++)
{
int idx = -1;
long min = -1;
for( int i=0; i<n; i++ )
{
if ( sizes[i] != -1 && ( idx == -1 || sizes[i] < min ) )
{
min = sizes[i];
idx = i;
}
}
sizes[idx] = -1;
sorted[nf] = files[idx];
}
return sorted;
}
protected File fileFromTemplate( File template, File dir, String suffix )
{
String filename = template.getName();
filename = filename.substring( 0, filename.length() - 3 ) + suffix;
return new File( dir, filename );
}
protected DataInputStream createInStream( File inFile ) throws IOException
{
return new DataInputStream( new BufferedInputStream ( new FileInputStream( inFile ) ) );
}
protected DiffCoderDataOutputStream createOutStream( File outFile ) throws IOException
{
return new DiffCoderDataOutputStream( new BufferedOutputStream( new FileOutputStream( outFile ) ) );
}
protected DiffCoderDataOutputStream getOutStreamForTile( int tileIndex ) throws Exception
{
if ( tileOutStreams == null )
{
tileOutStreams = new DiffCoderDataOutputStream[64];
}
if ( tileOutStreams[tileIndex] == null )
{
tileOutStreams[tileIndex] = createOutStream( new File( outTileDir, getNameForTile( tileIndex ) ) );
}
return tileOutStreams[tileIndex];
}
protected String getNameForTile( int tileIndex )
{
throw new IllegalArgumentException( "getNameForTile not implemented" );
}
protected void closeTileOutStreams() throws Exception
{
if ( tileOutStreams == null )
{
return;
}
for( int tileIndex=0; tileIndex<tileOutStreams.length; tileIndex++ )
{
if ( tileOutStreams[tileIndex] != null ) tileOutStreams[tileIndex].close();
tileOutStreams[tileIndex] = null;
}
}
// interface dummys
@Override
public void nodeFileStart( File nodefile ) throws Exception {}
@Override
public void nextNode( NodeData n ) throws Exception {}
@Override
public void nodeFileEnd( File nodefile ) throws Exception {}
@Override
public boolean wayFileStart( File wayfile ) throws Exception { return true; }
@Override
public void nextWay( WayData data ) throws Exception {}
@Override
public void wayFileEnd( File wayfile ) throws Exception {}
@Override
public void nextRelation( RelationData data ) throws Exception {}
@Override
public void nextRestriction( RelationData data, long fromWid, long toWid, long viaNid ) throws Exception {}
}
/**
* common base class for the map-filters
*
* @author ab
*/
package btools.mapcreator;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import btools.util.DiffCoderDataOutputStream;
public abstract class MapCreatorBase implements WayListener, NodeListener, RelationListener {
private DiffCoderDataOutputStream[] tileOutStreams;
protected File outTileDir;
protected HashMap<String, String> tags;
public void putTag(String key, String value) {
if (tags == null) tags = new HashMap<String, String>();
tags.put(key, value);
}
public String getTag(String key) {
return tags == null ? null : tags.get(key);
}
public HashMap<String, String> getTagsOrNull() {
return tags;
}
public void setTags(HashMap<String, String> tags) {
this.tags = tags;
}
protected static long readId(DataInputStream is) throws IOException {
int offset = is.readByte();
if (offset == 32) return -1;
long i = is.readInt();
i = i << 5;
return i | offset;
}
protected static void writeId(DataOutputStream o, long id) throws IOException {
if (id == -1) {
o.writeByte(32);
return;
}
int offset = (int) (id & 0x1f);
int i = (int) (id >> 5);
o.writeByte(offset);
o.writeInt(i);
}
protected static File[] sortBySizeAsc(File[] files) {
int n = files.length;
long[] sizes = new long[n];
File[] sorted = new File[n];
for (int i = 0; i < n; i++) sizes[i] = files[i].length();
for (int nf = 0; nf < n; nf++) {
int idx = -1;
long min = -1;
for (int i = 0; i < n; i++) {
if (sizes[i] != -1 && (idx == -1 || sizes[i] < min)) {
min = sizes[i];
idx = i;
}
}
sizes[idx] = -1;
sorted[nf] = files[idx];
}
return sorted;
}
protected File fileFromTemplate(File template, File dir, String suffix) {
String filename = template.getName();
filename = filename.substring(0, filename.length() - 3) + suffix;
return new File(dir, filename);
}
protected DataInputStream createInStream(File inFile) throws IOException {
return new DataInputStream(new BufferedInputStream(new FileInputStream(inFile)));
}
protected DiffCoderDataOutputStream createOutStream(File outFile) throws IOException {
return new DiffCoderDataOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));
}
protected DiffCoderDataOutputStream getOutStreamForTile(int tileIndex) throws Exception {
if (tileOutStreams == null) {
tileOutStreams = new DiffCoderDataOutputStream[64];
}
if (tileOutStreams[tileIndex] == null) {
tileOutStreams[tileIndex] = createOutStream(new File(outTileDir, getNameForTile(tileIndex)));
}
return tileOutStreams[tileIndex];
}
protected String getNameForTile(int tileIndex) {
throw new IllegalArgumentException("getNameForTile not implemented");
}
protected void closeTileOutStreams() throws Exception {
if (tileOutStreams == null) {
return;
}
for (int tileIndex = 0; tileIndex < tileOutStreams.length; tileIndex++) {
if (tileOutStreams[tileIndex] != null) tileOutStreams[tileIndex].close();
tileOutStreams[tileIndex] = null;
}
}
// interface dummys
@Override
public void nodeFileStart(File nodefile) throws Exception {
}
@Override
public void nextNode(NodeData n) throws Exception {
}
@Override
public void nodeFileEnd(File nodefile) throws Exception {
}
@Override
public boolean wayFileStart(File wayfile) throws Exception {
return true;
}
@Override
public void nextWay(WayData data) throws Exception {
}
@Override
public void wayFileEnd(File wayfile) throws Exception {
}
@Override
public void nextRelation(RelationData data) throws Exception {
}
@Override
public void nextRestriction(RelationData data, long fromWid, long toWid, long viaNid) throws Exception {
}
}

View file

@ -1,84 +1,75 @@
package btools.mapcreator;
import java.io.File;
/**
* NodeCutter does 1 step in map-processing:
*
* - cuts the 45*30 node tiles into 5*5 pieces
*
* @author ab
*/
public class NodeCutter extends MapCreatorBase
{
private int lonoffset;
private int latoffset;
public static void main(String[] args) throws Exception
{
System.out.println("*** NodeCutter: Cut big node-tiles into 5x5 tiles");
if (args.length != 2)
{
System.out.println("usage: java NodeCutter <node-tiles-in> <node-tiles-out>" );
return;
}
new NodeCutter().process( new File( args[0] ), new File( args[1] ) );
}
public void init( File nodeTilesOut )
{
this.outTileDir = nodeTilesOut;
}
public void process( File nodeTilesIn, File nodeTilesOut ) throws Exception
{
init( nodeTilesOut );
new NodeIterator( this, true ).processDir( nodeTilesIn, ".tlf" );
}
@Override
public void nodeFileStart( File nodefile ) throws Exception
{
lonoffset = -1;
latoffset = -1;
}
@Override
public void nextNode( NodeData n ) throws Exception
{
n.writeTo( getOutStreamForTile( getTileIndex( n.ilon, n.ilat ) ) );
}
@Override
public void nodeFileEnd( File nodeFile ) throws Exception
{
closeTileOutStreams();
}
private int getTileIndex( int ilon, int ilat )
{
int lonoff = (ilon / 45000000 ) * 45;
int latoff = (ilat / 30000000 ) * 30;
if ( lonoffset == -1 ) lonoffset = lonoff;
if ( latoffset == -1 ) latoffset = latoff;
if ( lonoff != lonoffset || latoff != latoffset )
throw new IllegalArgumentException( "inconsistent node: " + ilon + " " + ilat );
int lon = (ilon / 5000000) % 9;
int lat = (ilat / 5000000) % 6;
if ( lon < 0 || lon > 8 || lat < 0 || lat > 5 ) throw new IllegalArgumentException( "illegal pos: " + ilon + "," + ilat );
return lon*6 + lat;
}
protected String getNameForTile( int tileIndex )
{
int lon = (tileIndex / 6 ) * 5 + lonoffset - 180;
int lat = (tileIndex % 6 ) * 5 + latoffset - 90;
String slon = lon < 0 ? "W" + (-lon) : "E" + lon;
String slat = lat < 0 ? "S" + (-lat) : "N" + lat;
return slon + "_" + slat + ".n5d";
}
}
package btools.mapcreator;
import java.io.File;
/**
* NodeCutter does 1 step in map-processing:
* <p>
* - cuts the 45*30 node tiles into 5*5 pieces
*
* @author ab
*/
public class NodeCutter extends MapCreatorBase {
private int lonoffset;
private int latoffset;
public static void main(String[] args) throws Exception {
System.out.println("*** NodeCutter: Cut big node-tiles into 5x5 tiles");
if (args.length != 2) {
System.out.println("usage: java NodeCutter <node-tiles-in> <node-tiles-out>");
return;
}
new NodeCutter().process(new File(args[0]), new File(args[1]));
}
public void init(File nodeTilesOut) {
this.outTileDir = nodeTilesOut;
}
public void process(File nodeTilesIn, File nodeTilesOut) throws Exception {
init(nodeTilesOut);
new NodeIterator(this, true).processDir(nodeTilesIn, ".tlf");
}
@Override
public void nodeFileStart(File nodefile) throws Exception {
lonoffset = -1;
latoffset = -1;
}
@Override
public void nextNode(NodeData n) throws Exception {
n.writeTo(getOutStreamForTile(getTileIndex(n.ilon, n.ilat)));
}
@Override
public void nodeFileEnd(File nodeFile) throws Exception {
closeTileOutStreams();
}
private int getTileIndex(int ilon, int ilat) {
int lonoff = (ilon / 45000000) * 45;
int latoff = (ilat / 30000000) * 30;
if (lonoffset == -1) lonoffset = lonoff;
if (latoffset == -1) latoffset = latoff;
if (lonoff != lonoffset || latoff != latoffset)
throw new IllegalArgumentException("inconsistent node: " + ilon + " " + ilat);
int lon = (ilon / 5000000) % 9;
int lat = (ilat / 5000000) % 6;
if (lon < 0 || lon > 8 || lat < 0 || lat > 5)
throw new IllegalArgumentException("illegal pos: " + ilon + "," + ilat);
return lon * 6 + lat;
}
protected String getNameForTile(int tileIndex) {
int lon = (tileIndex / 6) * 5 + lonoffset - 180;
int lat = (tileIndex % 6) * 5 + latoffset - 90;
String slon = lon < 0 ? "W" + (-lon) : "E" + lon;
String slat = lat < 0 ? "S" + (-lat) : "N" + lat;
return slon + "_" + slat + ".n5d";
}
}

View file

@ -1,46 +1,49 @@
package btools.mapcreator;
import btools.util.DiffCoderDataInputStream;
import btools.util.DiffCoderDataOutputStream;
/**
* Container for node data on the preprocessor level
*
* @author ab
*/
public class NodeData extends MapCreatorBase
{
public long nid;
public int ilon;
public int ilat;
public byte[] description;
public short selev = Short.MIN_VALUE;
public NodeData( long id, double lon, double lat )
{
nid = id;
ilat = (int)( ( lat + 90. )*1000000. + 0.5);
ilon = (int)( ( lon + 180. )*1000000. + 0.5);
}
public NodeData( DiffCoderDataInputStream dis ) throws Exception
{
nid = dis.readDiffed( 0 );
ilon = (int)dis.readDiffed( 1 );
ilat = (int)dis.readDiffed( 2 );
int mode = dis.readByte();
if ( ( mode & 1 ) != 0 ) { int dlen = dis.readShort(); description = new byte[dlen]; dis.readFully( description ); }
if ( ( mode & 2 ) != 0 ) selev = dis.readShort();
}
public void writeTo( DiffCoderDataOutputStream dos ) throws Exception
{
dos.writeDiffed( nid, 0 );
dos.writeDiffed( ilon, 1 );
dos.writeDiffed( ilat, 2 );
int mode = (description == null ? 0 : 1 ) | ( selev == Short.MIN_VALUE ? 0 : 2 );
dos.writeByte( (byte) mode);
if ( ( mode & 1 ) != 0 ) { dos.writeShort( description.length ); dos.write( description ); }
if ( ( mode & 2 ) != 0 ) dos.writeShort( selev );
}
}
package btools.mapcreator;
import btools.util.DiffCoderDataInputStream;
import btools.util.DiffCoderDataOutputStream;
/**
* Container for node data on the preprocessor level
*
* @author ab
*/
public class NodeData extends MapCreatorBase {
public long nid;
public int ilon;
public int ilat;
public byte[] description;
public short selev = Short.MIN_VALUE;
public NodeData(long id, double lon, double lat) {
nid = id;
ilat = (int) ((lat + 90.) * 1000000. + 0.5);
ilon = (int) ((lon + 180.) * 1000000. + 0.5);
}
public NodeData(DiffCoderDataInputStream dis) throws Exception {
nid = dis.readDiffed(0);
ilon = (int) dis.readDiffed(1);
ilat = (int) dis.readDiffed(2);
int mode = dis.readByte();
if ((mode & 1) != 0) {
int dlen = dis.readShort();
description = new byte[dlen];
dis.readFully(description);
}
if ((mode & 2) != 0) selev = dis.readShort();
}
public void writeTo(DiffCoderDataOutputStream dos) throws Exception {
dos.writeDiffed(nid, 0);
dos.writeDiffed(ilon, 1);
dos.writeDiffed(ilat, 2);
int mode = (description == null ? 0 : 1) | (selev == Short.MIN_VALUE ? 0 : 2);
dos.writeByte((byte) mode);
if ((mode & 1) != 0) {
dos.writeShort(description.length);
dos.write(description);
}
if ((mode & 2) != 0) dos.writeShort(selev);
}
}

View file

@ -1,92 +1,80 @@
package btools.mapcreator;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import btools.util.DenseLongMap;
import btools.util.DiffCoderDataOutputStream;
import btools.util.TinyDenseLongMap;
/**
* NodeFilter does 1 step in map-processing:
*
* - filters out unused nodes according to the way file
*
* @author ab
*/
public class NodeFilter extends MapCreatorBase
{
private DiffCoderDataOutputStream nodesOutStream;
private File nodeTilesOut;
protected DenseLongMap nodebitmap;
public static void main(String[] args) throws Exception
{
System.out.println("*** NodeFilter: Filter way related nodes");
if (args.length != 3)
{
System.out.println("usage: java NodeFilter <node-tiles-in> <way-file-in> <node-tiles-out>" );
return;
}
new NodeFilter().process( new File( args[0] ), new File( args[1] ), new File( args[2] ) );
}
public void init() throws Exception
{
nodebitmap = Boolean.getBoolean( "useDenseMaps" ) ? new DenseLongMap( 512 ) : new TinyDenseLongMap();
}
public void process( File nodeTilesIn, File wayFileIn, File nodeTilesOut ) throws Exception
{
init();
this.nodeTilesOut = nodeTilesOut;
// read the wayfile into a bitmap of used nodes
new WayIterator( this, false ).processFile( wayFileIn );
// finally filter all node files
new NodeIterator( this, true ).processDir( nodeTilesIn, ".tls" );
}
@Override
public void nextWay( WayData data ) throws Exception
{
int nnodes = data.nodes.size();
for (int i=0; i<nnodes; i++ )
{
nodebitmap.put( data.nodes.get(i), 0 );
}
}
@Override
public void nodeFileStart( File nodefile ) throws Exception
{
String filename = nodefile.getName();
File outfile = new File( nodeTilesOut, filename );
nodesOutStream = new DiffCoderDataOutputStream( new BufferedOutputStream ( new FileOutputStream( outfile ) ) );
}
@Override
public void nextNode( NodeData n ) throws Exception
{
if ( isRelevant( n ) )
{
n.writeTo( nodesOutStream );
}
}
public boolean isRelevant( NodeData n )
{
// check if node passes bitmap
return nodebitmap.getInt( n.nid ) == 0; // 0 -> bit set, -1 -> unset
}
@Override
public void nodeFileEnd( File nodeFile ) throws Exception
{
nodesOutStream.close();
}
}
package btools.mapcreator;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import btools.util.DenseLongMap;
import btools.util.DiffCoderDataOutputStream;
import btools.util.TinyDenseLongMap;
/**
* NodeFilter does 1 step in map-processing:
* <p>
* - filters out unused nodes according to the way file
*
* @author ab
*/
public class NodeFilter extends MapCreatorBase {
private DiffCoderDataOutputStream nodesOutStream;
private File nodeTilesOut;
protected DenseLongMap nodebitmap;
public static void main(String[] args) throws Exception {
System.out.println("*** NodeFilter: Filter way related nodes");
if (args.length != 3) {
System.out.println("usage: java NodeFilter <node-tiles-in> <way-file-in> <node-tiles-out>");
return;
}
new NodeFilter().process(new File(args[0]), new File(args[1]), new File(args[2]));
}
public void init() throws Exception {
nodebitmap = Boolean.getBoolean("useDenseMaps") ? new DenseLongMap(512) : new TinyDenseLongMap();
}
public void process(File nodeTilesIn, File wayFileIn, File nodeTilesOut) throws Exception {
init();
this.nodeTilesOut = nodeTilesOut;
// read the wayfile into a bitmap of used nodes
new WayIterator(this, false).processFile(wayFileIn);
// finally filter all node files
new NodeIterator(this, true).processDir(nodeTilesIn, ".tls");
}
@Override
public void nextWay(WayData data) throws Exception {
int nnodes = data.nodes.size();
for (int i = 0; i < nnodes; i++) {
nodebitmap.put(data.nodes.get(i), 0);
}
}
@Override
public void nodeFileStart(File nodefile) throws Exception {
String filename = nodefile.getName();
File outfile = new File(nodeTilesOut, filename);
nodesOutStream = new DiffCoderDataOutputStream(new BufferedOutputStream(new FileOutputStream(outfile)));
}
@Override
public void nextNode(NodeData n) throws Exception {
if (isRelevant(n)) {
n.writeTo(nodesOutStream);
}
}
public boolean isRelevant(NodeData n) {
// check if node passes bitmap
return nodebitmap.getInt(n.nid) == 0; // 0 -> bit set, -1 -> unset
}
@Override
public void nodeFileEnd(File nodeFile) throws Exception {
nodesOutStream.close();
}
}

View file

@ -1,71 +1,59 @@
package btools.mapcreator;
import java.io.BufferedInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import btools.util.DiffCoderDataInputStream;
/**
* Iterate over a singe nodefile or a directory
* of nodetiles and feed the nodes to the callback listener
*
* @author ab
*/
public class NodeIterator extends MapCreatorBase
{
private NodeListener listener;
private boolean delete;
public NodeIterator( NodeListener nodeListener, boolean deleteAfterReading )
{
listener = nodeListener;
delete = deleteAfterReading;
}
public void processDir( File indir, String inSuffix ) throws Exception
{
if ( !indir.isDirectory() )
{
throw new IllegalArgumentException( "not a directory: " + indir );
}
File[] af = sortBySizeAsc( indir.listFiles() );
for( int i=0; i<af.length; i++ )
{
File nodefile = af[i];
if ( nodefile.getName().endsWith( inSuffix ) )
{
processFile( nodefile );
}
}
}
public void processFile(File nodefile) throws Exception
{
System.out.println( "*** NodeIterator reading: " + nodefile );
listener.nodeFileStart( nodefile );
DiffCoderDataInputStream di = new DiffCoderDataInputStream( new BufferedInputStream ( new FileInputStream( nodefile ) ) );
try
{
for(;;)
{
NodeData n = new NodeData( di );
listener.nextNode( n );
}
}
catch( EOFException eof )
{
di.close();
}
listener.nodeFileEnd( nodefile );
if ( delete && "true".equals( System.getProperty( "deletetmpfiles" ) ))
{
nodefile.delete();
}
}
}
package btools.mapcreator;
import java.io.BufferedInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import btools.util.DiffCoderDataInputStream;
/**
* Iterate over a singe nodefile or a directory
* of nodetiles and feed the nodes to the callback listener
*
* @author ab
*/
public class NodeIterator extends MapCreatorBase {
private NodeListener listener;
private boolean delete;
public NodeIterator(NodeListener nodeListener, boolean deleteAfterReading) {
listener = nodeListener;
delete = deleteAfterReading;
}
public void processDir(File indir, String inSuffix) throws Exception {
if (!indir.isDirectory()) {
throw new IllegalArgumentException("not a directory: " + indir);
}
File[] af = sortBySizeAsc(indir.listFiles());
for (int i = 0; i < af.length; i++) {
File nodefile = af[i];
if (nodefile.getName().endsWith(inSuffix)) {
processFile(nodefile);
}
}
}
public void processFile(File nodefile) throws Exception {
System.out.println("*** NodeIterator reading: " + nodefile);
listener.nodeFileStart(nodefile);
DiffCoderDataInputStream di = new DiffCoderDataInputStream(new BufferedInputStream(new FileInputStream(nodefile)));
try {
for (; ; ) {
NodeData n = new NodeData(di);
listener.nextNode(n);
}
} catch (EOFException eof) {
di.close();
}
listener.nodeFileEnd(nodefile);
if (delete && "true".equals(System.getProperty("deletetmpfiles"))) {
nodefile.delete();
}
}
}

View file

@ -1,17 +1,16 @@
package btools.mapcreator;
import java.io.File;
/**
* Callbacklistener for NodeIterator
*
* @author ab
*/
public interface NodeListener
{
void nodeFileStart( File nodefile ) throws Exception;
void nextNode( NodeData data ) throws Exception;
void nodeFileEnd( File nodefile ) throws Exception;
}
package btools.mapcreator;
import java.io.File;
/**
* Callbacklistener for NodeIterator
*
* @author ab
*/
public interface NodeListener {
void nodeFileStart(File nodefile) throws Exception;
void nextNode(NodeData data) throws Exception;
void nodeFileEnd(File nodefile) throws Exception;
}

View file

@ -1,332 +1,293 @@
/**
* This program
* - reads an *.osm from stdin
* - writes 45*30 degree node tiles + a way file + a rel file
*
* @author ab
*/
package btools.mapcreator;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import btools.expressions.BExpressionContextNode;
import btools.expressions.BExpressionContextWay;
import btools.expressions.BExpressionMetaData;
public class OsmCutter extends MapCreatorBase
{
private long recordCnt;
private long nodesParsed;
private long waysParsed;
private long relsParsed;
private long changesetsParsed;
private DataOutputStream wayDos;
private DataOutputStream cyclewayDos;
private DataOutputStream restrictionsDos;
public WayCutter wayCutter;
public RestrictionCutter restrictionCutter;
public NodeFilter nodeFilter;
public static void main(String[] args) throws Exception
{
System.out.println("*** OsmCutter: cut an osm map in node-tiles + a way file");
if (args.length != 6 && args.length != 7)
{
System.out.println("usage: bzip2 -dc <map> | java OsmCutter <lookup-file> <out-tile-dir> <out-way-file> <out-rel-file> <out-res-file> <filter-profile>");
System.out.println("or : java OsmCutter <lookup-file> <out-tile-dir> <out-way-file> <out-rel-file> <out-res-file> <filter-profile> <inputfile> ");
return;
}
new OsmCutter().process(
new File( args[0] )
, new File( args[1] )
, new File( args[2] )
, new File( args[3] )
, new File( args[4] )
, new File( args[5] )
, args.length > 6 ? new File( args[6] ) : null
);
}
private BExpressionContextWay _expctxWay;
private BExpressionContextNode _expctxNode;
// private BExpressionContextWay _expctxWayStat;
// private BExpressionContextNode _expctxNodeStat;
public void process (File lookupFile, File outTileDir, File wayFile, File relFile, File resFile, File profileFile, File mapFile ) throws Exception
{
if ( !lookupFile.exists() )
{
throw new IllegalArgumentException( "lookup-file: " + lookupFile + " does not exist" );
}
BExpressionMetaData meta = new BExpressionMetaData();
_expctxWay = new BExpressionContextWay( meta );
_expctxNode = new BExpressionContextNode( meta );
meta.readMetaData( lookupFile );
_expctxWay.parseFile( profileFile, "global" );
// _expctxWayStat = new BExpressionContextWay( null );
// _expctxNodeStat = new BExpressionContextNode( null );
this.outTileDir = outTileDir;
if ( !outTileDir.isDirectory() ) throw new RuntimeException( "out tile directory " + outTileDir + " does not exist" );
wayDos = wayFile == null ? null : new DataOutputStream( new BufferedOutputStream( new FileOutputStream( wayFile ) ) );
cyclewayDos = new DataOutputStream( new BufferedOutputStream( new FileOutputStream( relFile ) ) );
if ( resFile != null )
{
restrictionsDos = new DataOutputStream( new BufferedOutputStream( new FileOutputStream( resFile ) ) );
}
// read the osm map into memory
long t0 = System.currentTimeMillis();
new OsmParser().readMap( mapFile, this, this, this );
long t1 = System.currentTimeMillis();
System.out.println( "parsing time (ms) =" + (t1-t0) );
// close all files
closeTileOutStreams();
if ( wayDos != null )
{
wayDos.close();
}
cyclewayDos.close();
if ( restrictionsDos != null )
{
restrictionsDos.close();
}
// System.out.println( "-------- way-statistics -------- " );
// _expctxWayStat.dumpStatistics();
// System.out.println( "-------- node-statistics -------- " );
// _expctxNodeStat.dumpStatistics();
System.out.println( statsLine() );
}
private void checkStats()
{
if ( (++recordCnt % 100000) == 0 ) System.out.println( statsLine() );
}
private String statsLine()
{
return "records read: " + recordCnt + " nodes=" + nodesParsed + " ways=" + waysParsed + " rels=" + relsParsed + " changesets=" + changesetsParsed;
}
@Override
public void nextNode( NodeData n ) throws Exception
{
nodesParsed++;
checkStats();
if ( n.getTagsOrNull() != null )
{
int[] lookupData = _expctxNode.createNewLookupData();
for( Map.Entry<String,String> e : n.getTagsOrNull().entrySet() )
{
_expctxNode.addLookupValue( e.getKey(), e.getValue(), lookupData );
// _expctxNodeStat.addLookupValue( key, value, null );
}
n.description = _expctxNode.encode(lookupData);
}
// write node to file
int tileIndex = getTileIndex( n.ilon, n.ilat );
if ( tileIndex >= 0 )
{
n.writeTo( getOutStreamForTile( tileIndex ) );
if ( wayCutter != null )
{
wayCutter.nextNode( n );
}
}
}
private void generatePseudoTags( HashMap<String,String> map )
{
// add pseudo.tags for concrete:lanes and concrete:plates
String concrete = null;
for( Map.Entry<String,String> e : map.entrySet() )
{
String key = e.getKey();
if ( "concrete".equals( key ) )
{
return;
}
if ( "surface".equals( key ) )
{
String value = e.getValue();
if ( value.startsWith( "concrete:" ) )
{
concrete = value.substring( "concrete:".length() );
}
}
}
if ( concrete != null )
{
map.put( "concrete", concrete );
}
}
@Override
public void nextWay( WayData w ) throws Exception
{
waysParsed++;
checkStats();
// encode tags
if ( w.getTagsOrNull() == null ) return;
generatePseudoTags( w.getTagsOrNull() );
int[] lookupData = _expctxWay.createNewLookupData();
for( String key : w.getTagsOrNull().keySet() )
{
String value = w.getTag( key );
_expctxWay.addLookupValue( key, value.replace( ' ', '_' ), lookupData );
// _expctxWayStat.addLookupValue( key, value, null );
}
w.description = _expctxWay.encode(lookupData);
if ( w.description == null ) return;
// filter according to profile
_expctxWay.evaluate( false, w.description );
boolean ok = _expctxWay.getCostfactor() < 10000.;
_expctxWay.evaluate( true, w.description );
ok |= _expctxWay.getCostfactor() < 10000.;
if ( !ok ) return;
if ( wayDos != null )
{
w.writeTo( wayDos );
}
if ( wayCutter != null )
{
wayCutter.nextWay( w );
}
if ( nodeFilter != null )
{
nodeFilter.nextWay( w );
}
}
@Override
public void nextRelation( RelationData r ) throws Exception
{
relsParsed++;
checkStats();
String route = r.getTag( "route" );
// filter out non-cycle relations
if ( route == null )
{
return;
}
String network = r.getTag( "network" );
if ( network == null ) network = "";
String state = r.getTag( "state" );
if ( state == null ) state = "";
writeId( cyclewayDos, r.rid );
cyclewayDos.writeUTF( route );
cyclewayDos.writeUTF( network );
cyclewayDos.writeUTF( state );
for ( int i=0; i<r.ways.size();i++ )
{
long wid = r.ways.get(i);
writeId( cyclewayDos, wid );
}
writeId( cyclewayDos, -1 );
}
@Override
public void nextRestriction( RelationData r, long fromWid, long toWid, long viaNid ) throws Exception
{
String type = r.getTag( "type" );
if ( type == null || !"restriction".equals( type ) )
{
return;
}
short exceptions = 0;
String except = r.getTag( "except" );
if ( except != null )
{
exceptions |= toBit( "bicycle" , 0, except );
exceptions |= toBit( "motorcar" , 1, except );
exceptions |= toBit( "agricultural" , 2, except );
exceptions |= toBit( "forestry" , 2, except );
exceptions |= toBit( "psv" , 3, except );
exceptions |= toBit( "hgv" , 4, except );
}
for( String restrictionKey : r.getTagsOrNull().keySet() )
{
if ( !( restrictionKey.equals( "restriction" ) || restrictionKey.startsWith( "restriction:" ) ) )
{
continue;
}
String restriction = r.getTag( restrictionKey );
RestrictionData res = new RestrictionData();
res.restrictionKey = restrictionKey;
res.restriction = restriction;
res.exceptions = exceptions;
res.fromWid = fromWid;
res.toWid = toWid;
res.viaNid = viaNid;
if ( restrictionsDos != null )
{
res.writeTo( restrictionsDos );
}
if ( restrictionCutter != null )
{
restrictionCutter.nextRestriction( res );
}
}
}
private static short toBit( String tag, int bitpos, String s )
{
return (short) ( s.indexOf( tag ) < 0 ? 0 : 1 << bitpos );
}
private int getTileIndex( int ilon, int ilat )
{
int lon = ilon / 45000000;
int lat = ilat / 30000000;
if ( lon < 0 || lon > 7 || lat < 0 || lat > 5 )
{
System.out.println( "warning: ignoring illegal pos: " + ilon + "," + ilat );
return -1;
}
return lon*6 + lat;
}
protected String getNameForTile( int tileIndex )
{
int lon = (tileIndex / 6 ) * 45 - 180;
int lat = (tileIndex % 6 ) * 30 - 90;
String slon = lon < 0 ? "W" + (-lon) : "E" + lon;
String slat = lat < 0 ? "S" + (-lat) : "N" + lat;
return slon + "_" + slat + ".ntl";
}
}
/**
* This program
* - reads an *.osm from stdin
* - writes 45*30 degree node tiles + a way file + a rel file
*
* @author ab
*/
package btools.mapcreator;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import btools.expressions.BExpressionContextNode;
import btools.expressions.BExpressionContextWay;
import btools.expressions.BExpressionMetaData;
public class OsmCutter extends MapCreatorBase {
private long recordCnt;
private long nodesParsed;
private long waysParsed;
private long relsParsed;
private long changesetsParsed;
private DataOutputStream wayDos;
private DataOutputStream cyclewayDos;
private DataOutputStream restrictionsDos;
public WayCutter wayCutter;
public RestrictionCutter restrictionCutter;
public NodeFilter nodeFilter;
public static void main(String[] args) throws Exception {
System.out.println("*** OsmCutter: cut an osm map in node-tiles + a way file");
if (args.length != 6 && args.length != 7) {
System.out.println("usage: bzip2 -dc <map> | java OsmCutter <lookup-file> <out-tile-dir> <out-way-file> <out-rel-file> <out-res-file> <filter-profile>");
System.out.println("or : java OsmCutter <lookup-file> <out-tile-dir> <out-way-file> <out-rel-file> <out-res-file> <filter-profile> <inputfile> ");
return;
}
new OsmCutter().process(
new File(args[0])
, new File(args[1])
, new File(args[2])
, new File(args[3])
, new File(args[4])
, new File(args[5])
, args.length > 6 ? new File(args[6]) : null
);
}
private BExpressionContextWay _expctxWay;
private BExpressionContextNode _expctxNode;
// private BExpressionContextWay _expctxWayStat;
// private BExpressionContextNode _expctxNodeStat;
public void process(File lookupFile, File outTileDir, File wayFile, File relFile, File resFile, File profileFile, File mapFile) throws Exception {
if (!lookupFile.exists()) {
throw new IllegalArgumentException("lookup-file: " + lookupFile + " does not exist");
}
BExpressionMetaData meta = new BExpressionMetaData();
_expctxWay = new BExpressionContextWay(meta);
_expctxNode = new BExpressionContextNode(meta);
meta.readMetaData(lookupFile);
_expctxWay.parseFile(profileFile, "global");
// _expctxWayStat = new BExpressionContextWay( null );
// _expctxNodeStat = new BExpressionContextNode( null );
this.outTileDir = outTileDir;
if (!outTileDir.isDirectory())
throw new RuntimeException("out tile directory " + outTileDir + " does not exist");
wayDos = wayFile == null ? null : new DataOutputStream(new BufferedOutputStream(new FileOutputStream(wayFile)));
cyclewayDos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(relFile)));
if (resFile != null) {
restrictionsDos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(resFile)));
}
// read the osm map into memory
long t0 = System.currentTimeMillis();
new OsmParser().readMap(mapFile, this, this, this);
long t1 = System.currentTimeMillis();
System.out.println("parsing time (ms) =" + (t1 - t0));
// close all files
closeTileOutStreams();
if (wayDos != null) {
wayDos.close();
}
cyclewayDos.close();
if (restrictionsDos != null) {
restrictionsDos.close();
}
// System.out.println( "-------- way-statistics -------- " );
// _expctxWayStat.dumpStatistics();
// System.out.println( "-------- node-statistics -------- " );
// _expctxNodeStat.dumpStatistics();
System.out.println(statsLine());
}
private void checkStats() {
if ((++recordCnt % 100000) == 0) System.out.println(statsLine());
}
private String statsLine() {
return "records read: " + recordCnt + " nodes=" + nodesParsed + " ways=" + waysParsed + " rels=" + relsParsed + " changesets=" + changesetsParsed;
}
@Override
public void nextNode(NodeData n) throws Exception {
nodesParsed++;
checkStats();
if (n.getTagsOrNull() != null) {
int[] lookupData = _expctxNode.createNewLookupData();
for (Map.Entry<String, String> e : n.getTagsOrNull().entrySet()) {
_expctxNode.addLookupValue(e.getKey(), e.getValue(), lookupData);
// _expctxNodeStat.addLookupValue( key, value, null );
}
n.description = _expctxNode.encode(lookupData);
}
// write node to file
int tileIndex = getTileIndex(n.ilon, n.ilat);
if (tileIndex >= 0) {
n.writeTo(getOutStreamForTile(tileIndex));
if (wayCutter != null) {
wayCutter.nextNode(n);
}
}
}
private void generatePseudoTags(HashMap<String, String> map) {
// add pseudo.tags for concrete:lanes and concrete:plates
String concrete = null;
for (Map.Entry<String, String> e : map.entrySet()) {
String key = e.getKey();
if ("concrete".equals(key)) {
return;
}
if ("surface".equals(key)) {
String value = e.getValue();
if (value.startsWith("concrete:")) {
concrete = value.substring("concrete:".length());
}
}
}
if (concrete != null) {
map.put("concrete", concrete);
}
}
@Override
public void nextWay(WayData w) throws Exception {
waysParsed++;
checkStats();
// encode tags
if (w.getTagsOrNull() == null) return;
generatePseudoTags(w.getTagsOrNull());
int[] lookupData = _expctxWay.createNewLookupData();
for (String key : w.getTagsOrNull().keySet()) {
String value = w.getTag(key);
_expctxWay.addLookupValue(key, value.replace(' ', '_'), lookupData);
// _expctxWayStat.addLookupValue( key, value, null );
}
w.description = _expctxWay.encode(lookupData);
if (w.description == null) return;
// filter according to profile
_expctxWay.evaluate(false, w.description);
boolean ok = _expctxWay.getCostfactor() < 10000.;
_expctxWay.evaluate(true, w.description);
ok |= _expctxWay.getCostfactor() < 10000.;
if (!ok) return;
if (wayDos != null) {
w.writeTo(wayDos);
}
if (wayCutter != null) {
wayCutter.nextWay(w);
}
if (nodeFilter != null) {
nodeFilter.nextWay(w);
}
}
@Override
public void nextRelation(RelationData r) throws Exception {
relsParsed++;
checkStats();
String route = r.getTag("route");
// filter out non-cycle relations
if (route == null) {
return;
}
String network = r.getTag("network");
if (network == null) network = "";
String state = r.getTag("state");
if (state == null) state = "";
writeId(cyclewayDos, r.rid);
cyclewayDos.writeUTF(route);
cyclewayDos.writeUTF(network);
cyclewayDos.writeUTF(state);
for (int i = 0; i < r.ways.size(); i++) {
long wid = r.ways.get(i);
writeId(cyclewayDos, wid);
}
writeId(cyclewayDos, -1);
}
@Override
public void nextRestriction(RelationData r, long fromWid, long toWid, long viaNid) throws Exception {
String type = r.getTag("type");
if (type == null || !"restriction".equals(type)) {
return;
}
short exceptions = 0;
String except = r.getTag("except");
if (except != null) {
exceptions |= toBit("bicycle", 0, except);
exceptions |= toBit("motorcar", 1, except);
exceptions |= toBit("agricultural", 2, except);
exceptions |= toBit("forestry", 2, except);
exceptions |= toBit("psv", 3, except);
exceptions |= toBit("hgv", 4, except);
}
for (String restrictionKey : r.getTagsOrNull().keySet()) {
if (!(restrictionKey.equals("restriction") || restrictionKey.startsWith("restriction:"))) {
continue;
}
String restriction = r.getTag(restrictionKey);
RestrictionData res = new RestrictionData();
res.restrictionKey = restrictionKey;
res.restriction = restriction;
res.exceptions = exceptions;
res.fromWid = fromWid;
res.toWid = toWid;
res.viaNid = viaNid;
if (restrictionsDos != null) {
res.writeTo(restrictionsDos);
}
if (restrictionCutter != null) {
restrictionCutter.nextRestriction(res);
}
}
}
private static short toBit(String tag, int bitpos, String s) {
return (short) (s.indexOf(tag) < 0 ? 0 : 1 << bitpos);
}
private int getTileIndex(int ilon, int ilat) {
int lon = ilon / 45000000;
int lat = ilat / 30000000;
if (lon < 0 || lon > 7 || lat < 0 || lat > 5) {
System.out.println("warning: ignoring illegal pos: " + ilon + "," + ilat);
return -1;
}
return lon * 6 + lat;
}
protected String getNameForTile(int tileIndex) {
int lon = (tileIndex / 6) * 45 - 180;
int lat = (tileIndex % 6) * 30 - 90;
String slon = lon < 0 ? "W" + (-lon) : "E" + lon;
String slat = lat < 0 ? "S" + (-lat) : "N" + lat;
return slon + "_" + slat + ".ntl";
}
}

View file

@ -9,55 +9,51 @@ package btools.mapcreator;
import java.io.File;
public class OsmFastCutter extends MapCreatorBase
{
public static void main(String[] args) throws Exception
{
public class OsmFastCutter extends MapCreatorBase {
public static void main(String[] args) throws Exception {
System.out.println("*** OsmFastCutter: cut an osm map in node-tiles + way-tiles");
if (args.length != 11 && args.length != 12)
{
if (args.length != 11 && args.length != 12) {
String common = "java OsmFastCutter <lookup-file> <node-dir> <way-dir> <node55-dir> <way55-dir> <border-file> <out-rel-file> <out-res-file> <filter-profile> <report-profile> <check-profile>";
System.out.println("usage: bzip2 -dc <map> | " + common );
System.out.println("or : " + common + " <inputfile> " );
System.out.println("usage: bzip2 -dc <map> | " + common);
System.out.println("or : " + common + " <inputfile> ");
return;
}
doCut(
new File( args[0] )
, new File( args[1] )
, new File( args[2] )
, new File( args[3] )
, new File( args[4] )
, new File( args[5] )
, new File( args[6] )
, new File( args[7] )
, new File( args[8] )
, new File( args[9] )
, new File( args[10] )
, args.length > 11 ? new File( args[11] ) : null
);
new File(args[0])
, new File(args[1])
, new File(args[2])
, new File(args[3])
, new File(args[4])
, new File(args[5])
, new File(args[6])
, new File(args[7])
, new File(args[8])
, new File(args[9])
, new File(args[10])
, args.length > 11 ? new File(args[11]) : null
);
}
public static void doCut (File lookupFile, File nodeDir, File wayDir, File node55Dir, File way55Dir, File borderFile, File relFile, File resFile, File profileAll, File profileReport, File profileCheck, File mapFile ) throws Exception
{
public static void doCut(File lookupFile, File nodeDir, File wayDir, File node55Dir, File way55Dir, File borderFile, File relFile, File resFile, File profileAll, File profileReport, File profileCheck, File mapFile) throws Exception {
// **** run OsmCutter ****
OsmCutter cutter = new OsmCutter();
// ... inject WayCutter
cutter.wayCutter = new WayCutter();
cutter.wayCutter.init( wayDir );
cutter.wayCutter.init(wayDir);
// ... inject RestrictionCutter
cutter.restrictionCutter = new RestrictionCutter();
cutter.restrictionCutter.init( new File( nodeDir.getParentFile(), "restrictions" ), cutter.wayCutter );
cutter.restrictionCutter.init(new File(nodeDir.getParentFile(), "restrictions"), cutter.wayCutter);
// ... inject NodeFilter
NodeFilter nodeFilter = new NodeFilter();
nodeFilter.init();
cutter.nodeFilter = nodeFilter;
cutter.process( lookupFile, nodeDir, null, relFile, null, profileAll, mapFile );
cutter.process(lookupFile, nodeDir, null, relFile, null, profileAll, mapFile);
cutter.wayCutter.finish();
cutter.restrictionCutter.finish();
cutter = null;
@ -67,20 +63,20 @@ public class OsmFastCutter extends MapCreatorBase
//... inject RelationMerger
wayCut5.relMerger = new RelationMerger();
wayCut5.relMerger.init( relFile, lookupFile, profileReport, profileCheck );
wayCut5.relMerger.init(relFile, lookupFile, profileReport, profileCheck);
// ... inject RestrictionCutter5
wayCut5.restrictionCutter5 = new RestrictionCutter5();
wayCut5.restrictionCutter5.init( new File( nodeDir.getParentFile(), "restrictions55" ), wayCut5 );
wayCut5.restrictionCutter5.init(new File(nodeDir.getParentFile(), "restrictions55"), wayCut5);
//... inject NodeFilter
wayCut5.nodeFilter = nodeFilter;
// ... inject NodeCutter
wayCut5.nodeCutter = new NodeCutter();
wayCut5.nodeCutter.init( node55Dir );
wayCut5.nodeCutter.init(node55Dir);
wayCut5.process( nodeDir, wayDir, way55Dir, borderFile );
wayCut5.process(nodeDir, wayDir, way55Dir, borderFile);
}
}

View file

@ -1,118 +1,90 @@
/**
* Container for link between two Osm nodes (pre-pocessor version)
*
* @author ab
*/
package btools.mapcreator;
public class OsmLinkP
{
/**
* The description bitmap is mainly the way description
* used to calculate the costfactor
*/
public byte[] descriptionBitmap;
/**
* The target is either the next link or the target node
*/
protected OsmNodeP sourceNode;
protected OsmNodeP targetNode;
protected OsmLinkP previous;
protected OsmLinkP next;
public OsmLinkP( OsmNodeP source, OsmNodeP target )
{
sourceNode = source;
targetNode = target;
}
protected OsmLinkP()
{
}
public final boolean counterLinkWritten( )
{
return descriptionBitmap == null;
}
/**
* Set the relevant next-pointer for the given source
*/
public void setNext( OsmLinkP link, OsmNodeP source )
{
if ( sourceNode == source )
{
next = link;
}
else if ( targetNode == source )
{
previous = link;
}
else
{
throw new IllegalArgumentException( "internal error: setNext: unknown source" );
}
}
/**
* Get the relevant next-pointer for the given source
*/
public OsmLinkP getNext( OsmNodeP source )
{
if ( sourceNode == source )
{
return next;
}
else if ( targetNode == source )
{
return previous;
}
else
{
throw new IllegalArgumentException( "internal error: gextNext: unknown source" );
}
}
/**
* Get the relevant target-node for the given source
*/
public OsmNodeP getTarget( OsmNodeP source )
{
if ( sourceNode == source )
{
return targetNode;
}
else if ( targetNode == source )
{
return sourceNode;
}
else
{
throw new IllegalArgumentException( "internal error: getTarget: unknown source" );
}
}
/**
* Check if reverse link for the given source
*/
public boolean isReverse( OsmNodeP source )
{
if ( sourceNode == source )
{
return false;
}
else if ( targetNode == source )
{
return true;
}
else
{
throw new IllegalArgumentException( "internal error: isReverse: unknown source" );
}
}
}
/**
* Container for link between two Osm nodes (pre-pocessor version)
*
* @author ab
*/
package btools.mapcreator;
public class OsmLinkP {
/**
* The description bitmap is mainly the way description
* used to calculate the costfactor
*/
public byte[] descriptionBitmap;
/**
* The target is either the next link or the target node
*/
protected OsmNodeP sourceNode;
protected OsmNodeP targetNode;
protected OsmLinkP previous;
protected OsmLinkP next;
public OsmLinkP(OsmNodeP source, OsmNodeP target) {
sourceNode = source;
targetNode = target;
}
protected OsmLinkP() {
}
public final boolean counterLinkWritten() {
return descriptionBitmap == null;
}
/**
* Set the relevant next-pointer for the given source
*/
public void setNext(OsmLinkP link, OsmNodeP source) {
if (sourceNode == source) {
next = link;
} else if (targetNode == source) {
previous = link;
} else {
throw new IllegalArgumentException("internal error: setNext: unknown source");
}
}
/**
* Get the relevant next-pointer for the given source
*/
public OsmLinkP getNext(OsmNodeP source) {
if (sourceNode == source) {
return next;
} else if (targetNode == source) {
return previous;
} else {
throw new IllegalArgumentException("internal error: gextNext: unknown source");
}
}
/**
* Get the relevant target-node for the given source
*/
public OsmNodeP getTarget(OsmNodeP source) {
if (sourceNode == source) {
return targetNode;
} else if (targetNode == source) {
return sourceNode;
} else {
throw new IllegalArgumentException("internal error: getTarget: unknown source");
}
}
/**
* Check if reverse link for the given source
*/
public boolean isReverse(OsmNodeP source) {
if (sourceNode == source) {
return false;
} else if (targetNode == source) {
return true;
} else {
throw new IllegalArgumentException("internal error: isReverse: unknown source");
}
}
}

View file

@ -12,8 +12,7 @@ import java.util.HashMap;
import btools.codec.MicroCache;
import btools.codec.MicroCache2;
public class OsmNodeP extends OsmLinkP
{
public class OsmNodeP extends OsmLinkP {
/**
* The latitude
*/
@ -40,311 +39,261 @@ public class OsmNodeP extends OsmLinkP
public byte bits = 0;
// interface OsmPos
public int getILat()
{
public int getILat() {
return ilat;
}
public int getILon()
{
public int getILon() {
return ilon;
}
public short getSElev()
{
public short getSElev() {
// if all bridge or all tunnel, elevation=no-data
return ( bits & NO_BRIDGE_BIT ) == 0 || ( bits & NO_TUNNEL_BIT ) == 0 ? Short.MIN_VALUE : selev;
return (bits & NO_BRIDGE_BIT) == 0 || (bits & NO_TUNNEL_BIT) == 0 ? Short.MIN_VALUE : selev;
}
public double getElev()
{
public double getElev() {
return selev / 4.;
}
// populate and return the inherited link, if available,
// else create a new one
public OsmLinkP createLink( OsmNodeP source )
{
if ( sourceNode == null && targetNode == null )
{
public OsmLinkP createLink(OsmNodeP source) {
if (sourceNode == null && targetNode == null) {
// inherited instance is available, use this
sourceNode = source;
targetNode = this;
source.addLink( this );
source.addLink(this);
return this;
}
OsmLinkP link = new OsmLinkP( source, this );
addLink( link );
source.addLink( link );
OsmLinkP link = new OsmLinkP(source, this);
addLink(link);
source.addLink(link);
return link;
}
// memory-squeezing-hack: OsmLinkP's "previous" also used as firstlink..
public void addLink( OsmLinkP link )
{
link.setNext( previous, this );
public void addLink(OsmLinkP link) {
link.setNext(previous, this);
previous = link;
}
public OsmLinkP getFirstLink()
{
public OsmLinkP getFirstLink() {
return sourceNode == null && targetNode == null ? previous : this;
}
public byte[] getNodeDecsription()
{
public byte[] getNodeDecsription() {
return null;
}
public RestrictionData getFirstRestriction()
{
public RestrictionData getFirstRestriction() {
return null;
}
public void writeNodeData( MicroCache mc, OsmTrafficMap trafficMap ) throws IOException
{
public void writeNodeData(MicroCache mc, OsmTrafficMap trafficMap) throws IOException {
boolean valid = true;
if ( mc instanceof MicroCache2 )
{
valid = writeNodeData2( (MicroCache2) mc, trafficMap );
}
else
throw new IllegalArgumentException( "unknown cache version: " + mc.getClass() );
if ( valid )
{
mc.finishNode( getIdFromPos() );
}
else
{
if (mc instanceof MicroCache2) {
valid = writeNodeData2((MicroCache2) mc, trafficMap);
} else
throw new IllegalArgumentException("unknown cache version: " + mc.getClass());
if (valid) {
mc.finishNode(getIdFromPos());
} else {
mc.discardNode();
}
}
public void checkDuplicateTargets()
{
HashMap<OsmNodeP,OsmLinkP> targets = new HashMap<OsmNodeP,OsmLinkP>();
public void checkDuplicateTargets() {
HashMap<OsmNodeP, OsmLinkP> targets = new HashMap<OsmNodeP, OsmLinkP>();
for ( OsmLinkP link0 = getFirstLink(); link0 != null; link0 = link0.getNext( this ) )
{
for (OsmLinkP link0 = getFirstLink(); link0 != null; link0 = link0.getNext(this)) {
OsmLinkP link = link0;
OsmNodeP origin = this;
OsmNodeP target = null;
// first pass just to see if that link is consistent
while (link != null)
{
target = link.getTarget( origin );
if ( !target.isTransferNode() )
{
while (link != null) {
target = link.getTarget(origin);
if (!target.isTransferNode()) {
break;
}
// next link is the one (of two), does does'nt point back
for ( link = target.getFirstLink(); link != null; link = link.getNext( target ) )
{
if ( link.getTarget( target ) != origin )
for (link = target.getFirstLink(); link != null; link = link.getNext(target)) {
if (link.getTarget(target) != origin)
break;
}
origin = target;
}
if ( link == null ) continue;
OsmLinkP oldLink = targets.put( target, link0 );
if ( oldLink != null )
{
unifyLink( oldLink );
unifyLink( link0 );
if (link == null) continue;
OsmLinkP oldLink = targets.put(target, link0);
if (oldLink != null) {
unifyLink(oldLink);
unifyLink(link0);
}
}
}
private void unifyLink( OsmLinkP link )
{
if ( link.isReverse( this ) ) return;
OsmNodeP target = link.getTarget( this );
if ( target.isTransferNode() )
{
private void unifyLink(OsmLinkP link) {
if (link.isReverse(this)) return;
OsmNodeP target = link.getTarget(this);
if (target.isTransferNode()) {
target.incWayCount();
}
}
public boolean writeNodeData2( MicroCache2 mc, OsmTrafficMap trafficMap ) throws IOException
{
public boolean writeNodeData2(MicroCache2 mc, OsmTrafficMap trafficMap) throws IOException {
boolean hasLinks = false;
// write turn restrictions
RestrictionData r = getFirstRestriction();
while( r != null )
{
if ( r.isValid() && r.fromLon != 0 && r.toLon != 0 )
{
mc.writeBoolean( true ); // restriction follows
mc.writeShort( r.exceptions );
mc.writeBoolean( r.isPositive() );
mc.writeInt( r.fromLon );
mc.writeInt( r.fromLat );
mc.writeInt( r.toLon );
mc.writeInt( r.toLat );
while (r != null) {
if (r.isValid() && r.fromLon != 0 && r.toLon != 0) {
mc.writeBoolean(true); // restriction follows
mc.writeShort(r.exceptions);
mc.writeBoolean(r.isPositive());
mc.writeInt(r.fromLon);
mc.writeInt(r.fromLat);
mc.writeInt(r.toLon);
mc.writeInt(r.toLat);
}
r = r.next;
}
mc.writeBoolean( false ); // end restritions
mc.writeBoolean(false); // end restritions
mc.writeShort( getSElev() );
mc.writeVarBytes( getNodeDecsription() );
mc.writeShort(getSElev());
mc.writeVarBytes(getNodeDecsription());
// buffer internal reverse links
ArrayList<OsmNodeP> internalReverse = new ArrayList<OsmNodeP>();
for ( OsmLinkP link0 = getFirstLink(); link0 != null; link0 = link0.getNext( this ) )
{
for (OsmLinkP link0 = getFirstLink(); link0 != null; link0 = link0.getNext(this)) {
OsmLinkP link = link0;
OsmNodeP origin = this;
OsmNodeP target = null;
ArrayList<OsmNodeP> linkNodes = new ArrayList<OsmNodeP>();
linkNodes.add( this );
linkNodes.add(this);
// first pass just to see if that link is consistent
while (link != null)
{
target = link.getTarget( origin );
linkNodes.add( target );
while (link != null) {
target = link.getTarget(origin);
linkNodes.add(target);
if ( !target.isTransferNode() )
{
if (!target.isTransferNode()) {
break;
}
// next link is the one (of two), does does'nt point back
for ( link = target.getFirstLink(); link != null; link = link.getNext( target ) )
{
if ( link.getTarget( target ) != origin )
for (link = target.getFirstLink(); link != null; link = link.getNext(target)) {
if (link.getTarget(target) != origin)
break;
}
if ( link != null && link.descriptionBitmap != link0.descriptionBitmap )
{
throw new IllegalArgumentException( "assertion failed: description change along transfer nodes" );
if (link != null && link.descriptionBitmap != link0.descriptionBitmap) {
throw new IllegalArgumentException("assertion failed: description change along transfer nodes");
}
origin = target;
}
if ( link == null )
if (link == null)
continue; // dead end
if ( target == this )
if (target == this)
continue; // self-ref
hasLinks = true;
// internal reverse links later
boolean isReverse = link0.isReverse( this );
if ( isReverse )
{
if ( mc.isInternal( target.ilon, target.ilat ) )
{
internalReverse.add( target );
boolean isReverse = link0.isReverse(this);
if (isReverse) {
if (mc.isInternal(target.ilon, target.ilat)) {
internalReverse.add(target);
continue;
}
}
// add traffic simulation, if present
byte[] description = link0.descriptionBitmap;
if ( trafficMap != null )
{
description = trafficMap.addTrafficClass( linkNodes, description );
if (trafficMap != null) {
description = trafficMap.addTrafficClass(linkNodes, description);
}
// write link data
int sizeoffset = mc.writeSizePlaceHolder();
mc.writeVarLengthSigned( target.ilon - ilon );
mc.writeVarLengthSigned( target.ilat - ilat );
mc.writeModeAndDesc( isReverse, description );
if ( !isReverse && linkNodes.size() > 2 ) // write geometry for forward links only
mc.writeVarLengthSigned(target.ilon - ilon);
mc.writeVarLengthSigned(target.ilat - ilat);
mc.writeModeAndDesc(isReverse, description);
if (!isReverse && linkNodes.size() > 2) // write geometry for forward links only
{
DPFilter.doDPFilter( linkNodes );
DPFilter.doDPFilter(linkNodes);
origin = this;
for( int i=1; i<linkNodes.size()-1; i++ )
{
for (int i = 1; i < linkNodes.size() - 1; i++) {
OsmNodeP tranferNode = linkNodes.get(i);
if ( ( tranferNode.bits & OsmNodeP.DP_SURVIVOR_BIT ) != 0 )
{
mc.writeVarLengthSigned( tranferNode.ilon - origin.ilon );
mc.writeVarLengthSigned( tranferNode.ilat - origin.ilat );
mc.writeVarLengthSigned( tranferNode.getSElev() - origin.getSElev() );
if ((tranferNode.bits & OsmNodeP.DP_SURVIVOR_BIT) != 0) {
mc.writeVarLengthSigned(tranferNode.ilon - origin.ilon);
mc.writeVarLengthSigned(tranferNode.ilat - origin.ilat);
mc.writeVarLengthSigned(tranferNode.getSElev() - origin.getSElev());
origin = tranferNode;
}
}
}
mc.injectSize( sizeoffset );
mc.injectSize(sizeoffset);
}
while (internalReverse.size() > 0)
{
while (internalReverse.size() > 0) {
int nextIdx = 0;
if ( internalReverse.size() > 1 )
{
if (internalReverse.size() > 1) {
int max32 = Integer.MIN_VALUE;
for ( int i = 0; i < internalReverse.size(); i++ )
{
int id32 = mc.shrinkId( internalReverse.get( i ).getIdFromPos() );
if ( id32 > max32 )
{
for (int i = 0; i < internalReverse.size(); i++) {
int id32 = mc.shrinkId(internalReverse.get(i).getIdFromPos());
if (id32 > max32) {
max32 = id32;
nextIdx = i;
}
}
}
OsmNodeP target = internalReverse.remove( nextIdx );
OsmNodeP target = internalReverse.remove(nextIdx);
int sizeoffset = mc.writeSizePlaceHolder();
mc.writeVarLengthSigned( target.ilon - ilon );
mc.writeVarLengthSigned( target.ilat - ilat );
mc.writeModeAndDesc( true, null );
mc.injectSize( sizeoffset );
mc.writeVarLengthSigned(target.ilon - ilon);
mc.writeVarLengthSigned(target.ilat - ilat);
mc.writeModeAndDesc(true, null);
mc.injectSize(sizeoffset);
}
return hasLinks;
}
public String toString2()
{
return ( ilon - 180000000 ) + "_" + ( ilat - 90000000 ) + "_" + ( selev / 4 );
public String toString2() {
return (ilon - 180000000) + "_" + (ilat - 90000000) + "_" + (selev / 4);
}
public long getIdFromPos()
{
return ( (long) ilon ) << 32 | ilat;
public long getIdFromPos() {
return ((long) ilon) << 32 | ilat;
}
public boolean isBorderNode()
{
return ( bits & BORDER_BIT ) != 0;
public boolean isBorderNode() {
return (bits & BORDER_BIT) != 0;
}
public boolean hasTraffic()
{
return ( bits & TRAFFIC_BIT ) != 0;
public boolean hasTraffic() {
return (bits & TRAFFIC_BIT) != 0;
}
/**
* Not really count the ways, just detect if more than one
*/
public void incWayCount()
{
if ( ( bits & ANY_WAY_BIT ) != 0 )
{
public void incWayCount() {
if ((bits & ANY_WAY_BIT) != 0) {
bits |= MULTI_WAY_BIT;
}
bits |= ANY_WAY_BIT;
}
public boolean isTransferNode()
{
return ( bits & BORDER_BIT ) == 0 && ( bits & MULTI_WAY_BIT ) == 0 && _linkCnt() == 2;
public boolean isTransferNode() {
return (bits & BORDER_BIT) == 0 && (bits & MULTI_WAY_BIT) == 0 && _linkCnt() == 2;
}
private int _linkCnt()
{
private int _linkCnt() {
int cnt = 0;
for ( OsmLinkP link = getFirstLink(); link != null; link = link.getNext( this ) )
{
for (OsmLinkP link = getFirstLink(); link != null; link = link.getNext(this)) {
cnt++;
}
return cnt;

View file

@ -1,50 +1,43 @@
/**
* Container for an osm node with tags or restrictions (pre-pocessor version)
*
* @author ab
*/
package btools.mapcreator;
public class OsmNodePT extends OsmNodeP
{
public byte[] descriptionBits;
public RestrictionData firstRestriction;
public OsmNodePT()
{
}
public OsmNodePT( OsmNodeP n )
{
ilat = n.ilat;
ilon = n.ilon;
selev = n.selev;
bits = n.bits;
}
public OsmNodePT( byte[] descriptionBits )
{
this.descriptionBits = descriptionBits;
}
@Override
public final byte[] getNodeDecsription()
{
return descriptionBits;
}
@Override
public final RestrictionData getFirstRestriction()
{
return firstRestriction;
}
@Override
public boolean isTransferNode()
{
return false; // always have descriptionBits so never transfernode
}
}
/**
* Container for an osm node with tags or restrictions (pre-pocessor version)
*
* @author ab
*/
package btools.mapcreator;
public class OsmNodePT extends OsmNodeP {
public byte[] descriptionBits;
public RestrictionData firstRestriction;
public OsmNodePT() {
}
public OsmNodePT(OsmNodeP n) {
ilat = n.ilat;
ilon = n.ilon;
selev = n.selev;
bits = n.bits;
}
public OsmNodePT(byte[] descriptionBits) {
this.descriptionBits = descriptionBits;
}
@Override
public final byte[] getNodeDecsription() {
return descriptionBits;
}
@Override
public final RestrictionData getFirstRestriction() {
return firstRestriction;
}
@Override
public boolean isTransferNode() {
return false; // always have descriptionBits so never transfernode
}
}

View file

@ -1,236 +1,200 @@
package btools.mapcreator;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;
/**
* Parser for OSM data
*
* @author ab
*/
public class OsmParser extends MapCreatorBase
{
private BufferedReader _br;
private NodeListener nListener;
private WayListener wListener;
private RelationListener rListener;
public void readMap( File mapFile,
NodeListener nListener,
WayListener wListener,
RelationListener rListener ) throws Exception
{
this.nListener = nListener;
this.wListener = wListener;
this.rListener = rListener;
if ( mapFile == null )
{
_br = new BufferedReader(new InputStreamReader(System.in));
}
else
{
if ( mapFile.getName().endsWith( ".gz" ) )
{
_br = new BufferedReader(new InputStreamReader( new GZIPInputStream( new FileInputStream( mapFile ) ) ) );
}
else
{
_br = new BufferedReader(new InputStreamReader( new FileInputStream( mapFile ) ) );
}
}
for(;;)
{
String line = _br.readLine();
if ( line == null ) break;
if ( checkNode( line ) ) continue;
if ( checkWay( line ) ) continue;
if ( checkRelation( line ) ) continue;
if ( checkChangeset( line ) ) continue;
}
if ( mapFile != null )
{
_br.close();
}
}
private boolean checkNode( String line ) throws Exception
{
int idx0 = line.indexOf( "<node id=\"" );
if ( idx0 < 0 ) return false;
idx0 += 10;
int idx1 = line.indexOf( '"', idx0 );
long nodeId = Long.parseLong( line.substring( idx0, idx1 ) );
int idx2 = line.indexOf( " lat=\"" );
if ( idx2 < 0 ) return false;
idx2 += 6;
int idx3 = line.indexOf( '"', idx2 );
double lat = Double.parseDouble( line.substring( idx2, idx3 ) );
int idx4 = line.indexOf( " lon=\"" );
if ( idx4 < 0 ) return false;
idx4 += 6;
int idx5 = line.indexOf( '"', idx4 );
double lon = Double.parseDouble( line.substring( idx4, idx5 ) );
NodeData n = new NodeData( nodeId, lon, lat );
if ( !line.endsWith( "/>" ) )
{
// read additional tags
for(;;)
{
String l2 = _br.readLine();
if ( l2 == null ) return false;
int i2;
if ( (i2 = l2.indexOf( "<tag k=\"" )) >= 0 )
{ // property-tag
i2 += 8;
int ri2 = l2.indexOf( '"', i2 );
String key = l2.substring( i2, ri2 );
i2 = l2.indexOf( " v=\"", ri2 );
if ( i2 >= 0 )
{
i2 += 4;
int ri3 = l2.indexOf( '"', i2 );
String value = l2.substring( i2, ri3 );
n.putTag( key, value );
}
}
else if ( l2.indexOf( "</node>" ) >= 0 )
{ // end-tag
break;
}
}
}
nListener.nextNode( n );
return true;
}
private boolean checkWay( String line ) throws Exception
{
int idx0 = line.indexOf( "<way id=\"" );
if ( idx0 < 0 ) return false;
idx0 += 9;
int idx1 = line.indexOf( '"', idx0 );
long id = Long.parseLong( line.substring( idx0, idx1 ) );
WayData w = new WayData( id );
// read the nodes
for(;;)
{
String l2 = _br.readLine();
if ( l2 == null ) return false;
int i2;
if ( (i2 = l2.indexOf( "<nd ref=\"" )) >= 0 )
{ // node reference
i2 += 9;
int ri2 = l2.indexOf( '"', i2 );
long nid = Long.parseLong( l2.substring( i2, ri2 ) );
w.nodes.add( nid );
}
else if ( (i2 = l2.indexOf( "<tag k=\"" )) >= 0 )
{ // property-tag
i2 += 8;
int ri2 = l2.indexOf( '"', i2 );
String key = l2.substring( i2, ri2 );
i2 = l2.indexOf( " v=\"", ri2 );
if ( i2 >= 0 )
{
i2 += 4;
int ri3 = l2.indexOf( '"', i2 );
String value = l2.substring( i2, ri3 );
w.putTag( key, value );
}
}
else if ( l2.indexOf( "</way>" ) >= 0 )
{ // end-tag
break;
}
}
wListener.nextWay( w );
return true;
}
private boolean checkChangeset( String line ) throws Exception
{
int idx0 = line.indexOf( "<changeset id=\"" );
if ( idx0 < 0 ) return false;
if ( !line.endsWith( "/>" ) )
{
int loopcheck = 0;
for(;;)
{
String l2 = _br.readLine();
if ( l2.indexOf("</changeset>") >= 0 || ++loopcheck > 10000 ) break;
}
}
return true;
}
private boolean checkRelation( String line ) throws Exception
{
int idx0 = line.indexOf( "<relation id=\"" );
if ( idx0 < 0 ) return false;
idx0 += 14;
int idx1 = line.indexOf( '"', idx0 );
long rid = Long.parseLong( line.substring( idx0, idx1 ) );
RelationData r = new RelationData( rid );
// read the nodes
for(;;)
{
String l2 = _br.readLine();
if ( l2 == null ) return false;
int i2;
if ( (i2 = l2.indexOf( "<member type=\"way\" ref=\"" )) >= 0 )
{ // node reference
i2 += 24;
int ri2 = l2.indexOf( '"', i2 );
long wid = Long.parseLong( l2.substring( i2, ri2 ) );
r.ways.add( wid );
}
else if ( (i2 = l2.indexOf( "<tag k=\"" )) >= 0 )
{ // property-tag
i2 += 8;
int ri2 = l2.indexOf( '"', i2 );
String key = l2.substring( i2, ri2 );
i2 = l2.indexOf( " v=\"", ri2 );
if ( i2 >= 0 )
{
i2 += 4;
int ri3 = l2.indexOf( '"', i2 );
String value = l2.substring( i2, ri3 );
r.putTag( key, value );
}
}
else if ( l2.indexOf( "</relation>" ) >= 0 )
{ // end-tag
break;
}
}
rListener.nextRelation( r );
return true;
}
}
package btools.mapcreator;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;
/**
* Parser for OSM data
*
* @author ab
*/
public class OsmParser extends MapCreatorBase {
private BufferedReader _br;
private NodeListener nListener;
private WayListener wListener;
private RelationListener rListener;
public void readMap(File mapFile,
NodeListener nListener,
WayListener wListener,
RelationListener rListener) throws Exception {
this.nListener = nListener;
this.wListener = wListener;
this.rListener = rListener;
if (mapFile == null) {
_br = new BufferedReader(new InputStreamReader(System.in));
} else {
if (mapFile.getName().endsWith(".gz")) {
_br = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(mapFile))));
} else {
_br = new BufferedReader(new InputStreamReader(new FileInputStream(mapFile)));
}
}
for (; ; ) {
String line = _br.readLine();
if (line == null) break;
if (checkNode(line)) continue;
if (checkWay(line)) continue;
if (checkRelation(line)) continue;
if (checkChangeset(line)) continue;
}
if (mapFile != null) {
_br.close();
}
}
private boolean checkNode(String line) throws Exception {
int idx0 = line.indexOf("<node id=\"");
if (idx0 < 0) return false;
idx0 += 10;
int idx1 = line.indexOf('"', idx0);
long nodeId = Long.parseLong(line.substring(idx0, idx1));
int idx2 = line.indexOf(" lat=\"");
if (idx2 < 0) return false;
idx2 += 6;
int idx3 = line.indexOf('"', idx2);
double lat = Double.parseDouble(line.substring(idx2, idx3));
int idx4 = line.indexOf(" lon=\"");
if (idx4 < 0) return false;
idx4 += 6;
int idx5 = line.indexOf('"', idx4);
double lon = Double.parseDouble(line.substring(idx4, idx5));
NodeData n = new NodeData(nodeId, lon, lat);
if (!line.endsWith("/>")) {
// read additional tags
for (; ; ) {
String l2 = _br.readLine();
if (l2 == null) return false;
int i2;
if ((i2 = l2.indexOf("<tag k=\"")) >= 0) { // property-tag
i2 += 8;
int ri2 = l2.indexOf('"', i2);
String key = l2.substring(i2, ri2);
i2 = l2.indexOf(" v=\"", ri2);
if (i2 >= 0) {
i2 += 4;
int ri3 = l2.indexOf('"', i2);
String value = l2.substring(i2, ri3);
n.putTag(key, value);
}
} else if (l2.indexOf("</node>") >= 0) { // end-tag
break;
}
}
}
nListener.nextNode(n);
return true;
}
private boolean checkWay(String line) throws Exception {
int idx0 = line.indexOf("<way id=\"");
if (idx0 < 0) return false;
idx0 += 9;
int idx1 = line.indexOf('"', idx0);
long id = Long.parseLong(line.substring(idx0, idx1));
WayData w = new WayData(id);
// read the nodes
for (; ; ) {
String l2 = _br.readLine();
if (l2 == null) return false;
int i2;
if ((i2 = l2.indexOf("<nd ref=\"")) >= 0) { // node reference
i2 += 9;
int ri2 = l2.indexOf('"', i2);
long nid = Long.parseLong(l2.substring(i2, ri2));
w.nodes.add(nid);
} else if ((i2 = l2.indexOf("<tag k=\"")) >= 0) { // property-tag
i2 += 8;
int ri2 = l2.indexOf('"', i2);
String key = l2.substring(i2, ri2);
i2 = l2.indexOf(" v=\"", ri2);
if (i2 >= 0) {
i2 += 4;
int ri3 = l2.indexOf('"', i2);
String value = l2.substring(i2, ri3);
w.putTag(key, value);
}
} else if (l2.indexOf("</way>") >= 0) { // end-tag
break;
}
}
wListener.nextWay(w);
return true;
}
private boolean checkChangeset(String line) throws Exception {
int idx0 = line.indexOf("<changeset id=\"");
if (idx0 < 0) return false;
if (!line.endsWith("/>")) {
int loopcheck = 0;
for (; ; ) {
String l2 = _br.readLine();
if (l2.indexOf("</changeset>") >= 0 || ++loopcheck > 10000) break;
}
}
return true;
}
private boolean checkRelation(String line) throws Exception {
int idx0 = line.indexOf("<relation id=\"");
if (idx0 < 0) return false;
idx0 += 14;
int idx1 = line.indexOf('"', idx0);
long rid = Long.parseLong(line.substring(idx0, idx1));
RelationData r = new RelationData(rid);
// read the nodes
for (; ; ) {
String l2 = _br.readLine();
if (l2 == null) return false;
int i2;
if ((i2 = l2.indexOf("<member type=\"way\" ref=\"")) >= 0) { // node reference
i2 += 24;
int ri2 = l2.indexOf('"', i2);
long wid = Long.parseLong(l2.substring(i2, ri2));
r.ways.add(wid);
} else if ((i2 = l2.indexOf("<tag k=\"")) >= 0) { // property-tag
i2 += 8;
int ri2 = l2.indexOf('"', i2);
String key = l2.substring(i2, ri2);
i2 = l2.indexOf(" v=\"", ri2);
if (i2 >= 0) {
i2 += 4;
int ri3 = l2.indexOf('"', i2);
String value = l2.substring(i2, ri3);
r.putTag(key, value);
}
} else if (l2.indexOf("</relation>") >= 0) { // end-tag
break;
}
}
rListener.nextRelation(r);
return true;
}
}

View file

@ -22,8 +22,7 @@ import btools.util.CompactLongMap;
import btools.util.FrozenLongMap;
public class OsmTrafficMap
{
public class OsmTrafficMap {
int minLon;
int minLat;
int maxLon;
@ -38,243 +37,206 @@ public class OsmTrafficMap
private int totalChanges = 0;
private int supressedChanges = 0;
private boolean doNotAdd = false;
private boolean debug = false;
public OsmTrafficMap( BExpressionContextWay expctxWay )
{
public OsmTrafficMap(BExpressionContextWay expctxWay) {
this.expctxWay = expctxWay;
debug = Boolean.getBoolean( "debugTrafficMap" );
debug = Boolean.getBoolean("debugTrafficMap");
}
public static class OsmTrafficElement
{
public static class OsmTrafficElement {
public long node2;
public int traffic;
public OsmTrafficElement next;
}
private CompactLongMap<OsmTrafficElement> map = new CompactLongMap<OsmTrafficElement>();
public void loadAll( File file, int minLon, int minLat, int maxLon, int maxLat, boolean includeMotorways ) throws Exception
{
load( file, minLon, minLat, maxLon, maxLat, includeMotorways );
public void loadAll(File file, int minLon, int minLat, int maxLon, int maxLat, boolean includeMotorways) throws Exception {
load(file, minLon, minLat, maxLon, maxLat, includeMotorways);
// check for old traffic data
oldTrafficFile = new File( file.getParentFile(), file.getName() + "_old" );
if ( oldTrafficFile.exists() )
{
oldTrafficClasses = new OsmTrafficMap( null );
oldTrafficFile = new File(file.getParentFile(), file.getName() + "_old");
if (oldTrafficFile.exists()) {
oldTrafficClasses = new OsmTrafficMap(null);
oldTrafficClasses.doNotAdd = true;
oldTrafficClasses.load( oldTrafficFile, minLon, minLat, maxLon, maxLat, false );
oldTrafficClasses.load(oldTrafficFile, minLon, minLat, maxLon, maxLat, false);
}
// check for old traffic data
newTrafficFile = new File( file.getParentFile(), file.getName() + "_new" );
newTrafficDos = new DataOutputStream( new BufferedOutputStream( new FileOutputStream( newTrafficFile ) ) );
newTrafficFile = new File(file.getParentFile(), file.getName() + "_new");
newTrafficDos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(newTrafficFile)));
}
public void finish() throws Exception
{
if ( newTrafficDos != null )
{
public void finish() throws Exception {
if (newTrafficDos != null) {
newTrafficDos.close();
newTrafficDos = null;
oldTrafficFile.delete();
newTrafficFile.renameTo( oldTrafficFile );
System.out.println( "TrafficMap: changes total=" + totalChanges + " supressed=" + supressedChanges );
newTrafficFile.renameTo(oldTrafficFile);
System.out.println("TrafficMap: changes total=" + totalChanges + " supressed=" + supressedChanges);
}
}
public void load( File file, int minLon, int minLat, int maxLon, int maxLat, boolean includeMotorways ) throws Exception
{
public void load(File file, int minLon, int minLat, int maxLon, int maxLat, boolean includeMotorways) throws Exception {
this.minLon = minLon;
this.minLat = minLat;
this.maxLon = maxLon;
this.maxLat = maxLat;
int trafficElements = 0;
DataInputStream is = new DataInputStream( new BufferedInputStream( new FileInputStream( file ) ) );
try
{
for(;;)
{
long n1 = is.readLong();
long n2 = is.readLong();
int traffic = is.readInt();
if ( traffic == -1 && !includeMotorways )
{
continue;
}
if ( isInsideBounds( n1 ) || isInsideBounds( n2 ) )
{
if ( addElement( n1, n2, traffic ) )
{
trafficElements++;
}
int trafficElements = 0;
DataInputStream is = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
try {
for (; ; ) {
long n1 = is.readLong();
long n2 = is.readLong();
int traffic = is.readInt();
if (traffic == -1 && !includeMotorways) {
continue;
}
if (isInsideBounds(n1) || isInsideBounds(n2)) {
if (addElement(n1, n2, traffic)) {
trafficElements++;
}
}
}
catch( EOFException eof ) {}
finally{ is.close(); }
map = new FrozenLongMap<OsmTrafficElement>( map );
System.out.println( "read traffic-elements: " + trafficElements );
} catch (EOFException eof) {
} finally {
is.close();
}
map = new FrozenLongMap<OsmTrafficElement>(map);
System.out.println("read traffic-elements: " + trafficElements);
}
public boolean addElement( long n1, long n2, int traffic )
{
OsmTrafficElement e = getElement( n1, n2 );
if ( e == null )
{
public boolean addElement(long n1, long n2, int traffic) {
OsmTrafficElement e = getElement(n1, n2);
if (e == null) {
e = new OsmTrafficElement();
e.node2 = n2;
e.traffic = traffic;
OsmTrafficElement e0 = map.get( n1 );
if ( e0 != null )
{
while( e0.next != null )
{
OsmTrafficElement e0 = map.get(n1);
if (e0 != null) {
while (e0.next != null) {
e0 = e0.next;
}
e0.next = e;
}
else
{
map.fastPut( n1, e );
} else {
map.fastPut(n1, e);
}
return true;
}
if ( doNotAdd )
{
e.traffic = Math.max( e.traffic, traffic );
}
else
{
if (doNotAdd) {
e.traffic = Math.max(e.traffic, traffic);
} else {
e.traffic = e.traffic == -1 || traffic == -1 ? -1 : e.traffic + traffic;
}
return false;
}
private boolean isInsideBounds( long id )
{
int ilon = (int)(id >> 32);
int ilat = (int)(id & 0xffffffff);
private boolean isInsideBounds(long id) {
int ilon = (int) (id >> 32);
int ilat = (int) (id & 0xffffffff);
return ilon >= minLon && ilon < maxLon && ilat >= minLat && ilat < maxLat;
}
public int getTrafficClass( long n1, long n2 )
{
public int getTrafficClass(long n1, long n2) {
// used for the old data, where we stpre traffic-classes, not volumes
OsmTrafficElement e = getElement( n1, n2 );
OsmTrafficElement e = getElement(n1, n2);
return e == null ? 0 : e.traffic;
}
public int getTrafficClassForTraffic( int traffic )
{
if ( traffic < 0 ) return -1;
if ( traffic < 40000 ) return 0;
if ( traffic < 80000 ) return 2;
if ( traffic < 160000 ) return 3;
if ( traffic < 320000 ) return 4;
if ( traffic < 640000 ) return 5;
if ( traffic <1280000 ) return 6;
public int getTrafficClassForTraffic(int traffic) {
if (traffic < 0) return -1;
if (traffic < 40000) return 0;
if (traffic < 80000) return 2;
if (traffic < 160000) return 3;
if (traffic < 320000) return 4;
if (traffic < 640000) return 5;
if (traffic < 1280000) return 6;
return 7;
}
private int getTraffic( long n1, long n2 )
{
OsmTrafficElement e1 = getElement( n1, n2 );
private int getTraffic(long n1, long n2) {
OsmTrafficElement e1 = getElement(n1, n2);
int traffic1 = e1 == null ? 0 : e1.traffic;
OsmTrafficElement e2 = getElement( n2, n1 );
OsmTrafficElement e2 = getElement(n2, n1);
int traffic2 = e2 == null ? 0 : e2.traffic;
return traffic1 == -1 || traffic2 == -1 ? -1 : traffic1 > traffic2 ? traffic1 : traffic2;
}
public void freeze()
{
public void freeze() {
}
private OsmTrafficElement getElement( long n1, long n2 )
{
OsmTrafficElement e = map.get( n1 );
while( e != null )
{
if ( e.node2 == n2 )
{
private OsmTrafficElement getElement(long n1, long n2) {
OsmTrafficElement e = map.get(n1);
while (e != null) {
if (e.node2 == n2) {
return e;
}
e = e.next;
}
e = e.next;
}
return null;
}
public OsmTrafficElement getElement( long n )
{
return map.get( n );
public OsmTrafficElement getElement(long n) {
return map.get(n);
}
public byte[] addTrafficClass( ArrayList<OsmNodeP> linkNodes, byte[] description ) throws IOException
{
public byte[] addTrafficClass(ArrayList<OsmNodeP> linkNodes, byte[] description) throws IOException {
double distance = 0.;
double sum = 0.;
for( int i=0; i<linkNodes.size()-1; i++ )
{
for (int i = 0; i < linkNodes.size() - 1; i++) {
OsmNodeP n1 = linkNodes.get(i);
OsmNodeP n2 = linkNodes.get(i+1);
int traffic = getTraffic( n1.getIdFromPos(), n2.getIdFromPos() );
double dist = CheapRuler.distance( n1.ilon, n1.ilat, n2.ilon, n2.ilat );
OsmNodeP n2 = linkNodes.get(i + 1);
int traffic = getTraffic(n1.getIdFromPos(), n2.getIdFromPos());
double dist = CheapRuler.distance(n1.ilon, n1.ilat, n2.ilon, n2.ilat);
distance += dist;
sum += dist*traffic;
sum += dist * traffic;
}
if ( distance == 0. )
{
if (distance == 0.) {
return description;
}
int traffic = (int)(sum/distance + 0.5);
int traffic = (int) (sum / distance + 0.5);
long id0 = linkNodes.get(0).getIdFromPos();
long id1 = linkNodes.get(linkNodes.size()-1).getIdFromPos();
int trafficClass = getTrafficClassForTraffic( traffic );
long id1 = linkNodes.get(linkNodes.size() - 1).getIdFromPos();
int trafficClass = getTrafficClassForTraffic(traffic);
// delta suppression: keep old traffic classes within some buffer range
if ( oldTrafficClasses != null )
{
int oldTrafficClass = oldTrafficClasses.getTrafficClass( id0, id1 );
if ( oldTrafficClass != trafficClass )
{
if (oldTrafficClasses != null) {
int oldTrafficClass = oldTrafficClasses.getTrafficClass(id0, id1);
if (oldTrafficClass != trafficClass) {
totalChanges++;
boolean supressChange =
oldTrafficClass == getTrafficClassForTraffic( (int)(traffic*1.3) )
|| oldTrafficClass == getTrafficClassForTraffic( (int)(traffic*0.77) );
oldTrafficClass == getTrafficClassForTraffic((int) (traffic * 1.3))
|| oldTrafficClass == getTrafficClassForTraffic((int) (traffic * 0.77));
if ( debug )
{
System.out.println( "traffic class change " + oldTrafficClass + "->" + trafficClass + " supress=" + supressChange );
if (debug) {
System.out.println("traffic class change " + oldTrafficClass + "->" + trafficClass + " supress=" + supressChange);
}
if ( supressChange )
{
if (supressChange) {
trafficClass = oldTrafficClass;
supressedChanges++;
}
}
}
if ( trafficClass > 0 )
{
newTrafficDos.writeLong( id0 );
newTrafficDos.writeLong( id1 );
newTrafficDos.writeInt( trafficClass );
expctxWay.decode( description );
expctxWay.addLookupValue( "estimated_traffic_class", trafficClass + 1 );
if (trafficClass > 0) {
newTrafficDos.writeLong(id0);
newTrafficDos.writeLong(id1);
newTrafficDos.writeInt(trafficClass);
expctxWay.decode(description);
expctxWay.addLookupValue("estimated_traffic_class", trafficClass + 1);
return expctxWay.encode();
}
return description;

View file

@ -1,229 +1,194 @@
package btools.mapcreator;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import btools.util.CompactLongSet;
import btools.util.DiffCoderDataOutputStream;
import btools.util.FrozenLongSet;
/**
* PosUnifier does 3 steps in map-processing:
*
* - unify positions - add srtm elevation data - make a bordernodes file
* containing net data from the bordernids-file just containing ids
*
* @author ab
*/
public class PosUnifier extends MapCreatorBase
{
private DiffCoderDataOutputStream nodesOutStream;
private DiffCoderDataOutputStream borderNodesOut;
private File nodeTilesOut;
private CompactLongSet[] positionSets;
private HashMap<String, SrtmRaster> srtmmap;
private int lastSrtmLonIdx;
private int lastSrtmLatIdx;
private SrtmRaster lastSrtmRaster;
private String srtmdir;
private CompactLongSet borderNids;
public static void main( String[] args ) throws Exception
{
System.out.println( "*** PosUnifier: Unify position values and enhance elevation" );
if ( args.length != 5 )
{
System.out.println( "usage: java PosUnifier <node-tiles-in> <node-tiles-out> <bordernids-in> <bordernodes-out> <srtm-data-dir>" );
return;
}
new PosUnifier().process( new File( args[0] ), new File( args[1] ), new File( args[2] ), new File( args[3] ), args[4] );
}
public void process( File nodeTilesIn, File nodeTilesOut, File bordernidsinfile, File bordernodesoutfile, String srtmdir ) throws Exception
{
this.nodeTilesOut = nodeTilesOut;
this.srtmdir = srtmdir;
// read border nids set
DataInputStream dis = createInStream( bordernidsinfile );
borderNids = new CompactLongSet();
try
{
for ( ;; )
{
long nid = readId( dis );
if ( !borderNids.contains( nid ) )
borderNids.fastAdd( nid );
}
}
catch (EOFException eof)
{
dis.close();
}
borderNids = new FrozenLongSet( borderNids );
// process all files
borderNodesOut = createOutStream( bordernodesoutfile );
new NodeIterator( this, true ).processDir( nodeTilesIn, ".n5d" );
borderNodesOut.close();
}
@Override
public void nodeFileStart( File nodefile ) throws Exception
{
resetSrtm();
nodesOutStream = createOutStream( fileFromTemplate( nodefile, nodeTilesOut, "u5d" ) );
positionSets = new CompactLongSet[2500];
}
@Override
public void nextNode( NodeData n ) throws Exception
{
SrtmRaster srtm = srtmForNode( n.ilon, n.ilat );
n.selev = srtm == null ? Short.MIN_VALUE : srtm.getElevation( n.ilon, n.ilat );
findUniquePos( n );
n.writeTo( nodesOutStream );
if ( borderNids.contains( n.nid ) )
{
n.writeTo( borderNodesOut );
}
}
@Override
public void nodeFileEnd( File nodeFile ) throws Exception
{
nodesOutStream.close();
}
private boolean checkAdd( int lon, int lat )
{
int slot = ((lon%5000000)/100000)*50 + ((lat%5000000)/100000);
long id = ( (long) lon ) << 32 | lat;
CompactLongSet set = positionSets[slot];
if ( set == null )
{
positionSets[slot] = set = new CompactLongSet();
}
if ( !set.contains( id ) )
{
set.fastAdd( id );
return true;
}
return false;
}
private void findUniquePos( NodeData n )
{
if ( !checkAdd( n.ilon, n.ilat ) )
{
_findUniquePos( n );
}
}
private void _findUniquePos( NodeData n )
{
// fix the position for uniqueness
int lonmod = n.ilon % 1000000;
int londelta = lonmod < 500000 ? 1 : -1;
int latmod = n.ilat % 1000000;
int latdelta = latmod < 500000 ? 1 : -1;
for ( int latsteps = 0; latsteps < 100; latsteps++ )
{
for ( int lonsteps = 0; lonsteps <= latsteps; lonsteps++ )
{
int lon = n.ilon + lonsteps * londelta;
int lat = n.ilat + latsteps * latdelta;
if ( checkAdd( lon, lat ) )
{
n.ilon = lon;
n.ilat = lat;
return;
}
}
}
System.out.println( "*** WARNING: cannot unify position for: " + n.ilon + " " + n.ilat );
}
/**
* get the srtm data set for a position srtm coords are
* srtm_<srtmLon>_<srtmLat> where srtmLon = 180 + lon, srtmLat = 60 - lat
*/
private SrtmRaster srtmForNode( int ilon, int ilat ) throws Exception
{
int srtmLonIdx = ( ilon + 5000000 ) / 5000000;
int srtmLatIdx = ( 654999999 - ilat ) / 5000000 - 100; // ugly negative rounding...
if ( srtmLonIdx == lastSrtmLonIdx && srtmLatIdx == lastSrtmLatIdx )
{
return lastSrtmRaster;
}
lastSrtmLonIdx = srtmLonIdx;
lastSrtmLatIdx = srtmLatIdx;
String slonidx = "0" + srtmLonIdx;
String slatidx = "0" + srtmLatIdx;
String filename = "srtm_" + slonidx.substring( slonidx.length()-2 ) + "_" + slatidx.substring( slatidx.length()-2 );
lastSrtmRaster = srtmmap.get( filename );
if ( lastSrtmRaster == null && !srtmmap.containsKey( filename ) )
{
File f = new File( new File( srtmdir ), filename + ".bef" );
System.out.println( "checking: " + f + " ilon=" + ilon + " ilat=" + ilat );
if ( f.exists() )
{
System.out.println( "*** reading: " + f );
try
{
InputStream isc = new BufferedInputStream( new FileInputStream( f ) );
lastSrtmRaster = new RasterCoder().decodeRaster( isc );
isc.close();
}
catch (Exception e)
{
System.out.println( "**** ERROR reading " + f + " ****" );
}
srtmmap.put( filename, lastSrtmRaster );
return lastSrtmRaster;
}
f = new File( new File( srtmdir ), filename + ".zip" );
System.out.println( "reading: " + f + " ilon=" + ilon + " ilat=" + ilat );
if ( f.exists() )
{
try
{
lastSrtmRaster = new SrtmData( f ).getRaster();
}
catch (Exception e)
{
System.out.println( "**** ERROR reading " + f + " ****" );
}
}
srtmmap.put( filename, lastSrtmRaster );
}
return lastSrtmRaster;
}
private void resetSrtm()
{
srtmmap = new HashMap<String, SrtmRaster>();
lastSrtmLonIdx = -1;
lastSrtmLatIdx = -1;
lastSrtmRaster = null;
}
}
package btools.mapcreator;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import btools.util.CompactLongSet;
import btools.util.DiffCoderDataOutputStream;
import btools.util.FrozenLongSet;
/**
* PosUnifier does 3 steps in map-processing:
* <p>
* - unify positions - add srtm elevation data - make a bordernodes file
* containing net data from the bordernids-file just containing ids
*
* @author ab
*/
public class PosUnifier extends MapCreatorBase {
private DiffCoderDataOutputStream nodesOutStream;
private DiffCoderDataOutputStream borderNodesOut;
private File nodeTilesOut;
private CompactLongSet[] positionSets;
private HashMap<String, SrtmRaster> srtmmap;
private int lastSrtmLonIdx;
private int lastSrtmLatIdx;
private SrtmRaster lastSrtmRaster;
private String srtmdir;
private CompactLongSet borderNids;
public static void main(String[] args) throws Exception {
System.out.println("*** PosUnifier: Unify position values and enhance elevation");
if (args.length != 5) {
System.out.println("usage: java PosUnifier <node-tiles-in> <node-tiles-out> <bordernids-in> <bordernodes-out> <srtm-data-dir>");
return;
}
new PosUnifier().process(new File(args[0]), new File(args[1]), new File(args[2]), new File(args[3]), args[4]);
}
public void process(File nodeTilesIn, File nodeTilesOut, File bordernidsinfile, File bordernodesoutfile, String srtmdir) throws Exception {
this.nodeTilesOut = nodeTilesOut;
this.srtmdir = srtmdir;
// read border nids set
DataInputStream dis = createInStream(bordernidsinfile);
borderNids = new CompactLongSet();
try {
for (; ; ) {
long nid = readId(dis);
if (!borderNids.contains(nid))
borderNids.fastAdd(nid);
}
} catch (EOFException eof) {
dis.close();
}
borderNids = new FrozenLongSet(borderNids);
// process all files
borderNodesOut = createOutStream(bordernodesoutfile);
new NodeIterator(this, true).processDir(nodeTilesIn, ".n5d");
borderNodesOut.close();
}
@Override
public void nodeFileStart(File nodefile) throws Exception {
resetSrtm();
nodesOutStream = createOutStream(fileFromTemplate(nodefile, nodeTilesOut, "u5d"));
positionSets = new CompactLongSet[2500];
}
@Override
public void nextNode(NodeData n) throws Exception {
SrtmRaster srtm = srtmForNode(n.ilon, n.ilat);
n.selev = srtm == null ? Short.MIN_VALUE : srtm.getElevation(n.ilon, n.ilat);
findUniquePos(n);
n.writeTo(nodesOutStream);
if (borderNids.contains(n.nid)) {
n.writeTo(borderNodesOut);
}
}
@Override
public void nodeFileEnd(File nodeFile) throws Exception {
nodesOutStream.close();
}
private boolean checkAdd(int lon, int lat) {
int slot = ((lon % 5000000) / 100000) * 50 + ((lat % 5000000) / 100000);
long id = ((long) lon) << 32 | lat;
CompactLongSet set = positionSets[slot];
if (set == null) {
positionSets[slot] = set = new CompactLongSet();
}
if (!set.contains(id)) {
set.fastAdd(id);
return true;
}
return false;
}
private void findUniquePos(NodeData n) {
if (!checkAdd(n.ilon, n.ilat)) {
_findUniquePos(n);
}
}
private void _findUniquePos(NodeData n) {
// fix the position for uniqueness
int lonmod = n.ilon % 1000000;
int londelta = lonmod < 500000 ? 1 : -1;
int latmod = n.ilat % 1000000;
int latdelta = latmod < 500000 ? 1 : -1;
for (int latsteps = 0; latsteps < 100; latsteps++) {
for (int lonsteps = 0; lonsteps <= latsteps; lonsteps++) {
int lon = n.ilon + lonsteps * londelta;
int lat = n.ilat + latsteps * latdelta;
if (checkAdd(lon, lat)) {
n.ilon = lon;
n.ilat = lat;
return;
}
}
}
System.out.println("*** WARNING: cannot unify position for: " + n.ilon + " " + n.ilat);
}
/**
* get the srtm data set for a position srtm coords are
* srtm_<srtmLon>_<srtmLat> where srtmLon = 180 + lon, srtmLat = 60 - lat
*/
private SrtmRaster srtmForNode(int ilon, int ilat) throws Exception {
int srtmLonIdx = (ilon + 5000000) / 5000000;
int srtmLatIdx = (654999999 - ilat) / 5000000 - 100; // ugly negative rounding...
if (srtmLonIdx == lastSrtmLonIdx && srtmLatIdx == lastSrtmLatIdx) {
return lastSrtmRaster;
}
lastSrtmLonIdx = srtmLonIdx;
lastSrtmLatIdx = srtmLatIdx;
String slonidx = "0" + srtmLonIdx;
String slatidx = "0" + srtmLatIdx;
String filename = "srtm_" + slonidx.substring(slonidx.length() - 2) + "_" + slatidx.substring(slatidx.length() - 2);
lastSrtmRaster = srtmmap.get(filename);
if (lastSrtmRaster == null && !srtmmap.containsKey(filename)) {
File f = new File(new File(srtmdir), filename + ".bef");
System.out.println("checking: " + f + " ilon=" + ilon + " ilat=" + ilat);
if (f.exists()) {
System.out.println("*** reading: " + f);
try {
InputStream isc = new BufferedInputStream(new FileInputStream(f));
lastSrtmRaster = new RasterCoder().decodeRaster(isc);
isc.close();
} catch (Exception e) {
System.out.println("**** ERROR reading " + f + " ****");
}
srtmmap.put(filename, lastSrtmRaster);
return lastSrtmRaster;
}
f = new File(new File(srtmdir), filename + ".zip");
System.out.println("reading: " + f + " ilon=" + ilon + " ilat=" + ilat);
if (f.exists()) {
try {
lastSrtmRaster = new SrtmData(f).getRaster();
} catch (Exception e) {
System.out.println("**** ERROR reading " + f + " ****");
}
}
srtmmap.put(filename, lastSrtmRaster);
}
return lastSrtmRaster;
}
private void resetSrtm() {
srtmmap = new HashMap<String, SrtmRaster>();
lastSrtmLonIdx = -1;
lastSrtmLatIdx = -1;
lastSrtmRaster = null;
}
}

View file

@ -1,16 +1,15 @@
package btools.mapcreator;
import java.io.*;
import btools.util.*;
//
// Encode/decode a raster
//
public class RasterCoder
{
public void encodeRaster(SrtmRaster raster, OutputStream os) throws IOException
{
public class RasterCoder {
public void encodeRaster(SrtmRaster raster, OutputStream os) throws IOException {
DataOutputStream dos = new DataOutputStream(os);
long t0 = System.currentTimeMillis();
@ -29,8 +28,7 @@ public class RasterCoder
System.out.println("finished encoding in " + (t1 - t0) + " ms");
}
public SrtmRaster decodeRaster(InputStream is) throws IOException
{
public SrtmRaster decodeRaster(InputStream is) throws IOException {
DataInputStream dis = new DataInputStream(is);
long t0 = System.currentTimeMillis();
@ -46,78 +44,65 @@ public class RasterCoder
raster.eval_array = new short[raster.ncols * raster.nrows];
_decodeRaster(raster, is);
raster.usingWeights = raster.ncols > 6001;
long t1 = System.currentTimeMillis();
System.out.println("finished decoding in " + (t1 - t0) + " ms ncols=" + raster.ncols + " nrows=" + raster.nrows );
System.out.println("finished decoding in " + (t1 - t0) + " ms ncols=" + raster.ncols + " nrows=" + raster.nrows);
return raster;
}
private void _encodeRaster(SrtmRaster raster, OutputStream os) throws IOException
{
private void _encodeRaster(SrtmRaster raster, OutputStream os) throws IOException {
MixCoderDataOutputStream mco = new MixCoderDataOutputStream(os);
int nrows = raster.nrows;
int ncols = raster.ncols;
short[] pixels = raster.eval_array;
int colstep = raster.halfcol ? 2 : 1;
for (int row = 0; row < nrows; row++)
{
for (int row = 0; row < nrows; row++) {
short lastval = Short.MIN_VALUE; // nodata
for (int col = 0; col < ncols; col += colstep )
{
for (int col = 0; col < ncols; col += colstep) {
short val = pixels[row * ncols + col];
if ( val == -32766 )
{
if (val == -32766) {
val = lastval; // replace remaining (border) skips
}
else
{
} else {
lastval = val;
}
// remap nodata
int code = val == Short.MIN_VALUE ? -1 : ( val < 0 ? val-1 : val );
mco.writeMixed( code );
int code = val == Short.MIN_VALUE ? -1 : (val < 0 ? val - 1 : val);
mco.writeMixed(code);
}
}
mco.flush();
}
private void _decodeRaster(SrtmRaster raster, InputStream is) throws IOException
{
private void _decodeRaster(SrtmRaster raster, InputStream is) throws IOException {
MixCoderDataInputStream mci = new MixCoderDataInputStream(is);
int nrows = raster.nrows;
int ncols = raster.ncols;
short[] pixels = raster.eval_array;
int colstep = raster.halfcol ? 2 : 1;
for (int row = 0; row < nrows; row++)
{
for (int col = 0; col < ncols; col += colstep )
{
for (int row = 0; row < nrows; row++) {
for (int col = 0; col < ncols; col += colstep) {
int code = mci.readMixed();
// remap nodata
int v30 = code == -1 ? Short.MIN_VALUE : ( code < 0 ? code + 1 : code );
if ( raster.usingWeights && v30 > -32766 )
{
int v30 = code == -1 ? Short.MIN_VALUE : (code < 0 ? code + 1 : code);
if (raster.usingWeights && v30 > -32766) {
v30 *= 2;
}
pixels[row * ncols + col] = (short) ( v30 );
}
pixels[row * ncols + col] = (short) (v30);
}
if ( raster.halfcol )
{
for (int col = 1; col < ncols-1; col += colstep )
{
int l = (int)pixels[row * ncols + col - 1];
int r = (int)pixels[row * ncols + col + 1];
if (raster.halfcol) {
for (int col = 1; col < ncols - 1; col += colstep) {
int l = (int) pixels[row * ncols + col - 1];
int r = (int) pixels[row * ncols + col + 1];
short v30 = Short.MIN_VALUE; // nodata
if ( l > -32766 && r > -32766 )
{
v30 = (short)((l+r)/2);
if (l > -32766 && r > -32766) {
v30 = (short) ((l + r) / 2);
}
pixels[row * ncols + col] = v30;
}

View file

@ -1,27 +1,24 @@
package btools.mapcreator;
import btools.util.LongList;
/**
* Container for relation data on the preprocessor level
*
* @author ab
*/
public class RelationData extends MapCreatorBase
{
public long rid;
public long description;
public LongList ways;
public RelationData( long id )
{
rid = id;
ways = new LongList( 16 );
}
public RelationData( long id, LongList ways )
{
rid = id;
this.ways = ways;
}
}
package btools.mapcreator;
import btools.util.LongList;
/**
* Container for relation data on the preprocessor level
*
* @author ab
*/
public class RelationData extends MapCreatorBase {
public long rid;
public long description;
public LongList ways;
public RelationData(long id) {
rid = id;
ways = new LongList(16);
}
public RelationData(long id, LongList ways) {
rid = id;
this.ways = ways;
}
}

View file

@ -1,14 +1,13 @@
package btools.mapcreator;
/**
* Callbacklistener for Relations
*
* @author ab
*/
public interface RelationListener
{
void nextRelation( RelationData data ) throws Exception;
void nextRestriction( RelationData data, long fromWid, long toWid, long viaNid ) throws Exception;
}
package btools.mapcreator;
/**
* Callbacklistener for Relations
*
* @author ab
*/
public interface RelationListener {
void nextRelation(RelationData data) throws Exception;
void nextRestriction(RelationData data, long fromWid, long toWid, long viaNid) throws Exception;
}

View file

@ -13,152 +13,131 @@ import btools.util.FrozenLongSet;
/**
* RelationMerger does 1 step in map processing:
*
* <p>
* - enrich ways with relation information
*
* @author ab
*/
public class RelationMerger extends MapCreatorBase
{
private HashMap<String,CompactLongSet> routesets;
public class RelationMerger extends MapCreatorBase {
private HashMap<String, CompactLongSet> routesets;
private CompactLongSet routesetall;
private BExpressionContextWay expctxReport;
private BExpressionContextWay expctxCheck;
// private BExpressionContext expctxStat;
// private BExpressionContext expctxStat;
private DataOutputStream wayOutStream;
public static void main(String[] args) throws Exception
{
System.out.println("*** RelationMerger: merge relations into ways" );
if (args.length != 6)
{
System.out.println("usage: java RelationMerger <way-file-in> <way-file-out> <relation-file> <lookup-file> <report-profile> <check-profile>" );
public static void main(String[] args) throws Exception {
System.out.println("*** RelationMerger: merge relations into ways");
if (args.length != 6) {
System.out.println("usage: java RelationMerger <way-file-in> <way-file-out> <relation-file> <lookup-file> <report-profile> <check-profile>");
return;
}
new RelationMerger().process( new File( args[0] ), new File( args[1] ), new File( args[2] ), new File( args[3] ), new File( args[4] ), new File( args[5] ) );
new RelationMerger().process(new File(args[0]), new File(args[1]), new File(args[2]), new File(args[3]), new File(args[4]), new File(args[5]));
}
public void init( File relationFileIn, File lookupFile, File reportProfile, File checkProfile ) throws Exception
{
public void init(File relationFileIn, File lookupFile, File reportProfile, File checkProfile) throws Exception {
// read lookup + profile for relation access-check
BExpressionMetaData metaReport = new BExpressionMetaData();
expctxReport = new BExpressionContextWay( metaReport );
metaReport.readMetaData( lookupFile );
BExpressionMetaData metaReport = new BExpressionMetaData();
expctxReport = new BExpressionContextWay(metaReport);
metaReport.readMetaData(lookupFile);
BExpressionMetaData metaCheck = new BExpressionMetaData();
expctxCheck = new BExpressionContextWay( metaCheck );
metaCheck.readMetaData( lookupFile );
BExpressionMetaData metaCheck = new BExpressionMetaData();
expctxCheck = new BExpressionContextWay(metaCheck);
metaCheck.readMetaData(lookupFile);
expctxReport.parseFile( reportProfile, "global" );
expctxCheck.parseFile( checkProfile, "global" );
expctxReport.parseFile(reportProfile, "global");
expctxCheck.parseFile(checkProfile, "global");
// expctxStat = new BExpressionContext("way");
// *** read the relation file into sets for each processed tag
routesets = new HashMap<String,CompactLongSet>();
routesets = new HashMap<String, CompactLongSet>();
routesetall = new CompactLongSet();
DataInputStream dis = createInStream( relationFileIn );
try
{
for(;;)
{
long rid = readId( dis );
DataInputStream dis = createInStream(relationFileIn);
try {
for (; ; ) {
long rid = readId(dis);
String route = dis.readUTF();
String network = dis.readUTF();
String state = dis.readUTF();
int value = "proposed".equals( state ) ? 3 : 2; // 2=yes, 3=proposed
int value = "proposed".equals(state) ? 3 : 2; // 2=yes, 3=proposed
String tagname = "route_" + route + "_" + network;
CompactLongSet routeset = null;
if ( expctxCheck.getLookupNameIdx(tagname) >= 0 )
{
if (expctxCheck.getLookupNameIdx(tagname) >= 0) {
String key = tagname + "_" + value;
routeset = routesets.get( key );
if ( routeset == null )
{
routeset = routesets.get(key);
if (routeset == null) {
routeset = new CompactLongSet();
routesets.put( key, routeset );
routesets.put(key, routeset);
}
}
for(;;)
{
long wid = readId( dis );
if ( wid == -1 ) break;
for (; ; ) {
long wid = readId(dis);
if (wid == -1) break;
// expctxStat.addLookupValue( tagname, "yes", null );
if ( routeset != null && !routeset.contains( wid ) )
{
routeset.add( wid );
routesetall.add( wid );
if (routeset != null && !routeset.contains(wid)) {
routeset.add(wid);
routesetall.add(wid);
}
}
}
}
catch( EOFException eof )
{
} catch (EOFException eof) {
dis.close();
}
for( String key : routesets.keySet() )
{
CompactLongSet routeset = new FrozenLongSet( routesets.get( key ) );
routesets.put( key, routeset );
System.out.println( "marked " + routeset.size() + " routes for key: " + key );
for (String key : routesets.keySet()) {
CompactLongSet routeset = new FrozenLongSet(routesets.get(key));
routesets.put(key, routeset);
System.out.println("marked " + routeset.size() + " routes for key: " + key);
}
}
public void process( File wayFileIn, File wayFileOut, File relationFileIn, File lookupFile, File reportProfile, File checkProfile ) throws Exception
{
init( relationFileIn, lookupFile, reportProfile, checkProfile );
public void process(File wayFileIn, File wayFileOut, File relationFileIn, File lookupFile, File reportProfile, File checkProfile) throws Exception {
init(relationFileIn, lookupFile, reportProfile, checkProfile);
// *** finally process the way-file
wayOutStream = createOutStream( wayFileOut );
new WayIterator( this, true ).processFile( wayFileIn );
wayOutStream = createOutStream(wayFileOut);
new WayIterator(this, true).processFile(wayFileIn);
wayOutStream.close();
// System.out.println( "-------- route-statistics -------- " );
// expctxStat.dumpStatistics();
}
}
@Override
public void nextWay( WayData data ) throws Exception
{
public void nextWay(WayData data) throws Exception {
// propagate the route-bits
if ( routesetall.contains( data.wid ) )
{
if (routesetall.contains(data.wid)) {
boolean ok = true;
// check access and log a warning for conflicts
expctxReport.evaluate( false, data.description );
expctxReport.evaluate(false, data.description);
boolean warn = expctxReport.getCostfactor() >= 10000.;
if ( warn )
{
expctxCheck.evaluate( false, data.description );
if (warn) {
expctxCheck.evaluate(false, data.description);
ok = expctxCheck.getCostfactor() < 10000.;
System.out.println( "** relation access conflict for wid = " + data.wid + " tags:" + expctxReport.getKeyValueDescription( false, data.description ) + " (ok=" + ok + ")" );
System.out.println("** relation access conflict for wid = " + data.wid + " tags:" + expctxReport.getKeyValueDescription(false, data.description) + " (ok=" + ok + ")");
}
if ( ok )
{
expctxReport.decode( data.description );
for( String key : routesets.keySet() )
{
CompactLongSet routeset = routesets.get( key );
if ( routeset.contains( data.wid ) )
{
int sepIdx = key.lastIndexOf( '_' );
String tagname = key.substring( 0, sepIdx );
int val = Integer.valueOf( key.substring( sepIdx+1 ) );
expctxReport.addSmallestLookupValue( tagname, val );
if (ok) {
expctxReport.decode(data.description);
for (String key : routesets.keySet()) {
CompactLongSet routeset = routesets.get(key);
if (routeset.contains(data.wid)) {
int sepIdx = key.lastIndexOf('_');
String tagname = key.substring(0, sepIdx);
int val = Integer.valueOf(key.substring(sepIdx + 1));
expctxReport.addSmallestLookupValue(tagname, val);
}
}
data.description = expctxReport.encode();
data.description = expctxReport.encode();
}
}
if ( wayOutStream != null )
{
data.writeTo( wayOutStream );
if (wayOutStream != null) {
data.writeTo(wayOutStream);
}
}

View file

@ -10,63 +10,52 @@ import btools.util.TinyDenseLongMap;
/**
* WayCutter does 2 step in map-processing:
*
* <p>
* - cut the way file into 45*30 - pieces
* - enrich ways with relation information
*
* @author ab
*/
public class RelationStatistics extends MapCreatorBase
{
public static void main(String[] args) throws Exception
{
public class RelationStatistics extends MapCreatorBase {
public static void main(String[] args) throws Exception {
System.out.println("*** RelationStatistics: count relation networks");
if (args.length != 1)
{
System.out.println("usage: java WayCutter <relation-file>" );
if (args.length != 1) {
System.out.println("usage: java WayCutter <relation-file>");
return;
}
new RelationStatistics().process( new File( args[0] ) );
new RelationStatistics().process(new File(args[0]));
}
public void process( File relationFileIn ) throws Exception
{
HashMap<String,long[]> relstats = new HashMap<String,long[]>();
DataInputStream dis = createInStream( relationFileIn );
try
{
for(;;)
{
long rid = readId( dis );
public void process(File relationFileIn) throws Exception {
HashMap<String, long[]> relstats = new HashMap<String, long[]>();
DataInputStream dis = createInStream(relationFileIn);
try {
for (; ; ) {
long rid = readId(dis);
String network = dis.readUTF();
int waycount = 0;
for(;;)
{
long wid = readId( dis );
if ( wid == -1 ) break;
for (; ; ) {
long wid = readId(dis);
if (wid == -1) break;
waycount++;
}
long[] stat = relstats.get( network );
if ( stat == null )
{
long[] stat = relstats.get(network);
if (stat == null) {
stat = new long[2];
relstats.put( network, stat );
relstats.put(network, stat);
}
stat[0]++;
stat[1] += waycount;
}
}
catch( EOFException eof )
{
} catch (EOFException eof) {
dis.close();
}
for( String network : relstats.keySet() )
{
long[] stat = relstats.get( network );
System.out.println( "network: " + network + " has " + stat[0] + " relations with " + stat[1] + " ways" );
for (String network : relstats.keySet()) {
long[] stat = relstats.get(network);
System.out.println("network: " + network + " has " + stat[0] + " relations with " + stat[1] + " ways");
}
}

View file

@ -4,41 +4,35 @@ import java.io.File;
/**
* RestrictionCutter writes Restrictions to tiles
*
* <p>
* - cut the way file into 45*30 - pieces
* - enrich ways with relation information
*
* @author ab
*/
public class RestrictionCutter extends MapCreatorBase
{
public class RestrictionCutter extends MapCreatorBase {
private WayCutter wayCutter;
public void init( File outTileDir, WayCutter wayCutter ) throws Exception
{
public void init(File outTileDir, WayCutter wayCutter) throws Exception {
outTileDir.mkdir();
this.outTileDir = outTileDir;
this.wayCutter = wayCutter;
}
public void finish() throws Exception
{
public void finish() throws Exception {
closeTileOutStreams();
}
public void nextRestriction( RestrictionData data ) throws Exception
{
int tileIndex = wayCutter.getTileIndexForNid( data.viaNid );
if ( tileIndex != -1 )
{
data.writeTo( getOutStreamForTile( tileIndex ) );
public void nextRestriction(RestrictionData data) throws Exception {
int tileIndex = wayCutter.getTileIndexForNid(data.viaNid);
if (tileIndex != -1) {
data.writeTo(getOutStreamForTile(tileIndex));
}
}
protected String getNameForTile( int tileIndex )
{
String name = wayCutter.getNameForTile( tileIndex );
return name.substring( 0, name.length()-3 ) + "rtl";
protected String getNameForTile(int tileIndex) {
String name = wayCutter.getNameForTile(tileIndex);
return name.substring(0, name.length() - 3) + "rtl";
}
}

View file

@ -4,40 +4,34 @@ import java.io.File;
/**
* RestrictionCutter5 does 1 step in map-processing:
*
* <p>
* - cut the 45*30 restriction files into 5*5 pieces
*
* @author ab
*/
public class RestrictionCutter5 extends MapCreatorBase
{
public class RestrictionCutter5 extends MapCreatorBase {
private WayCutter5 wayCutter5;
public void init( File outTileDir, WayCutter5 wayCutter5 ) throws Exception
{
public void init(File outTileDir, WayCutter5 wayCutter5) throws Exception {
outTileDir.mkdir();
this.outTileDir = outTileDir;
this.wayCutter5 = wayCutter5;
}
public void finish() throws Exception
{
public void finish() throws Exception {
closeTileOutStreams();
}
public void nextRestriction( RestrictionData data ) throws Exception
{
int tileIndex = wayCutter5.getTileIndexForNid( data.viaNid );
if ( tileIndex != -1 )
{
data.writeTo( getOutStreamForTile( tileIndex ) );
public void nextRestriction(RestrictionData data) throws Exception {
int tileIndex = wayCutter5.getTileIndexForNid(data.viaNid);
if (tileIndex != -1) {
data.writeTo(getOutStreamForTile(tileIndex));
}
}
protected String getNameForTile( int tileIndex )
{
String name = wayCutter5.getNameForTile( tileIndex );
return name.substring( 0, name.length()-3 ) + "rt5";
protected String getNameForTile(int tileIndex) {
String name = wayCutter5.getNameForTile(tileIndex);
return name.substring(0, name.length() - 3) + "rt5";
}
}

View file

@ -15,8 +15,7 @@ import btools.util.CheapAngleMeter;
*
* @author ab
*/
public class RestrictionData extends MapCreatorBase
{
public class RestrictionData extends MapCreatorBase {
public String restrictionKey;
public String restriction;
public short exceptions;
@ -30,125 +29,99 @@ public class RestrictionData extends MapCreatorBase
public int fromLon;
public int fromLat;
public int toLon;
public int toLat;
public boolean badWayMatch;
private static HashMap<String,String> names = new HashMap<>();
private static HashMap<String, String> names = new HashMap<>();
private static TreeSet<Long> badTRs = new TreeSet<>();
public RestrictionData()
{
}
public boolean isPositive()
{
return restriction.startsWith( "only_" );
public RestrictionData() {
}
public boolean isValid()
{
boolean valid = fromLon != 0 && toLon != 0 && ( restriction.startsWith( "only_" ) || restriction.startsWith( "no_" ) );
if ( (!valid) || badWayMatch || !(checkGeometry()) )
{
synchronized( badTRs )
{
badTRs.add( ( (long) viaLon ) << 32 | viaLat );
public boolean isPositive() {
return restriction.startsWith("only_");
}
public boolean isValid() {
boolean valid = fromLon != 0 && toLon != 0 && (restriction.startsWith("only_") || restriction.startsWith("no_"));
if ((!valid) || badWayMatch || !(checkGeometry())) {
synchronized (badTRs) {
badTRs.add(((long) viaLon) << 32 | viaLat);
}
}
return valid && "restriction".equals( restrictionKey );
return valid && "restriction".equals(restrictionKey);
}
private boolean checkGeometry()
{
double a = (new CheapAngleMeter()).calcAngle( fromLon, fromLat, viaLon, viaLat, toLon, toLat );
private boolean checkGeometry() {
double a = (new CheapAngleMeter()).calcAngle(fromLon, fromLat, viaLon, viaLat, toLon, toLat);
String t;
if ( restriction.startsWith( "only_" ) )
{
t = restriction.substring( "only_".length() );
}
else if ( restriction.startsWith( "no_" ) )
{
t = restriction.substring( "no_".length() );
}
else throw new RuntimeException( "ups" );
if (restriction.startsWith("only_")) {
t = restriction.substring("only_".length());
} else if (restriction.startsWith("no_")) {
t = restriction.substring("no_".length());
} else throw new RuntimeException("ups");
if ( restrictionKey.endsWith( ":conditional" ) )
{
int idx = t.indexOf( '@' );
if ( idx >= 0 )
{
t = t.substring(0, idx ).trim();
if (restrictionKey.endsWith(":conditional")) {
int idx = t.indexOf('@');
if (idx >= 0) {
t = t.substring(0, idx).trim();
}
}
if ( "left_turn".equals( t ) )
{
if ("left_turn".equals(t)) {
return a < -5. && a > -175.;
}
if ( "right_turn".equals( t ) )
{
if ("right_turn".equals(t)) {
return a > 5. && a < 175.;
}
if ( "straight_on".equals( t ) )
{
if ("straight_on".equals(t)) {
return a > -85. && a < 85.;
}
if ( "u_turn".equals( t ) )
{
return a < - 95. || a > 95.;
if ("u_turn".equals(t)) {
return a < -95. || a > 95.;
}
return "entry".equals( t ) || "exit".equals( t );
return "entry".equals(t) || "exit".equals(t);
}
private static String unifyName( String name )
{
synchronized( names )
{
private static String unifyName(String name) {
synchronized (names) {
String n = names.get(name);
if ( n == null )
{
names.put( name, name );
if (n == null) {
names.put(name, name);
n = name;
}
return n;
}
}
public static void dumpBadTRs()
{
try( BufferedWriter bw = new BufferedWriter( new FileWriter( "badtrs.txt" ) ) )
{
for( Long id : badTRs )
{
bw.write( "" + id + " 26\n" );
public static void dumpBadTRs() {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("badtrs.txt"))) {
for (Long id : badTRs) {
bw.write("" + id + " 26\n");
}
}
catch( IOException ioe )
{
throw new RuntimeException( ioe );
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
public RestrictionData( DataInputStream di ) throws Exception
{
restrictionKey = unifyName( di.readUTF() );
restriction = unifyName( di.readUTF() );
public RestrictionData(DataInputStream di) throws Exception {
restrictionKey = unifyName(di.readUTF());
restriction = unifyName(di.readUTF());
exceptions = di.readShort();
fromWid = readId( di );
toWid = readId( di );
viaNid = readId( di );
fromWid = readId(di);
toWid = readId(di);
viaNid = readId(di);
}
public void writeTo( DataOutputStream dos ) throws Exception
{
dos.writeUTF( restrictionKey );
dos.writeUTF( restriction );
dos.writeShort( exceptions );
writeId( dos, fromWid );
writeId( dos, toWid );
writeId( dos, viaNid );
public void writeTo(DataOutputStream dos) throws Exception {
dos.writeUTF(restrictionKey);
dos.writeUTF(restriction);
dos.writeShort(exceptions);
writeId(dos, fromWid);
writeId(dos, toWid);
writeId(dos, viaNid);
}
}

View file

@ -1,195 +1,166 @@
package btools.mapcreator;
/**
* This is a wrapper for a 5*5 degree srtm file in ascii/zip-format
*
* - filter out unused nodes according to the way file
* - enhance with SRTM elevation data
* - split further in smaller (5*5 degree) tiles
*
* @author ab
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.StringTokenizer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class SrtmData
{
private SrtmRaster raster;
public SrtmData( File file ) throws Exception
{
raster = new SrtmRaster();
ZipInputStream zis = new ZipInputStream( new BufferedInputStream( new FileInputStream( file ) ) );
try
{
for ( ;; )
{
ZipEntry ze = zis.getNextEntry();
if ( ze.getName().endsWith( ".asc" ) )
{
readFromStream( zis );
return;
}
}
}
finally
{
zis.close();
}
}
public SrtmRaster getRaster()
{
return raster;
}
private String secondToken( String s )
{
StringTokenizer tk = new StringTokenizer( s, " " );
tk.nextToken();
return tk.nextToken();
}
public void readFromStream( InputStream is ) throws Exception
{
BufferedReader br = new BufferedReader( new InputStreamReader( is ) );
int linenr = 0;
for ( ;; )
{
linenr++;
if ( linenr <= 6 )
{
String line = br.readLine();
if ( linenr == 1 )
raster.ncols = Integer.parseInt( secondToken( line ) );
else if ( linenr == 2 )
raster.nrows = Integer.parseInt( secondToken( line ) );
else if ( linenr == 3 )
raster.xllcorner = Double.parseDouble( secondToken( line ) );
else if ( linenr == 4 )
raster.yllcorner = Double.parseDouble( secondToken( line ) );
else if ( linenr == 5 )
raster.cellsize = Double.parseDouble( secondToken( line ) );
else if ( linenr == 6 )
{
// nodata ignored here ( < -250 assumed nodata... )
// raster.noDataValue = Short.parseShort( secondToken( line ) );
raster.eval_array = new short[raster.ncols * raster.nrows];
}
}
else
{
int row = 0;
int col = 0;
int n = 0;
boolean negative = false;
for ( ;; )
{
int c = br.read();
if ( c < 0 )
break;
if ( c == ' ' )
{
if ( negative )
n = -n;
short val = n < -250 ? Short.MIN_VALUE : (short) (n);
raster.eval_array[row * raster.ncols + col] = val;
if ( ++col == raster.ncols )
{
col = 0;
++row;
}
n = 0;
negative = false;
}
else if ( c >= '0' && c <= '9' )
{
n = 10 * n + ( c - '0' );
}
else if ( c == '-' )
{
negative = true;
}
}
break;
}
}
br.close();
}
public static void main( String[] args ) throws Exception
{
String fromDir = args[0];
String toDir = args[1];
File[] files = new File( fromDir ).listFiles();
for( File f : files )
{
if ( !f.getName().endsWith( ".zip" ) )
{
continue;
}
System.out.println( "*** reading: " + f );
long t0 = System.currentTimeMillis();
SrtmRaster raster = new SrtmData( f ).getRaster();
long t1 = System.currentTimeMillis();
String name = f.getName();
long zipTime = t1-t0;
File fbef = new File( new File( toDir ), name.substring( 0, name.length()-3 ) + "bef" );
System.out.println( "recoding: " + f + " to " + fbef );
OutputStream osbef = new BufferedOutputStream( new FileOutputStream( fbef ) );
new RasterCoder().encodeRaster( raster, osbef );
osbef.close();
System.out.println( "*** re-reading: " + fbef );
long t2 = System.currentTimeMillis();
InputStream isc = new BufferedInputStream( new FileInputStream( fbef ) );
SrtmRaster raster2 = new RasterCoder().decodeRaster( isc );
isc.close();
long t3 = System.currentTimeMillis();
long befTime = t3-t2;
System.out.println( "*** zip-time: " + zipTime + "*** bef-time: " + befTime );
String s1 = raster.toString();
String s2 = raster2.toString();
if ( !s1.equals( s2 ) )
{
throw new IllegalArgumentException( "missmatch: " + s1 + "<--->" + s2 );
}
int cols = raster.ncols;
int rows = raster.nrows;
for( int c = 0; c < cols; c++ )
{
for( int r = 0; r < rows; r++ )
{
int idx = r * cols + c;
if ( raster.eval_array[idx] != raster2.eval_array[idx] )
{
throw new IllegalArgumentException( "missmatch: at " + c + "," + r + ": " + raster.eval_array[idx] + "<--->" + raster2.eval_array[idx] );
}
}
}
}
}
}
package btools.mapcreator;
/**
* This is a wrapper for a 5*5 degree srtm file in ascii/zip-format
* <p>
* - filter out unused nodes according to the way file
* - enhance with SRTM elevation data
* - split further in smaller (5*5 degree) tiles
*
* @author ab
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.StringTokenizer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class SrtmData {
private SrtmRaster raster;
public SrtmData(File file) throws Exception {
raster = new SrtmRaster();
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(file)));
try {
for (; ; ) {
ZipEntry ze = zis.getNextEntry();
if (ze.getName().endsWith(".asc")) {
readFromStream(zis);
return;
}
}
} finally {
zis.close();
}
}
public SrtmRaster getRaster() {
return raster;
}
private String secondToken(String s) {
StringTokenizer tk = new StringTokenizer(s, " ");
tk.nextToken();
return tk.nextToken();
}
public void readFromStream(InputStream is) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
int linenr = 0;
for (; ; ) {
linenr++;
if (linenr <= 6) {
String line = br.readLine();
if (linenr == 1)
raster.ncols = Integer.parseInt(secondToken(line));
else if (linenr == 2)
raster.nrows = Integer.parseInt(secondToken(line));
else if (linenr == 3)
raster.xllcorner = Double.parseDouble(secondToken(line));
else if (linenr == 4)
raster.yllcorner = Double.parseDouble(secondToken(line));
else if (linenr == 5)
raster.cellsize = Double.parseDouble(secondToken(line));
else if (linenr == 6) {
// nodata ignored here ( < -250 assumed nodata... )
// raster.noDataValue = Short.parseShort( secondToken( line ) );
raster.eval_array = new short[raster.ncols * raster.nrows];
}
} else {
int row = 0;
int col = 0;
int n = 0;
boolean negative = false;
for (; ; ) {
int c = br.read();
if (c < 0)
break;
if (c == ' ') {
if (negative)
n = -n;
short val = n < -250 ? Short.MIN_VALUE : (short) (n);
raster.eval_array[row * raster.ncols + col] = val;
if (++col == raster.ncols) {
col = 0;
++row;
}
n = 0;
negative = false;
} else if (c >= '0' && c <= '9') {
n = 10 * n + (c - '0');
} else if (c == '-') {
negative = true;
}
}
break;
}
}
br.close();
}
public static void main(String[] args) throws Exception {
String fromDir = args[0];
String toDir = args[1];
File[] files = new File(fromDir).listFiles();
for (File f : files) {
if (!f.getName().endsWith(".zip")) {
continue;
}
System.out.println("*** reading: " + f);
long t0 = System.currentTimeMillis();
SrtmRaster raster = new SrtmData(f).getRaster();
long t1 = System.currentTimeMillis();
String name = f.getName();
long zipTime = t1 - t0;
File fbef = new File(new File(toDir), name.substring(0, name.length() - 3) + "bef");
System.out.println("recoding: " + f + " to " + fbef);
OutputStream osbef = new BufferedOutputStream(new FileOutputStream(fbef));
new RasterCoder().encodeRaster(raster, osbef);
osbef.close();
System.out.println("*** re-reading: " + fbef);
long t2 = System.currentTimeMillis();
InputStream isc = new BufferedInputStream(new FileInputStream(fbef));
SrtmRaster raster2 = new RasterCoder().decodeRaster(isc);
isc.close();
long t3 = System.currentTimeMillis();
long befTime = t3 - t2;
System.out.println("*** zip-time: " + zipTime + "*** bef-time: " + befTime);
String s1 = raster.toString();
String s2 = raster2.toString();
if (!s1.equals(s2)) {
throw new IllegalArgumentException("missmatch: " + s1 + "<--->" + s2);
}
int cols = raster.ncols;
int rows = raster.nrows;
for (int c = 0; c < cols; c++) {
for (int r = 0; r < rows; r++) {
int idx = r * cols + c;
if (raster.eval_array[idx] != raster2.eval_array[idx]) {
throw new IllegalArgumentException("missmatch: at " + c + "," + r + ": " + raster.eval_array[idx] + "<--->" + raster2.eval_array[idx]);
}
}
}
}
}
}

View file

@ -7,8 +7,7 @@ import btools.util.ReducedMedianFilter;
*
* @author ab
*/
public class SrtmRaster
{
public class SrtmRaster {
public int ncols;
public int nrows;
public boolean halfcol;
@ -22,99 +21,93 @@ public class SrtmRaster
private boolean missingData = false;
public short getElevation( int ilon, int ilat )
{
public short getElevation(int ilon, int ilat) {
double lon = ilon / 1000000. - 180.;
double lat = ilat / 1000000. - 90.;
if ( usingWeights )
{
return getElevationFromShiftWeights( lon, lat );
if (usingWeights) {
return getElevationFromShiftWeights(lon, lat);
}
// no weights calculated, use 2d linear interpolation
double dcol = (lon - xllcorner)/cellsize -0.5;
double drow = (lat - yllcorner)/cellsize -0.5;
int row = (int)drow;
int col = (int)dcol;
if ( col < 0 ) col = 0;
if ( col >= ncols-1 ) col = ncols - 2;
if ( row < 0 ) row = 0;
if ( row >= nrows-1 ) row = nrows - 2;
double wrow = drow-row;
double wcol = dcol-col;
double dcol = (lon - xllcorner) / cellsize - 0.5;
double drow = (lat - yllcorner) / cellsize - 0.5;
int row = (int) drow;
int col = (int) dcol;
if (col < 0) col = 0;
if (col >= ncols - 1) col = ncols - 2;
if (row < 0) row = 0;
if (row >= nrows - 1) row = nrows - 2;
double wrow = drow - row;
double wcol = dcol - col;
missingData = false;
// System.out.println( "wrow=" + wrow + " wcol=" + wcol + " row=" + row + " col=" + col );
double eval = (1.-wrow)*(1.-wcol)*get(row ,col )
+ ( wrow)*(1.-wcol)*get(row+1,col )
+ (1.-wrow)*( wcol)*get(row ,col+1)
+ ( wrow)*( wcol)*get(row+1,col+1);
double eval = (1. - wrow) * (1. - wcol) * get(row, col)
+ (wrow) * (1. - wcol) * get(row + 1, col)
+ (1. - wrow) * (wcol) * get(row, col + 1)
+ (wrow) * (wcol) * get(row + 1, col + 1);
// System.out.println( "eval=" + eval );
return missingData ? Short.MIN_VALUE : (short)(eval*4);
return missingData ? Short.MIN_VALUE : (short) (eval * 4);
}
private short get( int r, int c )
{
short e = eval_array[ (nrows-1-r)*ncols + c ];
if ( e == Short.MIN_VALUE ) missingData = true;
private short get(int r, int c) {
short e = eval_array[(nrows - 1 - r) * ncols + c];
if (e == Short.MIN_VALUE) missingData = true;
return e;
}
private short getElevationFromShiftWeights( double lon, double lat )
{
private short getElevationFromShiftWeights(double lon, double lat) {
// calc lat-idx and -weight
double alat = lat < 0. ? - lat : lat;
double alat = lat < 0. ? -lat : lat;
alat /= 5.;
int latIdx = (int)alat;
int latIdx = (int) alat;
double wlat = alat - latIdx;
double dcol = (lon - xllcorner)/cellsize;
double drow = (lat - yllcorner)/cellsize;
int row = (int)drow;
int col = (int)dcol;
double dcol = (lon - xllcorner) / cellsize;
double drow = (lat - yllcorner) / cellsize;
int row = (int) drow;
int col = (int) dcol;
double dgx = (dcol-col)*gridSteps;
double dgy = (drow-row)*gridSteps;
double dgx = (dcol - col) * gridSteps;
double dgy = (drow - row) * gridSteps;
// System.out.println( "wrow=" + wrow + " wcol=" + wcol + " row=" + row + " col=" + col );
int gx = (int)(dgx);
int gy = (int)(dgy);
int gx = (int) (dgx);
int gy = (int) (dgy);
double wx = dgx-gx;
double wy = dgy-gy;
double wx = dgx - gx;
double wy = dgy - gy;
double w00 = (1.-wx)*(1.-wy);
double w01 = (1.-wx)*( wy);
double w10 = ( wx)*(1.-wy);
double w11 = ( wx)*( wy);
double w00 = (1. - wx) * (1. - wy);
double w01 = (1. - wx) * (wy);
double w10 = (wx) * (1. - wy);
double w11 = (wx) * (wy);
Weights[][] w0 = getWeights( latIdx );
Weights[][] w1 = getWeights( latIdx+1 );
Weights[][] w0 = getWeights(latIdx);
Weights[][] w1 = getWeights(latIdx + 1);
missingData = false;
double m0 = w00*getElevation( w0[gx ][gy ], row, col )
+ w01*getElevation( w0[gx ][gy+1], row, col )
+ w10*getElevation( w0[gx+1][gy ], row, col )
+ w11*getElevation( w0[gx+1][gy+1], row, col );
double m1 = w00*getElevation( w1[gx ][gy ], row, col )
+ w01*getElevation( w1[gx ][gy+1], row, col )
+ w10*getElevation( w1[gx+1][gy ], row, col )
+ w11*getElevation( w1[gx+1][gy+1], row, col );
double m0 = w00 * getElevation(w0[gx][gy], row, col)
+ w01 * getElevation(w0[gx][gy + 1], row, col)
+ w10 * getElevation(w0[gx + 1][gy], row, col)
+ w11 * getElevation(w0[gx + 1][gy + 1], row, col);
double m1 = w00 * getElevation(w1[gx][gy], row, col)
+ w01 * getElevation(w1[gx][gy + 1], row, col)
+ w10 * getElevation(w1[gx + 1][gy], row, col)
+ w11 * getElevation(w1[gx + 1][gy + 1], row, col);
if ( missingData ) return Short.MIN_VALUE;
double m = (1.-wlat) * m0 + wlat * m1;
return (short)(m * 2);
if (missingData) return Short.MIN_VALUE;
double m = (1. - wlat) * m0 + wlat * m1;
return (short) (m * 2);
}
private ReducedMedianFilter rmf = new ReducedMedianFilter( 256 );
private ReducedMedianFilter rmf = new ReducedMedianFilter(256);
private double getElevation( Weights w, int row, int col )
{
if ( missingData )
{
private double getElevation(Weights w, int row, int col) {
if (missingData) {
return 0.;
}
int nx = w.nx;
@ -126,64 +119,53 @@ public class SrtmRaster
rmf.reset();
for( int ix = 0; ix < nx; ix ++ )
{
for( int iy = 0; iy < ny; iy ++ )
{
short val = get( row + iy - my, col + ix - mx );
rmf.addSample( w.getWeight( ix, iy ), val );
for (int ix = 0; ix < nx; ix++) {
for (int iy = 0; iy < ny; iy++) {
short val = get(row + iy - my, col + ix - mx);
rmf.addSample(w.getWeight(ix, iy), val);
}
}
return missingData ? 0. : rmf.calcEdgeReducedMedian( filterCenterFraction );
return missingData ? 0. : rmf.calcEdgeReducedMedian(filterCenterFraction);
}
private static class Weights
{
private static class Weights {
int nx;
int ny;
double[] weights;
long total = 0;
Weights( int nx, int ny )
{
Weights(int nx, int ny) {
this.nx = nx;
this.ny = ny;
weights = new double[nx*ny];
weights = new double[nx * ny];
}
void inc( int ix, int iy )
{
weights[ iy*nx + ix ] += 1.;
void inc(int ix, int iy) {
weights[iy * nx + ix] += 1.;
total++;
}
void normalize( boolean verbose )
{
for( int iy =0; iy < ny; iy++ )
{
void normalize(boolean verbose) {
for (int iy = 0; iy < ny; iy++) {
StringBuilder sb = verbose ? new StringBuilder() : null;
for( int ix =0; ix < nx; ix++ )
{
weights[ iy*nx + ix ] /= total;
if ( sb != null )
{
int iweight = (int)(1000*weights[ iy*nx + ix ] + 0.5 );
for (int ix = 0; ix < nx; ix++) {
weights[iy * nx + ix] /= total;
if (sb != null) {
int iweight = (int) (1000 * weights[iy * nx + ix] + 0.5);
String sval = " " + iweight;
sb.append( sval.substring( sval.length() - 4 ) );
sb.append(sval.substring(sval.length() - 4));
}
}
if ( sb != null )
{
System.out.println( sb );
if (sb != null) {
System.out.println(sb);
System.out.println();
}
}
}
double getWeight( int ix, int iy )
{
return weights[ iy*nx + ix ];
double getWeight(int ix, int iy) {
return weights[iy * nx + ix];
}
}
@ -193,19 +175,16 @@ public class SrtmRaster
private static double filterCenterFraction = 0.2;
private static double filterDiscRadius = 4.999; // in pixels
static
{
String sRadius = System.getProperty( "filterDiscRadius" );
if ( sRadius != null && sRadius.length() > 0 )
{
filterDiscRadius = Integer.parseInt( sRadius );
System.out.println( "using filterDiscRadius = " + filterDiscRadius );
static {
String sRadius = System.getProperty("filterDiscRadius");
if (sRadius != null && sRadius.length() > 0) {
filterDiscRadius = Integer.parseInt(sRadius);
System.out.println("using filterDiscRadius = " + filterDiscRadius);
}
String sFraction = System.getProperty( "filterCenterFraction" );
if ( sFraction != null && sFraction.length() > 0 )
{
filterCenterFraction = Integer.parseInt( sFraction ) / 100.;
System.out.println( "using filterCenterFraction = " + filterCenterFraction );
String sFraction = System.getProperty("filterCenterFraction");
if (sFraction != null && sFraction.length() > 0) {
filterCenterFraction = Integer.parseInt(sFraction) / 100.;
System.out.println("using filterCenterFraction = " + filterCenterFraction);
}
}
@ -213,82 +192,73 @@ public class SrtmRaster
// calculate interpolation weights from the overlap of a probe disc of given radius at given latitude
// ( latIndex = 0 -> 0 deg, latIndex = 16 -> 80 degree)
private static Weights[][] getWeights( int latIndex )
{
private static Weights[][] getWeights(int latIndex) {
int idx = latIndex < 16 ? latIndex : 16;
Weights[][] res = allShiftWeights[idx];
if ( res == null )
{
res = calcWeights( idx );
if (res == null) {
res = calcWeights(idx);
allShiftWeights[idx] = res;
}
return res;
}
private static Weights[][] calcWeights( int latIndex )
{
double coslat = Math.cos( latIndex * 5. / 57.3 );
private static Weights[][] calcWeights(int latIndex) {
double coslat = Math.cos(latIndex * 5. / 57.3);
// radius in pixel units
double ry = filterDiscRadius;
double rx = ry / coslat;
// gridsize is 2*radius + 1 cell
int nx = ((int)rx) *2 + 3;
int ny = ((int)ry) *2 + 3;
int nx = ((int) rx) * 2 + 3;
int ny = ((int) ry) * 2 + 3;
System.out.println( "nx="+ nx + " ny=" + ny );
System.out.println("nx=" + nx + " ny=" + ny);
int mx = nx / 2; // mean pixels
int my = ny / 2;
// create a matrix for the relative intergrid-position
Weights[][] shiftWeights = new Weights[gridSteps+1][];
Weights[][] shiftWeights = new Weights[gridSteps + 1][];
// loop the intergrid-position
for( int gx=0; gx<=gridSteps; gx++ )
{
shiftWeights[gx] = new Weights[gridSteps+1];
double x0 = mx + ( (double)gx ) / gridSteps;
for (int gx = 0; gx <= gridSteps; gx++) {
shiftWeights[gx] = new Weights[gridSteps + 1];
double x0 = mx + ((double) gx) / gridSteps;
for( int gy=0; gy<=gridSteps; gy++ )
{
double y0 = my + ( (double)gy ) / gridSteps;
for (int gy = 0; gy <= gridSteps; gy++) {
double y0 = my + ((double) gy) / gridSteps;
// create the weight-matrix
Weights weights = new Weights( nx, ny );
Weights weights = new Weights(nx, ny);
shiftWeights[gx][gy] = weights;
double sampleStep = 0.001;
for( double x = -1. + sampleStep/2.; x < 1.; x += sampleStep )
{
double mx2 = 1. - x*x;
for (double x = -1. + sampleStep / 2.; x < 1.; x += sampleStep) {
double mx2 = 1. - x * x;
int x_idx = (int)(x0 + x*rx);
int x_idx = (int) (x0 + x * rx);
for( double y = -1. + sampleStep/2.; y < 1.; y += sampleStep )
{
if ( y*y > mx2 )
{
for (double y = -1. + sampleStep / 2.; y < 1.; y += sampleStep) {
if (y * y > mx2) {
continue;
}
// we are in the ellipse, see what pixel we are on
int y_idx = (int)(y0 + y*ry);
weights.inc( x_idx, y_idx );
int y_idx = (int) (y0 + y * ry);
weights.inc(x_idx, y_idx);
}
}
weights.normalize( true );
weights.normalize(true);
}
}
return shiftWeights;
}
@Override
public String toString()
{
public String toString() {
return ncols + "," + nrows + "," + halfcol + "," + xllcorner + "," + yllcorner + "," + cellsize + "," + noDataValue + "," + usingWeights;
}
}

View file

@ -1,112 +1,98 @@
package btools.mapcreator;
import java.io.File;
import btools.util.DenseLongMap;
import btools.util.TinyDenseLongMap;
/**
* WayCutter does 2 step in map-processing:
*
* - cut the way file into 45*30 - pieces
* - enrich ways with relation information
*
* @author ab
*/
public class WayCutter extends MapCreatorBase
{
private DenseLongMap tileIndexMap;
public static void main(String[] args) throws Exception
{
System.out.println("*** WayCutter: Soft-Cut way-data into tiles");
if (args.length != 3)
{
System.out.println("usage: java WayCutter <node-tiles-in> <way-file-in> <way-tiles-out>" );
return;
}
new WayCutter().process( new File( args[0] ), new File( args[1] ), new File( args[2] ) );
}
public void process( File nodeTilesIn, File wayFileIn, File wayTilesOut ) throws Exception
{
init( wayTilesOut );
new NodeIterator( this, false ).processDir( nodeTilesIn, ".tlf" );
// *** finally process the way-file, cutting into pieces
new WayIterator( this, true ).processFile( wayFileIn );
finish();
}
public void init( File wayTilesOut ) throws Exception
{
this.outTileDir = wayTilesOut;
// *** read all nodes into tileIndexMap
tileIndexMap = Boolean.getBoolean( "useDenseMaps" ) ? new DenseLongMap() : new TinyDenseLongMap();
}
public void finish() throws Exception
{
closeTileOutStreams();
}
@Override
public void nextNode( NodeData n ) throws Exception
{
tileIndexMap.put( n.nid, getTileIndex( n.ilon, n.ilat ) );
}
@Override
public void nextWay( WayData data ) throws Exception
{
long waytileset = 0;
int nnodes = data.nodes.size();
// determine the tile-index for each node
for (int i=0; i<nnodes; i++ )
{
int tileIndex = tileIndexMap.getInt( data.nodes.get(i) );
if ( tileIndex != -1 )
{
waytileset |= ( 1L << tileIndex );
}
}
// now write way to all tiles hit
for( int tileIndex=0; tileIndex<54; tileIndex++ )
{
if ( ( waytileset & ( 1L << tileIndex ) ) == 0 )
{
continue;
}
data.writeTo( getOutStreamForTile( tileIndex ) );
}
}
public int getTileIndexForNid( long nid )
{
return tileIndexMap.getInt( nid );
}
private int getTileIndex( int ilon, int ilat )
{
int lon = ilon / 45000000;
int lat = ilat / 30000000;
if ( lon < 0 || lon > 7 || lat < 0 || lat > 5 ) throw new IllegalArgumentException( "illegal pos: " + ilon + "," + ilat );
return lon*6 + lat;
}
public String getNameForTile( int tileIndex )
{
int lon = (tileIndex / 6 ) * 45 - 180;
int lat = (tileIndex % 6 ) * 30 - 90;
String slon = lon < 0 ? "W" + (-lon) : "E" + lon;
String slat = lat < 0 ? "S" + (-lat) : "N" + lat;
return slon + "_" + slat + ".wtl";
}
}
package btools.mapcreator;
import java.io.File;
import btools.util.DenseLongMap;
import btools.util.TinyDenseLongMap;
/**
* WayCutter does 2 step in map-processing:
* <p>
* - cut the way file into 45*30 - pieces
* - enrich ways with relation information
*
* @author ab
*/
public class WayCutter extends MapCreatorBase {
private DenseLongMap tileIndexMap;
public static void main(String[] args) throws Exception {
System.out.println("*** WayCutter: Soft-Cut way-data into tiles");
if (args.length != 3) {
System.out.println("usage: java WayCutter <node-tiles-in> <way-file-in> <way-tiles-out>");
return;
}
new WayCutter().process(new File(args[0]), new File(args[1]), new File(args[2]));
}
public void process(File nodeTilesIn, File wayFileIn, File wayTilesOut) throws Exception {
init(wayTilesOut);
new NodeIterator(this, false).processDir(nodeTilesIn, ".tlf");
// *** finally process the way-file, cutting into pieces
new WayIterator(this, true).processFile(wayFileIn);
finish();
}
public void init(File wayTilesOut) throws Exception {
this.outTileDir = wayTilesOut;
// *** read all nodes into tileIndexMap
tileIndexMap = Boolean.getBoolean("useDenseMaps") ? new DenseLongMap() : new TinyDenseLongMap();
}
public void finish() throws Exception {
closeTileOutStreams();
}
@Override
public void nextNode(NodeData n) throws Exception {
tileIndexMap.put(n.nid, getTileIndex(n.ilon, n.ilat));
}
@Override
public void nextWay(WayData data) throws Exception {
long waytileset = 0;
int nnodes = data.nodes.size();
// determine the tile-index for each node
for (int i = 0; i < nnodes; i++) {
int tileIndex = tileIndexMap.getInt(data.nodes.get(i));
if (tileIndex != -1) {
waytileset |= (1L << tileIndex);
}
}
// now write way to all tiles hit
for (int tileIndex = 0; tileIndex < 54; tileIndex++) {
if ((waytileset & (1L << tileIndex)) == 0) {
continue;
}
data.writeTo(getOutStreamForTile(tileIndex));
}
}
public int getTileIndexForNid(long nid) {
return tileIndexMap.getInt(nid);
}
private int getTileIndex(int ilon, int ilat) {
int lon = ilon / 45000000;
int lat = ilat / 30000000;
if (lon < 0 || lon > 7 || lat < 0 || lat > 5)
throw new IllegalArgumentException("illegal pos: " + ilon + "," + ilat);
return lon * 6 + lat;
}
public String getNameForTile(int tileIndex) {
int lon = (tileIndex / 6) * 45 - 180;
int lat = (tileIndex % 6) * 30 - 90;
String slon = lon < 0 ? "W" + (-lon) : "E" + lon;
String slat = lat < 0 ? "S" + (-lat) : "N" + lat;
return slon + "_" + slat + ".wtl";
}
}

View file

@ -1,212 +1,182 @@
package btools.mapcreator;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import btools.util.DenseLongMap;
import btools.util.TinyDenseLongMap;
/**
* WayCutter5 does 2 step in map-processing:
*
* - cut the 45*30 way files into 5*5 pieces
* - create a file containing all border node ids
*
* @author ab
*/
public class WayCutter5 extends MapCreatorBase
{
private DataOutputStream borderNidsOutStream;
private DenseLongMap tileIndexMap;
private File nodeTilesIn;
private int lonoffset;
private int latoffset;
public RelationMerger relMerger;
public NodeFilter nodeFilter;
public NodeCutter nodeCutter;
public RestrictionCutter5 restrictionCutter5;
public static void main(String[] args) throws Exception
{
System.out.println("*** WayCutter5: Soft-Cut way-data into tiles");
if (args.length != 4)
{
System.out.println("usage: java WayCutter5 <node-tiles-in> <way-tiles-in> <way-tiles-out> <border-nids-out>" );
return;
}
new WayCutter5().process( new File( args[0] ), new File( args[1] ), new File( args[2] ), new File( args[3] ) );
}
public void process( File nodeTilesIn, File wayTilesIn, File wayTilesOut, File borderNidsOut ) throws Exception
{
this.nodeTilesIn = nodeTilesIn;
this.outTileDir = wayTilesOut;
borderNidsOutStream = createOutStream( borderNidsOut );
new WayIterator( this, true ).processDir( wayTilesIn, ".wtl" );
borderNidsOutStream.close();
}
@Override
public boolean wayFileStart( File wayfile ) throws Exception
{
// read corresponding node-file into tileIndexMap
String name = wayfile.getName();
String nodefilename = name.substring( 0, name.length()-3 ) + "ntl";
File nodefile = new File( nodeTilesIn, nodefilename );
tileIndexMap = Boolean.getBoolean( "useDenseMaps" ) ? new DenseLongMap() : new TinyDenseLongMap();
lonoffset = -1;
latoffset = -1;
if ( nodeCutter != null )
{
nodeCutter.nodeFileStart( null );
}
new NodeIterator( this, nodeCutter != null ).processFile( nodefile );
if ( restrictionCutter5 != null )
{
String resfilename = name.substring( 0, name.length()-3 ) + "rtl";
File resfile = new File( "restrictions", resfilename );
if ( resfile.exists() )
{
// read restrictions for nodes in nodesMap
DataInputStream di = new DataInputStream( new BufferedInputStream ( new FileInputStream( resfile ) ) );
int ntr = 0;
try
{
for(;;)
{
RestrictionData res = new RestrictionData( di );
restrictionCutter5.nextRestriction( res );
ntr++;
}
}
catch( EOFException eof )
{
di.close();
}
System.out.println( "read " + ntr + " turn-restrictions" );
}
}
return true;
}
@Override
public void nextNode( NodeData n ) throws Exception
{
if ( nodeFilter != null )
{
if ( !nodeFilter.isRelevant( n ) )
{
return;
}
}
if ( nodeCutter != null )
{
nodeCutter.nextNode( n );
}
tileIndexMap.put( n.nid, getTileIndex( n.ilon, n.ilat ) );
}
@Override
public void nextWay( WayData data ) throws Exception
{
long waytileset = 0;
int nnodes = data.nodes.size();
int[] tiForNode = new int[nnodes];
// determine the tile-index for each node
for (int i=0; i<nnodes; i++ )
{
int tileIndex = tileIndexMap.getInt( data.nodes.get(i) );
if ( tileIndex != -1 )
{
waytileset |= ( 1L << tileIndex );
}
tiForNode[i] = tileIndex;
}
if ( relMerger != null )
{
relMerger.nextWay( data );
}
// now write way to all tiles hit
for( int tileIndex=0; tileIndex<54; tileIndex++ )
{
if ( ( waytileset & ( 1L << tileIndex ) ) == 0 )
{
continue;
}
data.writeTo( getOutStreamForTile( tileIndex ) );
}
// and write edge nodes to the border-nid file
for( int i=0; i < nnodes; i++ )
{
int ti = tiForNode[i];
if ( ti != -1 )
{
if ( ( i > 0 && tiForNode[i-1] != ti ) || (i+1 < nnodes && tiForNode[i+1] != ti ) )
{
writeId( borderNidsOutStream, data.nodes.get(i) );
}
}
}
}
@Override
public void wayFileEnd( File wayFile ) throws Exception
{
closeTileOutStreams();
if ( nodeCutter != null )
{
nodeCutter.nodeFileEnd( null );
}
if ( restrictionCutter5 != null )
{
restrictionCutter5.finish();
}
}
public int getTileIndexForNid( long nid )
{
return tileIndexMap.getInt( nid );
}
private int getTileIndex( int ilon, int ilat )
{
int lonoff = (ilon / 45000000 ) * 45;
int latoff = (ilat / 30000000 ) * 30;
if ( lonoffset == -1 ) lonoffset = lonoff;
if ( latoffset == -1 ) latoffset = latoff;
if ( lonoff != lonoffset || latoff != latoffset )
throw new IllegalArgumentException( "inconsistent node: " + ilon + " " + ilat );
int lon = (ilon / 5000000) % 9;
int lat = (ilat / 5000000) % 6;
if ( lon < 0 || lon > 8 || lat < 0 || lat > 5 ) throw new IllegalArgumentException( "illegal pos: " + ilon + "," + ilat );
return lon*6 + lat;
}
protected String getNameForTile( int tileIndex )
{
int lon = (tileIndex / 6 ) * 5 + lonoffset - 180;
int lat = (tileIndex % 6 ) * 5 + latoffset - 90;
String slon = lon < 0 ? "W" + (-lon) : "E" + lon;
String slat = lat < 0 ? "S" + (-lat) : "N" + lat;
return slon + "_" + slat + ".wt5";
}
}
package btools.mapcreator;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import btools.util.DenseLongMap;
import btools.util.TinyDenseLongMap;
/**
* WayCutter5 does 2 step in map-processing:
* <p>
* - cut the 45*30 way files into 5*5 pieces
* - create a file containing all border node ids
*
* @author ab
*/
public class WayCutter5 extends MapCreatorBase {
private DataOutputStream borderNidsOutStream;
private DenseLongMap tileIndexMap;
private File nodeTilesIn;
private int lonoffset;
private int latoffset;
public RelationMerger relMerger;
public NodeFilter nodeFilter;
public NodeCutter nodeCutter;
public RestrictionCutter5 restrictionCutter5;
public static void main(String[] args) throws Exception {
System.out.println("*** WayCutter5: Soft-Cut way-data into tiles");
if (args.length != 4) {
System.out.println("usage: java WayCutter5 <node-tiles-in> <way-tiles-in> <way-tiles-out> <border-nids-out>");
return;
}
new WayCutter5().process(new File(args[0]), new File(args[1]), new File(args[2]), new File(args[3]));
}
public void process(File nodeTilesIn, File wayTilesIn, File wayTilesOut, File borderNidsOut) throws Exception {
this.nodeTilesIn = nodeTilesIn;
this.outTileDir = wayTilesOut;
borderNidsOutStream = createOutStream(borderNidsOut);
new WayIterator(this, true).processDir(wayTilesIn, ".wtl");
borderNidsOutStream.close();
}
@Override
public boolean wayFileStart(File wayfile) throws Exception {
// read corresponding node-file into tileIndexMap
String name = wayfile.getName();
String nodefilename = name.substring(0, name.length() - 3) + "ntl";
File nodefile = new File(nodeTilesIn, nodefilename);
tileIndexMap = Boolean.getBoolean("useDenseMaps") ? new DenseLongMap() : new TinyDenseLongMap();
lonoffset = -1;
latoffset = -1;
if (nodeCutter != null) {
nodeCutter.nodeFileStart(null);
}
new NodeIterator(this, nodeCutter != null).processFile(nodefile);
if (restrictionCutter5 != null) {
String resfilename = name.substring(0, name.length() - 3) + "rtl";
File resfile = new File("restrictions", resfilename);
if (resfile.exists()) {
// read restrictions for nodes in nodesMap
DataInputStream di = new DataInputStream(new BufferedInputStream(new FileInputStream(resfile)));
int ntr = 0;
try {
for (; ; ) {
RestrictionData res = new RestrictionData(di);
restrictionCutter5.nextRestriction(res);
ntr++;
}
} catch (EOFException eof) {
di.close();
}
System.out.println("read " + ntr + " turn-restrictions");
}
}
return true;
}
@Override
public void nextNode(NodeData n) throws Exception {
if (nodeFilter != null) {
if (!nodeFilter.isRelevant(n)) {
return;
}
}
if (nodeCutter != null) {
nodeCutter.nextNode(n);
}
tileIndexMap.put(n.nid, getTileIndex(n.ilon, n.ilat));
}
@Override
public void nextWay(WayData data) throws Exception {
long waytileset = 0;
int nnodes = data.nodes.size();
int[] tiForNode = new int[nnodes];
// determine the tile-index for each node
for (int i = 0; i < nnodes; i++) {
int tileIndex = tileIndexMap.getInt(data.nodes.get(i));
if (tileIndex != -1) {
waytileset |= (1L << tileIndex);
}
tiForNode[i] = tileIndex;
}
if (relMerger != null) {
relMerger.nextWay(data);
}
// now write way to all tiles hit
for (int tileIndex = 0; tileIndex < 54; tileIndex++) {
if ((waytileset & (1L << tileIndex)) == 0) {
continue;
}
data.writeTo(getOutStreamForTile(tileIndex));
}
// and write edge nodes to the border-nid file
for (int i = 0; i < nnodes; i++) {
int ti = tiForNode[i];
if (ti != -1) {
if ((i > 0 && tiForNode[i - 1] != ti) || (i + 1 < nnodes && tiForNode[i + 1] != ti)) {
writeId(borderNidsOutStream, data.nodes.get(i));
}
}
}
}
@Override
public void wayFileEnd(File wayFile) throws Exception {
closeTileOutStreams();
if (nodeCutter != null) {
nodeCutter.nodeFileEnd(null);
}
if (restrictionCutter5 != null) {
restrictionCutter5.finish();
}
}
public int getTileIndexForNid(long nid) {
return tileIndexMap.getInt(nid);
}
private int getTileIndex(int ilon, int ilat) {
int lonoff = (ilon / 45000000) * 45;
int latoff = (ilat / 30000000) * 30;
if (lonoffset == -1) lonoffset = lonoff;
if (latoffset == -1) latoffset = latoff;
if (lonoff != lonoffset || latoff != latoffset)
throw new IllegalArgumentException("inconsistent node: " + ilon + " " + ilat);
int lon = (ilon / 5000000) % 9;
int lat = (ilat / 5000000) % 6;
if (lon < 0 || lon > 8 || lat < 0 || lat > 5)
throw new IllegalArgumentException("illegal pos: " + ilon + "," + ilat);
return lon * 6 + lat;
}
protected String getNameForTile(int tileIndex) {
int lon = (tileIndex / 6) * 5 + lonoffset - 180;
int lat = (tileIndex % 6) * 5 + latoffset - 90;
String slon = lon < 0 ? "W" + (-lon) : "E" + lon;
String slat = lat < 0 ? "S" + (-lat) : "N" + lat;
return slon + "_" + slat + ".wt5";
}
}

View file

@ -1,55 +1,51 @@
package btools.mapcreator;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import btools.util.LongList;
/**
* Container for waydata on the preprocessor level
*
* @author ab
*/
public class WayData extends MapCreatorBase
{
public long wid;
public byte[] description;
public LongList nodes;
public WayData( long id )
{
wid = id;
nodes = new LongList( 16 );
}
public WayData( long id, LongList nodes )
{
wid = id;
this.nodes = nodes;
}
public WayData( DataInputStream di ) throws Exception
{
nodes = new LongList( 16 );
wid = readId( di) ;
int dlen = di.readByte(); description = new byte[dlen]; di.readFully( description );
for (;;)
{
long nid = readId( di );
if ( nid == -1 ) break;
nodes.add( nid );
}
}
public void writeTo( DataOutputStream dos ) throws Exception
{
writeId( dos, wid );
dos.writeByte( description.length ); dos.write( description );
int size = nodes.size();
for( int i=0; i < size; i++ )
{
writeId( dos, nodes.get( i ) );
}
writeId( dos, -1 ); // stopbyte
}
}
package btools.mapcreator;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import btools.util.LongList;
/**
* Container for waydata on the preprocessor level
*
* @author ab
*/
public class WayData extends MapCreatorBase {
public long wid;
public byte[] description;
public LongList nodes;
public WayData(long id) {
wid = id;
nodes = new LongList(16);
}
public WayData(long id, LongList nodes) {
wid = id;
this.nodes = nodes;
}
public WayData(DataInputStream di) throws Exception {
nodes = new LongList(16);
wid = readId(di);
int dlen = di.readByte();
description = new byte[dlen];
di.readFully(description);
for (; ; ) {
long nid = readId(di);
if (nid == -1) break;
nodes.add(nid);
}
}
public void writeTo(DataOutputStream dos) throws Exception {
writeId(dos, wid);
dos.writeByte(description.length);
dos.write(description);
int size = nodes.size();
for (int i = 0; i < size; i++) {
writeId(dos, nodes.get(i));
}
writeId(dos, -1); // stopbyte
}
}

View file

@ -1,80 +1,66 @@
package btools.mapcreator;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
/**
* Iterate over a singe wayfile or a directory
* of waytiles and feed the ways to the callback listener
*
* @author ab
*/
public class WayIterator extends MapCreatorBase
{
private WayListener listener;
private boolean delete;
private boolean descendingSize;
public WayIterator( WayListener wayListener, boolean deleteAfterReading )
{
listener = wayListener;
delete = deleteAfterReading;
}
public WayIterator( WayListener wayListener, boolean deleteAfterReading, boolean descendingSize )
{
this( wayListener, deleteAfterReading );
this.descendingSize = descendingSize;
}
public void processDir( File indir, String inSuffix ) throws Exception
{
if ( !indir.isDirectory() )
{
throw new IllegalArgumentException( "not a directory: " + indir );
}
File[] af = sortBySizeAsc( indir.listFiles() );
for( int i=0; i<af.length; i++ )
{
File wayfile = descendingSize ? af[af.length -1 - i] : af[i];
if ( wayfile.getName().endsWith( inSuffix ) )
{
processFile( wayfile );
}
}
}
public void processFile(File wayfile) throws Exception
{
System.out.println( "*** WayIterator reading: " + wayfile );
if ( !listener.wayFileStart( wayfile ) )
{
return;
}
DataInputStream di = new DataInputStream( new BufferedInputStream ( new FileInputStream( wayfile ) ) );
try
{
for(;;)
{
WayData w = new WayData( di );
listener.nextWay( w );
}
}
catch( EOFException eof )
{
di.close();
}
listener.wayFileEnd( wayfile );
if ( delete && "true".equals( System.getProperty( "deletetmpfiles" ) ))
{
wayfile.delete();
}
}
}
package btools.mapcreator;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
/**
* Iterate over a singe wayfile or a directory
* of waytiles and feed the ways to the callback listener
*
* @author ab
*/
public class WayIterator extends MapCreatorBase {
private WayListener listener;
private boolean delete;
private boolean descendingSize;
public WayIterator(WayListener wayListener, boolean deleteAfterReading) {
listener = wayListener;
delete = deleteAfterReading;
}
public WayIterator(WayListener wayListener, boolean deleteAfterReading, boolean descendingSize) {
this(wayListener, deleteAfterReading);
this.descendingSize = descendingSize;
}
public void processDir(File indir, String inSuffix) throws Exception {
if (!indir.isDirectory()) {
throw new IllegalArgumentException("not a directory: " + indir);
}
File[] af = sortBySizeAsc(indir.listFiles());
for (int i = 0; i < af.length; i++) {
File wayfile = descendingSize ? af[af.length - 1 - i] : af[i];
if (wayfile.getName().endsWith(inSuffix)) {
processFile(wayfile);
}
}
}
public void processFile(File wayfile) throws Exception {
System.out.println("*** WayIterator reading: " + wayfile);
if (!listener.wayFileStart(wayfile)) {
return;
}
DataInputStream di = new DataInputStream(new BufferedInputStream(new FileInputStream(wayfile)));
try {
for (; ; ) {
WayData w = new WayData(di);
listener.nextWay(w);
}
} catch (EOFException eof) {
di.close();
}
listener.wayFileEnd(wayfile);
if (delete && "true".equals(System.getProperty("deletetmpfiles"))) {
wayfile.delete();
}
}
}

View file

@ -32,8 +32,7 @@ import btools.util.LazyArrayOfLists;
*
* @author ab
*/
public class WayLinker extends MapCreatorBase implements Runnable
{
public class WayLinker extends MapCreatorBase implements Runnable {
private File nodeTilesIn;
private File wayTilesIn;
private File trafficTilesIn;
@ -70,108 +69,92 @@ public class WayLinker extends MapCreatorBase implements Runnable
private boolean isSlave;
private ThreadController tc;
public static final class ThreadController
{
public static final class ThreadController {
long maxFileSize = 0L;
long currentSlaveSize;
long currentSlaveSize;
long currentMasterSize = 2000000000L;
synchronized boolean setCurrentMasterSize( long size )
{
try
{
if ( size <= currentSlaveSize )
{
synchronized boolean setCurrentMasterSize(long size) {
try {
if (size <= currentSlaveSize) {
maxFileSize = Long.MAX_VALUE;
return false;
}
currentMasterSize = size;
if ( maxFileSize == 0L )
{
if (maxFileSize == 0L) {
maxFileSize = size;
}
return true;
}
finally
{
} finally {
notify();
}
}
synchronized boolean setCurrentSlaveSize( long size ) throws Exception
{
if ( size >= currentMasterSize )
{
synchronized boolean setCurrentSlaveSize(long size) throws Exception {
if (size >= currentMasterSize) {
return false;
}
while ( size + currentMasterSize + 50000000L > maxFileSize )
{
System.out.println( "****** slave thread waiting for permission to process file of size " + size
+ " currentMaster=" + currentMasterSize + " maxFileSize=" + maxFileSize );
wait( 10000 );
while (size + currentMasterSize + 50000000L > maxFileSize) {
System.out.println("****** slave thread waiting for permission to process file of size " + size
+ " currentMaster=" + currentMasterSize + " maxFileSize=" + maxFileSize);
wait(10000);
}
currentSlaveSize = size;
return true;
}
}
private void reset()
{
private void reset() {
minLon = -1;
minLat = -1;
nodesMap = new CompactLongMap<OsmNodeP>();
borderSet = new CompactLongSet();
}
public static void main( String[] args ) throws Exception
{
System.out.println( "*** WayLinker: Format a region of an OSM map for routing" );
if ( args.length != 8 )
{
public static void main(String[] args) throws Exception {
System.out.println("*** WayLinker: Format a region of an OSM map for routing");
if (args.length != 8) {
System.out
.println( "usage: java WayLinker <node-tiles-in> <way-tiles-in> <bordernodes> <restrictions> <lookup-file> <profile-file> <data-tiles-out> <data-tiles-suffix> " );
.println("usage: java WayLinker <node-tiles-in> <way-tiles-in> <bordernodes> <restrictions> <lookup-file> <profile-file> <data-tiles-out> <data-tiles-suffix> ");
return;
}
new WayLinker().process( new File( args[0] ), new File( args[1] ), new File( args[2] ), new File( args[3] ), new File( args[4] ), new File( args[5] ), new File(
args[6] ), args[7] );
System.out.println( "dumping bad TRs" );
new WayLinker().process(new File(args[0]), new File(args[1]), new File(args[2]), new File(args[3]), new File(args[4]), new File(args[5]), new File(
args[6]), args[7]);
System.out.println("dumping bad TRs");
RestrictionData.dumpBadTRs();
}
public void process( File nodeTilesIn, File wayTilesIn, File borderFileIn, File restrictionsFileIn, File lookupFile, File profileFile, File dataTilesOut,
String dataTilesSuffix ) throws Exception
{
public void process(File nodeTilesIn, File wayTilesIn, File borderFileIn, File restrictionsFileIn, File lookupFile, File profileFile, File dataTilesOut,
String dataTilesSuffix) throws Exception {
WayLinker master = new WayLinker();
WayLinker slave = new WayLinker();
slave.isSlave = true;
master.isSlave = false;
ThreadController tc = new ThreadController();
slave.tc = tc;
master.tc = tc;
master._process( nodeTilesIn, wayTilesIn, borderFileIn, restrictionsFileIn, lookupFile, profileFile, dataTilesOut, dataTilesSuffix );
slave._process( nodeTilesIn, wayTilesIn, borderFileIn, restrictionsFileIn, lookupFile, profileFile, dataTilesOut, dataTilesSuffix );
Thread m = new Thread( master );
Thread s = new Thread( slave );
master._process(nodeTilesIn, wayTilesIn, borderFileIn, restrictionsFileIn, lookupFile, profileFile, dataTilesOut, dataTilesSuffix);
slave._process(nodeTilesIn, wayTilesIn, borderFileIn, restrictionsFileIn, lookupFile, profileFile, dataTilesOut, dataTilesSuffix);
Thread m = new Thread(master);
Thread s = new Thread(slave);
m.start();
s.start();
m.join();
s.join();
}
private void _process( File nodeTilesIn, File wayTilesIn, File borderFileIn, File restrictionsFileIn, File lookupFile, File profileFile, File dataTilesOut,
String dataTilesSuffix ) throws Exception
{
private void _process(File nodeTilesIn, File wayTilesIn, File borderFileIn, File restrictionsFileIn, File lookupFile, File profileFile, File dataTilesOut,
String dataTilesSuffix) throws Exception {
this.nodeTilesIn = nodeTilesIn;
this.wayTilesIn = wayTilesIn;
this.trafficTilesIn = new File( "../traffic" );
this.trafficTilesIn = new File("../traffic");
this.dataTilesOut = dataTilesOut;
this.borderFileIn = borderFileIn;
this.restrictionsFileIn = restrictionsFileIn;
@ -180,114 +163,92 @@ public class WayLinker extends MapCreatorBase implements Runnable
BExpressionMetaData meta = new BExpressionMetaData();
// read lookup + profile for lookup-version + access-filter
expctxWay = new BExpressionContextWay( meta );
meta.readMetaData( lookupFile );
expctxWay = new BExpressionContextWay(meta);
meta.readMetaData(lookupFile);
lookupVersion = meta.lookupVersion;
lookupMinorVersion = meta.lookupMinorVersion;
expctxWay.parseFile( profileFile, "global" );
expctxWay.parseFile(profileFile, "global");
creationTimeStamp = System.currentTimeMillis();
abUnifier = new ByteArrayUnifier( 16384, false );
abUnifier = new ByteArrayUnifier(16384, false);
skipEncodingCheck = Boolean.getBoolean( "skipEncodingCheck" );
skipEncodingCheck = Boolean.getBoolean("skipEncodingCheck");
}
@Override
public void run()
{
try
{
public void run() {
try {
// then process all segments
new WayIterator( this, true, !isSlave ).processDir( wayTilesIn, ".wt5" );
}
catch( Exception e )
{
System.out.println( "******* thread (slave=" + isSlave + ") got Exception: " + e );
throw new RuntimeException( e );
}
finally
{
if (!isSlave)
{
tc.setCurrentMasterSize( 0L );
new WayIterator(this, true, !isSlave).processDir(wayTilesIn, ".wt5");
} catch (Exception e) {
System.out.println("******* thread (slave=" + isSlave + ") got Exception: " + e);
throw new RuntimeException(e);
} finally {
if (!isSlave) {
tc.setCurrentMasterSize(0L);
}
}
}
@Override
public boolean wayFileStart( File wayfile ) throws Exception
{
public boolean wayFileStart(File wayfile) throws Exception {
// master/slave logic:
// total memory size should stay below a maximum
// and no file should be processed twice
long filesize = wayfile.length();
long filesize = wayfile.length();
System.out.println( "**** wayFileStart() for isSlave=" + isSlave + " size=" + filesize );
System.out.println("**** wayFileStart() for isSlave=" + isSlave + " size=" + filesize);
if ( isSlave )
{
if ( !tc.setCurrentSlaveSize( filesize ) )
{
if (isSlave) {
if (!tc.setCurrentSlaveSize(filesize)) {
return false;
}
} else {
if (!tc.setCurrentMasterSize(filesize)) {
return false;
}
}
else
{
if ( !tc.setCurrentMasterSize( filesize ) )
{
return false;
}
}
File trafficFile = fileFromTemplate( wayfile, trafficTilesIn, "trf" );
File trafficFile = fileFromTemplate(wayfile, trafficTilesIn, "trf");
// process corresponding node-file, if any
File nodeFile = fileFromTemplate( wayfile, nodeTilesIn, "u5d" );
if ( nodeFile.exists() )
{
File nodeFile = fileFromTemplate(wayfile, nodeTilesIn, "u5d");
if (nodeFile.exists()) {
reset();
// read the border file
readingBorder = true;
new NodeIterator( this, false ).processFile( borderFileIn );
borderSet = new FrozenLongSet( borderSet );
new NodeIterator(this, false).processFile(borderFileIn);
borderSet = new FrozenLongSet(borderSet);
// read this tile's nodes
readingBorder = false;
new NodeIterator( this, true ).processFile( nodeFile );
new NodeIterator(this, true).processFile(nodeFile);
// freeze the nodes-map
FrozenLongMap<OsmNodeP> nodesMapFrozen = new FrozenLongMap<OsmNodeP>( nodesMap );
FrozenLongMap<OsmNodeP> nodesMapFrozen = new FrozenLongMap<OsmNodeP>(nodesMap);
nodesMap = nodesMapFrozen;
File restrictionFile = fileFromTemplate( wayfile, new File( nodeTilesIn.getParentFile(), "restrictions55" ), "rt5" );
File restrictionFile = fileFromTemplate(wayfile, new File(nodeTilesIn.getParentFile(), "restrictions55"), "rt5");
// read restrictions for nodes in nodesMap
if ( restrictionFile.exists() )
{
DataInputStream di = new DataInputStream( new BufferedInputStream ( new FileInputStream( restrictionFile ) ) );
if (restrictionFile.exists()) {
DataInputStream di = new DataInputStream(new BufferedInputStream(new FileInputStream(restrictionFile)));
int ntr = 0;
try
{
for(;;)
{
RestrictionData res = new RestrictionData( di );
OsmNodeP n = nodesMap.get( res.viaNid );
if ( n != null )
{
if ( ! ( n instanceof OsmNodePT ) )
{
n = new OsmNodePT( n );
nodesMap.put( res.viaNid, n );
try {
for (; ; ) {
RestrictionData res = new RestrictionData(di);
OsmNodeP n = nodesMap.get(res.viaNid);
if (n != null) {
if (!(n instanceof OsmNodePT)) {
n = new OsmNodePT(n);
nodesMap.put(res.viaNid, n);
}
OsmNodePT nt = (OsmNodePT) n;
res.viaLon = nt.ilon;
@ -297,55 +258,49 @@ public class WayLinker extends MapCreatorBase implements Runnable
ntr++;
}
}
}
catch( EOFException eof )
{
} catch (EOFException eof) {
di.close();
}
System.out.println( "read " + ntr + " turn-restrictions" );
System.out.println("read " + ntr + " turn-restrictions");
}
nodesList = nodesMapFrozen.getValueList();
}
// read a traffic-file, if any
if ( trafficFile.exists() )
{
trafficMap = new OsmTrafficMap( expctxWay );
trafficMap.loadAll( trafficFile, minLon, minLat, minLon + 5000000, minLat + 5000000, false );
if (trafficFile.exists()) {
trafficMap = new OsmTrafficMap(expctxWay);
trafficMap.loadAll(trafficFile, minLon, minLat, minLon + 5000000, minLat + 5000000, false);
}
return true;
}
@Override
public void nextNode( NodeData data ) throws Exception
{
OsmNodeP n = data.description == null ? new OsmNodeP() : new OsmNodePT( data.description );
public void nextNode(NodeData data) throws Exception {
OsmNodeP n = data.description == null ? new OsmNodeP() : new OsmNodePT(data.description);
n.ilon = data.ilon;
n.ilat = data.ilat;
n.selev = data.selev;
if ( readingBorder || ( !borderSet.contains( data.nid ) ) )
{
nodesMap.fastPut( data.nid, n );
if (readingBorder || (!borderSet.contains(data.nid))) {
nodesMap.fastPut(data.nid, n);
}
if ( readingBorder )
{
if (readingBorder) {
n.bits |= OsmNodeP.BORDER_BIT;
borderSet.fastAdd( data.nid );
borderSet.fastAdd(data.nid);
return;
}
// remember the segment coords
int min_lon = ( n.ilon / 5000000 ) * 5000000;
int min_lat = ( n.ilat / 5000000 ) * 5000000;
if ( minLon == -1 )
int min_lon = (n.ilon / 5000000) * 5000000;
int min_lat = (n.ilat / 5000000) * 5000000;
if (minLon == -1)
minLon = min_lon;
if ( minLat == -1 )
if (minLat == -1)
minLat = min_lat;
if ( minLat != min_lat || minLon != min_lon )
throw new IllegalArgumentException( "inconsistent node: " + n.ilon + " " + n.ilat );
if (minLat != min_lat || minLon != min_lon)
throw new IllegalArgumentException("inconsistent node: " + n.ilon + " " + n.ilat);
}
// check if one of the nodes has a turn-restriction with
@ -354,39 +309,30 @@ public class WayLinker extends MapCreatorBase implements Runnable
// starts or ends at it's via node. However, we allow
// ways not ending at the via node, and in this case we take
// the leg according to the mapped direction
private void checkRestriction( OsmNodeP n1, OsmNodeP n2, WayData w )
{
checkRestriction( n1, n2, w, true );
checkRestriction( n2, n1, w, false );
private void checkRestriction(OsmNodeP n1, OsmNodeP n2, WayData w) {
checkRestriction(n1, n2, w, true);
checkRestriction(n2, n1, w, false);
}
private void checkRestriction( OsmNodeP n1, OsmNodeP n2, WayData w, boolean checkFrom )
{
private void checkRestriction(OsmNodeP n1, OsmNodeP n2, WayData w, boolean checkFrom) {
RestrictionData r = n2.getFirstRestriction();
while ( r != null )
{
if ( r.fromWid == w.wid )
{
if ( r.fromLon == 0 || checkFrom )
{
while (r != null) {
if (r.fromWid == w.wid) {
if (r.fromLon == 0 || checkFrom) {
r.fromLon = n1.ilon;
r.fromLat = n1.ilat;
n1.bits |= OsmNodeP.DP_SURVIVOR_BIT;
if ( !isEndNode( n2, w ) )
{
if (!isEndNode(n2, w)) {
r.badWayMatch = true;
}
}
}
if ( r.toWid == w.wid )
{
if ( r.toLon == 0 || !checkFrom )
{
if (r.toWid == w.wid) {
if (r.toLon == 0 || !checkFrom) {
r.toLon = n1.ilon;
r.toLat = n1.ilat;
n1.bits |= OsmNodeP.DP_SURVIVOR_BIT;
if ( !isEndNode( n2, w ) )
{
if (!isEndNode(n2, w)) {
r.badWayMatch = true;
}
}
@ -394,56 +340,50 @@ public class WayLinker extends MapCreatorBase implements Runnable
r = r.next;
}
}
private boolean isEndNode( OsmNodeP n, WayData w )
{
return n == nodesMap.get( w.nodes.get( 0 ) ) || n == nodesMap.get( w.nodes.get( w.nodes.size() - 1 ) );
private boolean isEndNode(OsmNodeP n, WayData w) {
return n == nodesMap.get(w.nodes.get(0)) || n == nodesMap.get(w.nodes.get(w.nodes.size() - 1));
}
@Override
public void nextWay( WayData way ) throws Exception
{
byte[] description = abUnifier.unify( way.description );
public void nextWay(WayData way) throws Exception {
byte[] description = abUnifier.unify(way.description);
// filter according to profile
expctxWay.evaluate( false, description );
expctxWay.evaluate(false, description);
boolean ok = expctxWay.getCostfactor() < 10000.;
expctxWay.evaluate( true, description );
expctxWay.evaluate(true, description);
ok |= expctxWay.getCostfactor() < 10000.;
if ( !ok )
if (!ok)
return;
byte wayBits = 0;
expctxWay.decode( description );
if ( !expctxWay.getBooleanLookupValue( "bridge" ) )
expctxWay.decode(description);
if (!expctxWay.getBooleanLookupValue("bridge"))
wayBits |= OsmNodeP.NO_BRIDGE_BIT;
if ( !expctxWay.getBooleanLookupValue( "tunnel" ) )
if (!expctxWay.getBooleanLookupValue("tunnel"))
wayBits |= OsmNodeP.NO_TUNNEL_BIT;
OsmNodeP n1 = null;
OsmNodeP n2 = null;
for ( int i = 0; i < way.nodes.size(); i++ )
{
long nid = way.nodes.get( i );
for (int i = 0; i < way.nodes.size(); i++) {
long nid = way.nodes.get(i);
n1 = n2;
n2 = nodesMap.get( nid );
n2 = nodesMap.get(nid);
if ( n1 != null && n2 != null && n1 != n2 )
{
checkRestriction( n1, n2, way );
OsmLinkP link = n2.createLink( n1 );
if (n1 != null && n2 != null && n1 != n2) {
checkRestriction(n1, n2, way);
OsmLinkP link = n2.createLink(n1);
link.descriptionBitmap = description;
if ( n1.ilon / cellsize != n2.ilon / cellsize || n1.ilat / cellsize != n2.ilat / cellsize )
{
if (n1.ilon / cellsize != n2.ilon / cellsize || n1.ilat / cellsize != n2.ilat / cellsize) {
n2.incWayCount(); // force first node after cell-change to be a
// network node
// network node
}
}
if ( n2 != null )
{
if (n2 != null) {
n2.bits |= wayBits;
n2.incWayCount();
}
@ -451,8 +391,7 @@ public class WayLinker extends MapCreatorBase implements Runnable
}
@Override
public void wayFileEnd( File wayfile ) throws Exception
{
public void wayFileEnd(File wayfile) throws Exception {
int ncaches = divisor * divisor;
int indexsize = ncaches * 4;
@ -466,126 +405,109 @@ public class WayLinker extends MapCreatorBase implements Runnable
int maxLat = minLat + 5000000;
// cleanup duplicate targets
for ( OsmNodeP n : nodesList )
{
if ( n == null || n.getFirstLink() == null || n.isTransferNode() )
for (OsmNodeP n : nodesList) {
if (n == null || n.getFirstLink() == null || n.isTransferNode())
continue;
n.checkDuplicateTargets();
}
// write segment data to individual files
{
int nLonSegs = ( maxLon - minLon ) / 1000000;
int nLatSegs = ( maxLat - minLat ) / 1000000;
int nLonSegs = (maxLon - minLon) / 1000000;
int nLatSegs = (maxLat - minLat) / 1000000;
// sort the nodes into segments
LazyArrayOfLists<OsmNodeP> seglists = new LazyArrayOfLists<OsmNodeP>( nLonSegs * nLatSegs );
for ( OsmNodeP n : nodesList )
{
if ( n == null || n.getFirstLink() == null || n.isTransferNode() )
LazyArrayOfLists<OsmNodeP> seglists = new LazyArrayOfLists<OsmNodeP>(nLonSegs * nLatSegs);
for (OsmNodeP n : nodesList) {
if (n == null || n.getFirstLink() == null || n.isTransferNode())
continue;
if ( n.ilon < minLon || n.ilon >= maxLon || n.ilat < minLat || n.ilat >= maxLat )
if (n.ilon < minLon || n.ilon >= maxLon || n.ilat < minLat || n.ilat >= maxLat)
continue;
int lonIdx = ( n.ilon - minLon ) / 1000000;
int latIdx = ( n.ilat - minLat ) / 1000000;
int lonIdx = (n.ilon - minLon) / 1000000;
int latIdx = (n.ilat - minLat) / 1000000;
int tileIndex = lonIdx * nLatSegs + latIdx;
seglists.getList( tileIndex ).add( n );
seglists.getList(tileIndex).add(n);
}
nodesList = null;
seglists.trimAll();
// open the output file
File outfile = fileFromTemplate( wayfile, dataTilesOut, dataTilesSuffix );
DataOutputStream os = createOutStream( outfile );
File outfile = fileFromTemplate(wayfile, dataTilesOut, dataTilesSuffix);
DataOutputStream os = createOutStream(outfile);
long[] fileIndex = new long[25];
int[] fileHeaderCrcs = new int[25];
// write 5*5 index dummy
for ( int i55 = 0; i55 < 25; i55++ )
{
os.writeLong( 0 );
for (int i55 = 0; i55 < 25; i55++) {
os.writeLong(0);
}
long filepos = 200L;
// sort further in 1/divisor-degree squares
for ( int lonIdx = 0; lonIdx < nLonSegs; lonIdx++ )
{
for ( int latIdx = 0; latIdx < nLatSegs; latIdx++ )
{
for (int lonIdx = 0; lonIdx < nLonSegs; lonIdx++) {
for (int latIdx = 0; latIdx < nLatSegs; latIdx++) {
int tileIndex = lonIdx * nLatSegs + latIdx;
if ( seglists.getSize( tileIndex ) > 0 )
{
List<OsmNodeP> nlist = seglists.getList( tileIndex );
if (seglists.getSize(tileIndex) > 0) {
List<OsmNodeP> nlist = seglists.getList(tileIndex);
LazyArrayOfLists<OsmNodeP> subs = new LazyArrayOfLists<OsmNodeP>( ncaches );
LazyArrayOfLists<OsmNodeP> subs = new LazyArrayOfLists<OsmNodeP>(ncaches);
byte[][] subByteArrays = new byte[ncaches][];
for ( int ni = 0; ni < nlist.size(); ni++ )
{
OsmNodeP n = nlist.get( ni );
int subLonIdx = ( n.ilon - minLon ) / cellsize - divisor * lonIdx;
int subLatIdx = ( n.ilat - minLat ) / cellsize - divisor * latIdx;
for (int ni = 0; ni < nlist.size(); ni++) {
OsmNodeP n = nlist.get(ni);
int subLonIdx = (n.ilon - minLon) / cellsize - divisor * lonIdx;
int subLatIdx = (n.ilat - minLat) / cellsize - divisor * latIdx;
int si = subLatIdx * divisor + subLonIdx;
subs.getList( si ).add( n );
subs.getList(si).add(n);
}
subs.trimAll();
int[] posIdx = new int[ncaches];
int pos = indexsize;
for ( int si = 0; si < ncaches; si++ )
{
List<OsmNodeP> subList = subs.getList( si );
for (int si = 0; si < ncaches; si++) {
List<OsmNodeP> subList = subs.getList(si);
int size = subList.size();
if ( size > 0 )
{
OsmNodeP n0 = subList.get( 0 );
if (size > 0) {
OsmNodeP n0 = subList.get(0);
int lonIdxDiv = n0.ilon / cellsize;
int latIdxDiv = n0.ilat / cellsize;
MicroCache mc = new MicroCache2( size, abBuf2, lonIdxDiv, latIdxDiv, divisor );
MicroCache mc = new MicroCache2(size, abBuf2, lonIdxDiv, latIdxDiv, divisor);
// sort via treemap
TreeMap<Integer, OsmNodeP> sortedList = new TreeMap<Integer, OsmNodeP>();
for ( OsmNodeP n : subList )
{
for (OsmNodeP n : subList) {
long longId = n.getIdFromPos();
int shrinkid = mc.shrinkId( longId );
if ( mc.expandId( shrinkid ) != longId )
{
throw new IllegalArgumentException( "inconstistent shrinking: " + longId );
int shrinkid = mc.shrinkId(longId);
if (mc.expandId(shrinkid) != longId) {
throw new IllegalArgumentException("inconstistent shrinking: " + longId);
}
sortedList.put( Integer.valueOf( shrinkid ), n );
sortedList.put(Integer.valueOf(shrinkid), n);
}
for ( OsmNodeP n : sortedList.values() )
{
n.writeNodeData( mc, trafficMap );
for (OsmNodeP n : sortedList.values()) {
n.writeNodeData(mc, trafficMap);
}
if ( mc.getSize() > 0 )
{
if (mc.getSize() > 0) {
byte[] subBytes;
for ( ;; )
{
int len = mc.encodeMicroCache( abBuf1 );
for (; ; ) {
int len = mc.encodeMicroCache(abBuf1);
subBytes = new byte[len];
System.arraycopy( abBuf1, 0, subBytes, 0, len );
if ( skipEncodingCheck )
{
System.arraycopy(abBuf1, 0, subBytes, 0, len);
if (skipEncodingCheck) {
break;
}
// cross-check the encoding: re-instantiate the cache
MicroCache mc2 = new MicroCache2( new StatCoderContext( subBytes ), new DataBuffers( null ), lonIdxDiv, latIdxDiv, divisor, null, null );
MicroCache mc2 = new MicroCache2(new StatCoderContext(subBytes), new DataBuffers(null), lonIdxDiv, latIdxDiv, divisor, null, null);
// ..and check if still the same
String diffMessage = mc.compareWith( mc2 );
if ( diffMessage != null )
{
if ( MicroCache.debug )
throw new RuntimeException( "encoding crosscheck failed: " + diffMessage );
String diffMessage = mc.compareWith(mc2);
if (diffMessage != null) {
if (MicroCache.debug)
throw new RuntimeException("encoding crosscheck failed: " + diffMessage);
else
MicroCache.debug = true;
}
else
} else
break;
}
pos += subBytes.length + 4; // reserve 4 bytes for crc
@ -595,16 +517,14 @@ public class WayLinker extends MapCreatorBase implements Runnable
posIdx[si] = pos;
}
byte[] abSubIndex = compileSubFileIndex( posIdx );
fileHeaderCrcs[tileIndex] = Crc32.crc( abSubIndex, 0, abSubIndex.length );
os.write( abSubIndex, 0, abSubIndex.length );
for ( int si = 0; si < ncaches; si++ )
{
byte[] abSubIndex = compileSubFileIndex(posIdx);
fileHeaderCrcs[tileIndex] = Crc32.crc(abSubIndex, 0, abSubIndex.length);
os.write(abSubIndex, 0, abSubIndex.length);
for (int si = 0; si < ncaches; si++) {
byte[] ab = subByteArrays[si];
if ( ab != null )
{
os.write( ab );
os.writeInt( Crc32.crc( ab, 0, ab.length ) ^ microCacheEncoding );
if (ab != null) {
os.write(ab);
os.writeInt(Crc32.crc(ab, 0, ab.length) ^ microCacheEncoding);
}
}
filepos += pos;
@ -613,52 +533,46 @@ public class WayLinker extends MapCreatorBase implements Runnable
}
}
byte[] abFileIndex = compileFileIndex( fileIndex, lookupVersion, lookupMinorVersion );
byte[] abFileIndex = compileFileIndex(fileIndex, lookupVersion, lookupMinorVersion);
// write extra data: timestamp + index-checksums
os.writeLong( creationTimeStamp );
os.writeInt( Crc32.crc( abFileIndex, 0, abFileIndex.length ) ^ microCacheEncoding );
for ( int i55 = 0; i55 < 25; i55++ )
{
os.writeInt( fileHeaderCrcs[i55] );
os.writeLong(creationTimeStamp);
os.writeInt(Crc32.crc(abFileIndex, 0, abFileIndex.length) ^ microCacheEncoding);
for (int i55 = 0; i55 < 25; i55++) {
os.writeInt(fileHeaderCrcs[i55]);
}
os.close();
// re-open random-access to write file-index
RandomAccessFile ra = new RandomAccessFile( outfile, "rw" );
ra.write( abFileIndex, 0, abFileIndex.length );
RandomAccessFile ra = new RandomAccessFile(outfile, "rw");
ra.write(abFileIndex, 0, abFileIndex.length);
ra.close();
}
if ( trafficMap != null )
{
if (trafficMap != null) {
trafficMap.finish();
trafficMap = null;
}
System.out.println( "**** codec stats: *******\n" + StatCoderContext.getBitReport() );
System.out.println("**** codec stats: *******\n" + StatCoderContext.getBitReport());
}
private byte[] compileFileIndex( long[] fileIndex, short lookupVersion, short lookupMinorVersion ) throws Exception
{
private byte[] compileFileIndex(long[] fileIndex, short lookupVersion, short lookupMinorVersion) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream( bos );
for ( int i55 = 0; i55 < 25; i55++ )
{
DataOutputStream dos = new DataOutputStream(bos);
for (int i55 = 0; i55 < 25; i55++) {
long versionPrefix = i55 == 1 ? lookupMinorVersion : lookupVersion;
versionPrefix <<= 48;
dos.writeLong( fileIndex[i55] | versionPrefix );
dos.writeLong(fileIndex[i55] | versionPrefix);
}
dos.close();
return bos.toByteArray();
}
private byte[] compileSubFileIndex( int[] posIdx ) throws Exception
{
private byte[] compileSubFileIndex(int[] posIdx) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream( bos );
for ( int si = 0; si < posIdx.length; si++ )
{
dos.writeInt( posIdx[si] );
DataOutputStream dos = new DataOutputStream(bos);
for (int si = 0; si < posIdx.length; si++) {
dos.writeInt(posIdx[si]);
}
dos.close();
return bos.toByteArray();

View file

@ -1,17 +1,16 @@
package btools.mapcreator;
import java.io.File;
/**
* Callbacklistener for WayIterator
*
* @author ab
*/
public interface WayListener
{
boolean wayFileStart( File wayfile ) throws Exception;
void nextWay( WayData data ) throws Exception;
void wayFileEnd( File wayfile ) throws Exception;
}
package btools.mapcreator;
import java.io.File;
/**
* Callbacklistener for WayIterator
*
* @author ab
*/
public interface WayListener {
boolean wayFileStart(File wayfile) throws Exception;
void nextWay(WayData data) throws Exception;
void wayFileEnd(File wayfile) throws Exception;
}

View file

@ -1,54 +1,53 @@
package btools.mapcreator;
import java.util.Random;
import java.util.HashMap;
import org.junit.Assert;
import org.junit.Test;
import java.net.URL;
import java.io.File;
public class MapcreatorTest
{
@Test
public void mapcreatorTest() throws Exception
{
URL mapurl = this.getClass().getResource( "/dreieich.osm.gz" );
Assert.assertTrue( "test-osm-map dreieich.osm not found", mapurl != null );
File mapFile = new File(mapurl.getFile());
File workingDir = mapFile.getParentFile();
File profileDir = new File( workingDir, "/../../../../misc/profiles2" );
File tmpdir = new File( workingDir, "tmp" );
tmpdir.mkdir();
File nodes = new File( tmpdir, "nodetiles" );
nodes.mkdir();
File ways = new File( tmpdir, "waytiles" );
ways.mkdir();
File nodes55 = new File( tmpdir, "nodes55" );
nodes55.mkdir();
File ways55 = new File( tmpdir, "waytiles55" );
ways55.mkdir();
File lookupFile = new File( profileDir, "lookups.dat" );
File relFile = new File( tmpdir, "cycleways.dat" );
File resFile = new File( tmpdir, "restrictions.dat" );
File profileAll = new File( profileDir, "all.brf" );
File profileReport = new File( profileDir, "trekking.brf" );
File profileCheck = new File( profileDir, "softaccess.brf" );
File borderFile = new File( tmpdir, "bordernids.dat" );
new OsmFastCutter().doCut( lookupFile, nodes, ways, nodes55, ways55, borderFile, relFile, resFile, profileAll, profileReport, profileCheck, mapFile );
// run PosUnifier
File unodes55 = new File( tmpdir, "unodes55" );
File bordernodes = new File( tmpdir, "bordernodes.dat" );
unodes55.mkdir();
new PosUnifier().process( nodes55, unodes55, borderFile, bordernodes, "/private-backup/srtm" );
// run WayLinker
File segments = new File( tmpdir, "segments" );
segments.mkdir();
new WayLinker().process( unodes55, ways55, bordernodes, resFile, lookupFile, profileAll, segments, "rd5" );
}
}
package btools.mapcreator;
import java.util.Random;
import java.util.HashMap;
import org.junit.Assert;
import org.junit.Test;
import java.net.URL;
import java.io.File;
public class MapcreatorTest {
@Test
public void mapcreatorTest() throws Exception {
URL mapurl = this.getClass().getResource("/dreieich.osm.gz");
Assert.assertTrue("test-osm-map dreieich.osm not found", mapurl != null);
File mapFile = new File(mapurl.getFile());
File workingDir = mapFile.getParentFile();
File profileDir = new File(workingDir, "/../../../../misc/profiles2");
File tmpdir = new File(workingDir, "tmp");
tmpdir.mkdir();
File nodes = new File(tmpdir, "nodetiles");
nodes.mkdir();
File ways = new File(tmpdir, "waytiles");
ways.mkdir();
File nodes55 = new File(tmpdir, "nodes55");
nodes55.mkdir();
File ways55 = new File(tmpdir, "waytiles55");
ways55.mkdir();
File lookupFile = new File(profileDir, "lookups.dat");
File relFile = new File(tmpdir, "cycleways.dat");
File resFile = new File(tmpdir, "restrictions.dat");
File profileAll = new File(profileDir, "all.brf");
File profileReport = new File(profileDir, "trekking.brf");
File profileCheck = new File(profileDir, "softaccess.brf");
File borderFile = new File(tmpdir, "bordernids.dat");
new OsmFastCutter().doCut(lookupFile, nodes, ways, nodes55, ways55, borderFile, relFile, resFile, profileAll, profileReport, profileCheck, mapFile);
// run PosUnifier
File unodes55 = new File(tmpdir, "unodes55");
File bordernodes = new File(tmpdir, "bordernodes.dat");
unodes55.mkdir();
new PosUnifier().process(nodes55, unodes55, borderFile, bordernodes, "/private-backup/srtm");
// run WayLinker
File segments = new File(tmpdir, "segments");
segments.mkdir();
new WayLinker().process(unodes55, ways55, bordernodes, resFile, lookupFile, profileAll, segments, "rd5");
}
}