diff --git a/brouter-map-creator/src/main/java/btools/mapcreator/OsmLinkP.java b/brouter-map-creator/src/main/java/btools/mapcreator/OsmLinkP.java index aad7f73..bbeb89e 100644 --- a/brouter-map-creator/src/main/java/btools/mapcreator/OsmLinkP.java +++ b/brouter-map-creator/src/main/java/btools/mapcreator/OsmLinkP.java @@ -17,11 +17,11 @@ public class OsmLinkP /** * The target is either the next link or the target node */ - private OsmNodeP sourceNode; - private OsmNodeP targetNode; + protected OsmNodeP sourceNode; + protected OsmNodeP targetNode; - private OsmLinkP previous; - private OsmLinkP next; + protected OsmLinkP previous; + protected OsmLinkP next; public OsmLinkP( OsmNodeP source, OsmNodeP target ) @@ -30,6 +30,10 @@ public class OsmLinkP targetNode = target; } + protected OsmLinkP() + { + } + public final boolean counterLinkWritten( ) { return descriptionBitmap == null; diff --git a/brouter-map-creator/src/main/java/btools/mapcreator/OsmNodeP.java b/brouter-map-creator/src/main/java/btools/mapcreator/OsmNodeP.java index 2138f9a..4433f7f 100644 --- a/brouter-map-creator/src/main/java/btools/mapcreator/OsmNodeP.java +++ b/brouter-map-creator/src/main/java/btools/mapcreator/OsmNodeP.java @@ -9,7 +9,7 @@ import java.io.IOException; import btools.util.ByteDataWriter; -public class OsmNodeP implements Comparable +public class OsmNodeP extends OsmLinkP implements Comparable { public static final int SIGNLON_BITMASK = 0x80; public static final int SIGNLAT_BITMASK = 0x40; @@ -29,12 +29,6 @@ public class OsmNodeP implements Comparable public int ilon; - /** - * The links to other nodes - */ - public OsmLinkP firstlink = null; - - /** * The elevation */ @@ -46,7 +40,7 @@ public class OsmNodeP implements Comparable public final static int NO_TUNNEL_BIT = 2; public final static int LCN_BIT = 4; public final static int CR_BIT = 8; - + public byte wayBits = 0; // interface OsmPos @@ -72,11 +66,36 @@ public class OsmNodeP implements Comparable } + // populate and return the inherited link, if available, + // else create a new one + public OsmLinkP createLink( OsmNodeP source ) + { + if ( sourceNode == null && targetNode == null ) + { + // inherited instance is available, use this + sourceNode = source; + targetNode = this; + source.addLink( this ); + return this; + } + 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( firstlink, this ); - firstlink = link; + link.setNext( previous, this ); + previous = link; + } + + public OsmLinkP getFirstLink() + { + return sourceNode == null && targetNode == null ? previous : this; } public byte[] getNodeDecsription() @@ -92,15 +111,15 @@ public class OsmNodeP implements Comparable // buffer the body to first calc size ByteDataWriter os2 = new ByteDataWriter( abBuf ); os2.writeShort( getSElev() ); - + // hack: write node-desc as link tag (copy cycleway-bits) byte[] nodeDescription = getNodeDecsription(); - for( OsmLinkP link0 = firstlink; link0 != null; link0 = link0.getNext( this ) ) + for( OsmLinkP link0 = getFirstLink(); link0 != null; link0 = link0.getNext( this ) ) { int ilonref = ilon; int ilatref = ilat; - + OsmLinkP link = link0; OsmNodeP origin = this; int skipDetailBit = link0.descriptionBitmap == null ? SKIPDETAILS_BITMASK : 0; @@ -114,7 +133,7 @@ public class OsmNodeP implements Comparable break; } // next link is the one (of two), does does'nt point back - for( link = target.firstlink; link != null; link = link.getNext( target ) ) + for( link = target.getFirstLink(); link != null; link = link.getNext( target ) ) { if ( link.getTarget( target ) != origin ) break; } @@ -130,8 +149,8 @@ public class OsmNodeP implements Comparable byte[] lastDescription = null; while( link != null ) { - if ( link.descriptionBitmap == null && skipDetailBit == 0 ) throw new IllegalArgumentException( "missing way description..."); - + if ( link.descriptionBitmap == null && skipDetailBit == 0 ) throw new IllegalArgumentException( "missing way description..."); + OsmNodeP target = link.getTarget( origin ); int tranferbit = target.isTransferNode() ? TRANSFERNODE_BITMASK : 0; int nodedescbit = nodeDescription != null ? NODEDESC_BITMASK : 0; @@ -139,9 +158,9 @@ public class OsmNodeP implements Comparable int writedescbit = 0; if ( skipDetailBit == 0 ) // check if description changed { - int inverseBitByteIndex = writeVarLength ? 0 : 7; - boolean inverseDirection = link.isReverse( origin ); - byte[] ab = link.descriptionBitmap; + int inverseBitByteIndex = writeVarLength ? 0 : 7; + boolean inverseDirection = link.isReverse( origin ); + byte[] ab = link.descriptionBitmap; int abLen = ab.length; int lastLen = lastDescription == null ? 0 : lastDescription.length; boolean equalsCurrent = abLen == lastLen; @@ -151,17 +170,17 @@ public class OsmNodeP implements Comparable { byte b = ab[i]; if ( i == inverseBitByteIndex && inverseDirection ) b ^= 1; - if ( b != lastDescription[i] ) { equalsCurrent = false; break; } + if ( b != lastDescription[i] ) { equalsCurrent = false; break; } } } - if ( !equalsCurrent ) - { - writedescbit = WRITEDESC_BITMASK; - lastDescription = new byte[abLen]; - System.arraycopy( ab, 0, lastDescription, 0 , abLen ); - if ( inverseDirection ) lastDescription[inverseBitByteIndex] ^= 1; - } - + if ( !equalsCurrent ) + { + writedescbit = WRITEDESC_BITMASK; + lastDescription = new byte[abLen]; + System.arraycopy( ab, 0, lastDescription, 0 , abLen ); + if ( inverseDirection ) lastDescription[inverseBitByteIndex] ^= 1; + } + } int bm = tranferbit | writedescbit | nodedescbit | skipDetailBit; @@ -190,14 +209,14 @@ public class OsmNodeP implements Comparable } link.descriptionBitmap = null; // mark link as written - + if ( tranferbit == 0) { break; } os2.writeVarLengthSigned( target.getSElev() -getSElev() ); // next link is the one (of two), does does'nt point back - for( link = target.firstlink; link != null; link = link.getNext( target ) ) + for( link = target.getFirstLink(); link != null; link = link.getNext( target ) ) { if ( link.getTarget( target ) != origin ) break; } @@ -208,7 +227,7 @@ public class OsmNodeP implements Comparable // calculate the body size int bodySize = os2.size(); - + os.ensureCapacity( bodySize + 8 ); os.writeShort( (short)(ilon - lonIdx*62500 - 31250) ); @@ -237,7 +256,7 @@ public class OsmNodeP implements Comparable { int cnt = 0; - for( OsmLinkP link = firstlink; link != null; link = link.getNext( this ) ) + for( OsmLinkP link = getFirstLink(); link != null; link = link.getNext( this ) ) { cnt++; } diff --git a/brouter-map-creator/src/main/java/btools/mapcreator/WayLinker.java b/brouter-map-creator/src/main/java/btools/mapcreator/WayLinker.java index 491e660..26934f5 100644 --- a/brouter-map-creator/src/main/java/btools/mapcreator/WayLinker.java +++ b/brouter-map-creator/src/main/java/btools/mapcreator/WayLinker.java @@ -181,10 +181,8 @@ public class WayLinker extends MapCreatorBase n2 = nodesMap.get( nid ); if ( n1 != null && n2 != null && n1 != n2 ) { - OsmLinkP link = new OsmLinkP( n1, n2 ); + OsmLinkP link = n2.createLink( n1 ); link.descriptionBitmap = description; - n1.addLink( link ); - n2.addLink( link ); } if ( n2 != null ) { @@ -214,7 +212,7 @@ public class WayLinker extends MapCreatorBase LazyArrayOfLists seglists = new LazyArrayOfLists(nLonSegs*nLatSegs); for( OsmNodeP n : nodesList ) { - if ( n == null || n.firstlink == null || n.isTransferNode() ) continue; + if ( n == null || n.getFirstLink() == null || n.isTransferNode() ) continue; if ( n.ilon < minLon || n.ilon >= maxLon || n.ilat < minLat || n.ilat >= maxLat ) continue; int lonIdx = (n.ilon-minLon)/1000000;