-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Save Atlas to ProtobufAtlas similar to TextAtlas #99
Changes from 1 commit
bb7706a
1890bc8
a03cade
4e05e34
18611a3
f382466
fd9af27
fbdaacc
c9720fe
bc15496
a804b72
40437f7
70abe50
1eba0c9
d08f1a8
5aa04a8
539337e
0bfec87
5ce39d5
6359a6f
ba3b136
6db5ee7
a7266a6
27aa4e2
4cd48d0
4a06a5d
cd9fb95
f8db3ff
307b015
0e19a0b
2678b77
bc6f404
fe1e9a1
3918bb8
ee43801
eeeb127
0d4e3f0
9657bac
79e7450
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,9 @@ | |
import org.openstreetmap.atlas.geography.atlas.AtlasMetaData; | ||
import org.openstreetmap.atlas.geography.atlas.builder.AtlasSize; | ||
import org.openstreetmap.atlas.geography.atlas.items.Area; | ||
import org.openstreetmap.atlas.geography.atlas.items.Edge; | ||
import org.openstreetmap.atlas.geography.atlas.items.Line; | ||
import org.openstreetmap.atlas.geography.atlas.items.Node; | ||
import org.openstreetmap.atlas.geography.atlas.items.Point; | ||
import org.openstreetmap.atlas.geography.atlas.packed.PackedAtlas; | ||
import org.openstreetmap.atlas.geography.atlas.packed.PackedAtlasBuilder; | ||
|
@@ -23,8 +25,10 @@ | |
import org.openstreetmap.atlas.proto.ProtoArea; | ||
import org.openstreetmap.atlas.proto.ProtoAtlasContainer; | ||
import org.openstreetmap.atlas.proto.ProtoAtlasContainer.Builder; | ||
import org.openstreetmap.atlas.proto.ProtoEdge; | ||
import org.openstreetmap.atlas.proto.ProtoLine; | ||
import org.openstreetmap.atlas.proto.ProtoLocation; | ||
import org.openstreetmap.atlas.proto.ProtoNode; | ||
import org.openstreetmap.atlas.proto.ProtoPoint; | ||
import org.openstreetmap.atlas.streaming.resource.Resource; | ||
import org.openstreetmap.atlas.streaming.resource.WritableResource; | ||
|
@@ -42,6 +46,9 @@ | |
*/ | ||
public class ProtoAtlasBuilder | ||
{ | ||
// File extension for naive protoatlas format | ||
public static final String SUGGESTED_FILE_EXTENSION = ".npatlas"; | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ProtoAtlasBuilder.class); | ||
|
||
/** | ||
|
@@ -70,8 +77,8 @@ public PackedAtlas read(final Resource resource) | |
final long numberOfPoints = protoAtlasContainer.getNumberOfPoints(); | ||
final long numberOfLines = protoAtlasContainer.getNumberOfLines(); | ||
final long numberOfAreas = protoAtlasContainer.getNumberOfAreas(); | ||
final long numberOfNodes = 0; | ||
final long numberOfEdges = 0; | ||
final long numberOfNodes = protoAtlasContainer.getNumberOfNodes(); | ||
final long numberOfEdges = protoAtlasContainer.getNumberOfEdges(); | ||
final long numberOfRelations = 0; | ||
final AtlasSize atlasSize = new AtlasSize(numberOfEdges, numberOfNodes, numberOfAreas, | ||
numberOfLines, numberOfPoints, numberOfRelations); | ||
|
@@ -85,6 +92,8 @@ public PackedAtlas read(final Resource resource) | |
parsePoints(builder, protoAtlasContainer.getPointsList()); | ||
parseLines(builder, protoAtlasContainer.getLinesList()); | ||
parseAreas(builder, protoAtlasContainer.getAreasList()); | ||
parseNodes(builder, protoAtlasContainer.getNodesList()); | ||
parseEdges(builder, protoAtlasContainer.getEdgesList()); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there no relations being added to the new packed Atlas or am I missing something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're getting there. It's still in development :) |
||
return (PackedAtlas) builder.get(); | ||
} | ||
|
@@ -101,9 +110,12 @@ public void write(final Atlas atlas, final WritableResource resource) | |
{ | ||
final ProtoAtlasContainer.Builder protoAtlasBuilder = ProtoAtlasContainer.newBuilder(); | ||
|
||
// get the Atlas features into the ProtoAtlasBuilder | ||
writePointsToBuilder(atlas, protoAtlasBuilder); | ||
writeLinesToBuilder(atlas, protoAtlasBuilder); | ||
writeAreasToBuilder(atlas, protoAtlasBuilder); | ||
writeNodesToBuilder(atlas, protoAtlasBuilder); | ||
writeEdgesToBuilder(atlas, protoAtlasBuilder); | ||
|
||
final ProtoAtlasContainer protoAtlas = protoAtlasBuilder.build(); | ||
resource.writeAndClose(protoAtlas.toByteArray()); | ||
|
@@ -128,6 +140,21 @@ private void parseAreas(final PackedAtlasBuilder builder, final List<ProtoArea> | |
}); | ||
} | ||
|
||
private void parseEdges(final PackedAtlasBuilder builder, final List<ProtoEdge> edges) | ||
{ | ||
final ProtoLocationConverter protoLocationConverter = new ProtoLocationConverter(); | ||
final ProtoTagListConverter protoTagListConverter = new ProtoTagListConverter(); | ||
edges.forEach(protoEdge -> | ||
{ | ||
final long identifier = protoEdge.getId(); | ||
final List<Location> shapePoints = protoEdge.getShapePointsList().stream() | ||
.map(protoLocationConverter::convert).collect(Collectors.toList()); | ||
final PolyLine geometry = new PolyLine(shapePoints); | ||
final Map<String, String> tags = protoTagListConverter.convert(protoEdge.getTagsList()); | ||
builder.addEdge(identifier, geometry, tags); | ||
}); | ||
} | ||
|
||
private void parseLines(final PackedAtlasBuilder builder, final List<ProtoLine> lines) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you want to combine parseLines and parseAreas? The only difference is creating a PolyLine vs. a Polygon, which can be handled with a boolean flag. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to take a look at some of these things today. Please don't merge yet!! |
||
{ | ||
final ProtoLocationConverter protoLocationConverter = new ProtoLocationConverter(); | ||
|
@@ -143,16 +170,31 @@ private void parseLines(final PackedAtlasBuilder builder, final List<ProtoLine> | |
}); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is something Mike and I discussed in our 1on1. We decided for now to have them separate purely so that the design follows the API in a logically consistent way, in spite of the code reuse. Later on if it seems to be an issue we can pretty easily refactor it away. |
||
|
||
private void parseNodes(final PackedAtlasBuilder builder, final List<ProtoNode> nodes) | ||
{ | ||
final ProtoTagListConverter protoTagListConverter = new ProtoTagListConverter(); | ||
nodes.forEach(protoNode -> | ||
{ | ||
final long identifier = protoNode.getId(); | ||
final Longitude longitude = Longitude.dm7(protoNode.getLocation().getLongitude()); | ||
final Latitude latitude = Latitude.dm7(protoNode.getLocation().getLatitude()); | ||
final Location geometry = new Location(latitude, longitude); | ||
final Map<String, String> tags = protoTagListConverter.convert(protoNode.getTagsList()); | ||
builder.addNode(identifier, geometry, tags); | ||
}); | ||
} | ||
|
||
private void parsePoints(final PackedAtlasBuilder builder, final List<ProtoPoint> points) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar comment on combining parseNodes and parsePoints |
||
{ | ||
final ProtoTagListConverter converter = new ProtoTagListConverter(); | ||
final ProtoTagListConverter protoTagListConverter = new ProtoTagListConverter(); | ||
points.forEach(protoPoint -> | ||
{ | ||
final long identifier = protoPoint.getId(); | ||
final Longitude longitude = Longitude.dm7(protoPoint.getLocation().getLongitude()); | ||
final Latitude latitude = Latitude.dm7(protoPoint.getLocation().getLatitude()); | ||
final Location geometry = new Location(latitude, longitude); | ||
final Map<String, String> tags = converter.convert(protoPoint.getTagsList()); | ||
final Map<String, String> tags = protoTagListConverter | ||
.convert(protoPoint.getTagsList()); | ||
builder.addPoint(identifier, geometry, tags); | ||
}); | ||
} | ||
|
@@ -181,6 +223,30 @@ private void writeAreasToBuilder(final Atlas atlas, final Builder protoAtlasBuil | |
protoAtlasBuilder.setNumberOfAreas(numberOfAreas); | ||
} | ||
|
||
private void writeEdgesToBuilder(final Atlas atlas, final Builder protoAtlasBuilder) | ||
{ | ||
long numberOfEdges = 0; | ||
final ProtoLocationConverter protoLocationConverter = new ProtoLocationConverter(); | ||
final ProtoTagListConverter protoTagListConverter = new ProtoTagListConverter(); | ||
|
||
for (final Edge edge : atlas.edges()) | ||
{ | ||
final ProtoEdge.Builder protoEdgeBuilder = ProtoEdge.newBuilder(); | ||
protoEdgeBuilder.setId(edge.getIdentifier()); | ||
|
||
final List<ProtoLocation> protoLocations = edge.asPolyLine().stream() | ||
.map(protoLocationConverter::backwardConvert).collect(Collectors.toList()); | ||
protoEdgeBuilder.addAllShapePoints(protoLocations); | ||
|
||
final Map<String, String> tags = edge.getTags(); | ||
protoEdgeBuilder.addAllTags(protoTagListConverter.backwardConvert(tags)); | ||
|
||
numberOfEdges++; | ||
protoAtlasBuilder.addEdges(protoEdgeBuilder.build()); | ||
} | ||
protoAtlasBuilder.setNumberOfEdges(numberOfEdges); | ||
} | ||
|
||
private void writeLinesToBuilder(final Atlas atlas, | ||
final ProtoAtlasContainer.Builder protoAtlasBuilder) | ||
{ | ||
|
@@ -206,6 +272,29 @@ private void writeLinesToBuilder(final Atlas atlas, | |
protoAtlasBuilder.setNumberOfLines(numberOfLines); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above |
||
|
||
private void writeNodesToBuilder(final Atlas atlas, final Builder protoAtlasBuilder) | ||
{ | ||
long numberOfNodes = 0; | ||
final ProtoLocationConverter protoLocationConverter = new ProtoLocationConverter(); | ||
final ProtoTagListConverter protoTagListConverter = new ProtoTagListConverter(); | ||
|
||
for (final Node node : atlas.nodes()) | ||
{ | ||
final ProtoNode.Builder protoNodeBuilder = ProtoNode.newBuilder(); | ||
|
||
protoNodeBuilder.setId(node.getIdentifier()); | ||
protoNodeBuilder | ||
.setLocation(protoLocationConverter.backwardConvert(node.getLocation())); | ||
|
||
final Map<String, String> tags = node.getTags(); | ||
protoNodeBuilder.addAllTags(protoTagListConverter.backwardConvert(tags)); | ||
|
||
numberOfNodes++; | ||
protoAtlasBuilder.addNodes(protoNodeBuilder.build()); | ||
} | ||
protoAtlasBuilder.setNumberOfNodes(numberOfNodes); | ||
} | ||
|
||
private void writePointsToBuilder(final Atlas atlas, | ||
final ProtoAtlasContainer.Builder protoAtlasBuilder) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding this to the
FileSuffix
class and using it here.