Skip to content

Instantly share code, notes, and snippets.

@stesie
Created September 11, 2020 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stesie/71035ca376f4456ad20d080351f43922 to your computer and use it in GitHub Desktop.
Save stesie/71035ca376f4456ad20d080351f43922 to your computer and use it in GitHub Desktop.
package btools.mapcreator;
import btools.expressions.BExpressionContextWay;
import btools.expressions.BExpressionMetaData;
import btools.util.DenseLongMap;
import btools.util.TinyDenseLongMap;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
import java.io.DataOutputStream;
import java.io.File;
import static com.mongodb.client.model.Filters.*;
/**
* HeatmapEnricher does 1 step in map-processing:
*
* - add heatmap pseudo tags to ways
*
* @author stefan
*/
public class HeatmapEnricher extends MapCreatorBase
{
private long nodesParsed;
private long nodesFound;
private long waysParsed;
private long waysEnriched;
private DataOutputStream wayOutStream;
private MongoCollection<Document> mongoCollection;
private String mapname;
protected DenseLongMap nodebitmap;
private BExpressionContextWay expctxWay;
public static void main(String[] args) throws Exception
{
System.out.println("*** HeatmapEnricher: Enright ways with heatmap pseudo keys");
if (args.length != 8)
{
System.out.println("usage: java HeatmapEnricher <lookup-file> <mongo-host> <mongo-database> <mongo-collection> <mapname> <node-tiles> <way-file-in> <way-file-out>" );
return;
}
HeatmapEnricher heatmapEnricher = new HeatmapEnricher();
heatmapEnricher.init( new File( args[0]), args[1], args[2], args[3] );
heatmapEnricher.process( args[4], new File( args[5] ), new File( args[6] ), new File( args[7] ) );
}
public void init(File lookupFile, String mongoHost, String databaseName, String collectionName)
{
BExpressionMetaData meta = new BExpressionMetaData();
expctxWay = new BExpressionContextWay( meta );
meta.readMetaData( lookupFile );
nodebitmap = Boolean.getBoolean( "useDenseMaps" ) ? new DenseLongMap( 512 ) : new TinyDenseLongMap();
mongoCollection = new MongoClient(mongoHost).getDatabase(databaseName).getCollection(collectionName);
}
public void process( String mapname, File nodeTiles, File wayFileIn, File wayFileOut ) throws Exception
{
this.mapname = mapname;
// read nodes and query mongo database
new NodeIterator( this, false ).processDir( nodeTiles, ".ntl" );
// finally process ways file
wayOutStream = createOutStream( wayFileOut );
new WayIterator( this, true ).processFile( wayFileIn );
wayOutStream.close();
}
@Override
public void nextWay( WayData data ) throws Exception
{
waysParsed ++;
int nnodes = data.nodes.size();
boolean allMatches = true;
for (int i = 0; i < nnodes; i ++ )
{
boolean hasMatch = nodebitmap.getInt(data.nodes.get(i)) == 0; // 0 -> bit set, -1 -> unset
if (!hasMatch) {
allMatches = false;
break;
}
}
if (allMatches) {
waysEnriched ++;
expctxWay.decode(data.description);
expctxWay.addLookupValue("heatmap:stefan", 2);
data.description = expctxWay.encode();
}
data.writeTo( wayOutStream );
checkWaysStats();
}
@Override
public void nextNode( NodeData n )
{
nodesParsed ++;
double lat = n.ilat / 1000000. - 90;
double lon = n.ilon / 1000000. - 180;
long num = mongoCollection.countDocuments(and(
eq("mapname", mapname),
gte("lat", lat - .0003d),
gte("lng", lon - .0003d),
lte("lat", lat + .0003d),
lte("lng", lon + .0003d)
));
if (num > 0) {
nodebitmap.put(n.nid, 0);
nodesFound ++;
}
checkNodesStats();
}
private void checkNodesStats() {
if (nodesParsed % 1000 == 0) {
System.out.println("nodes checked: " + nodesParsed + ", heatmap matches so far: " + nodesFound);
}
}
private void checkWaysStats() {
if (waysParsed % 1000 == 0) {
System.out.println("ways checked: " + waysParsed + ", heatmap matches so far: " + waysEnriched);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment