mirror of
https://github.com/FoggedLens/deflock-app.git
synced 2026-07-12 07:16:34 +02:00
Disallow editing location of nodes attached to ways/relations
This commit is contained in:
@@ -47,44 +47,7 @@ Future<List<OsmNode>> fetchOsmApiNodes({
|
||||
|
||||
// Parse XML response
|
||||
final document = XmlDocument.parse(response.body);
|
||||
final nodes = <OsmNode>[];
|
||||
|
||||
// Find all node elements
|
||||
for (final nodeElement in document.findAllElements('node')) {
|
||||
final id = int.tryParse(nodeElement.getAttribute('id') ?? '');
|
||||
final latStr = nodeElement.getAttribute('lat');
|
||||
final lonStr = nodeElement.getAttribute('lon');
|
||||
|
||||
if (id == null || latStr == null || lonStr == null) continue;
|
||||
|
||||
final lat = double.tryParse(latStr);
|
||||
final lon = double.tryParse(lonStr);
|
||||
if (lat == null || lon == null) continue;
|
||||
|
||||
// Parse tags
|
||||
final tags = <String, String>{};
|
||||
for (final tagElement in nodeElement.findElements('tag')) {
|
||||
final key = tagElement.getAttribute('k');
|
||||
final value = tagElement.getAttribute('v');
|
||||
if (key != null && value != null) {
|
||||
tags[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if this node matches any of our profiles
|
||||
if (_nodeMatchesProfiles(tags, profiles)) {
|
||||
nodes.add(OsmNode(
|
||||
id: id,
|
||||
coord: LatLng(lat, lon),
|
||||
tags: tags,
|
||||
));
|
||||
}
|
||||
|
||||
// Respect maxResults limit if set
|
||||
if (maxResults > 0 && nodes.length >= maxResults) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
final nodes = _parseOsmApiResponseWithConstraints(document, profiles, maxResults);
|
||||
|
||||
if (nodes.isNotEmpty) {
|
||||
debugPrint('[fetchOsmApiNodes] Retrieved ${nodes.length} matching surveillance nodes');
|
||||
@@ -107,6 +70,93 @@ Future<List<OsmNode>> fetchOsmApiNodes({
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse OSM API XML response to create OsmNode objects with constraint information.
|
||||
List<OsmNode> _parseOsmApiResponseWithConstraints(XmlDocument document, List<NodeProfile> profiles, int maxResults) {
|
||||
final surveillanceNodes = <int, Map<String, dynamic>>{}; // nodeId -> node data
|
||||
final constrainedNodeIds = <int>{};
|
||||
|
||||
// First pass: collect surveillance nodes
|
||||
for (final nodeElement in document.findAllElements('node')) {
|
||||
final id = int.tryParse(nodeElement.getAttribute('id') ?? '');
|
||||
final latStr = nodeElement.getAttribute('lat');
|
||||
final lonStr = nodeElement.getAttribute('lon');
|
||||
|
||||
if (id == null || latStr == null || lonStr == null) continue;
|
||||
|
||||
final lat = double.tryParse(latStr);
|
||||
final lon = double.tryParse(lonStr);
|
||||
if (lat == null || lon == null) continue;
|
||||
|
||||
// Parse tags
|
||||
final tags = <String, String>{};
|
||||
for (final tagElement in nodeElement.findElements('tag')) {
|
||||
final key = tagElement.getAttribute('k');
|
||||
final value = tagElement.getAttribute('v');
|
||||
if (key != null && value != null) {
|
||||
tags[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if this node matches any of our profiles
|
||||
if (_nodeMatchesProfiles(tags, profiles)) {
|
||||
surveillanceNodes[id] = {
|
||||
'id': id,
|
||||
'lat': lat,
|
||||
'lon': lon,
|
||||
'tags': tags,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Second pass: identify constrained nodes from ways
|
||||
for (final wayElement in document.findAllElements('way')) {
|
||||
for (final ndElement in wayElement.findElements('nd')) {
|
||||
final ref = int.tryParse(ndElement.getAttribute('ref') ?? '');
|
||||
if (ref != null && surveillanceNodes.containsKey(ref)) {
|
||||
constrainedNodeIds.add(ref);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Third pass: identify constrained nodes from relations
|
||||
for (final relationElement in document.findAllElements('relation')) {
|
||||
for (final memberElement in relationElement.findElements('member')) {
|
||||
if (memberElement.getAttribute('type') == 'node') {
|
||||
final ref = int.tryParse(memberElement.getAttribute('ref') ?? '');
|
||||
if (ref != null && surveillanceNodes.containsKey(ref)) {
|
||||
constrainedNodeIds.add(ref);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create OsmNode objects with constraint information
|
||||
final nodes = <OsmNode>[];
|
||||
for (final nodeData in surveillanceNodes.values) {
|
||||
final nodeId = nodeData['id'] as int;
|
||||
final isConstrained = constrainedNodeIds.contains(nodeId);
|
||||
|
||||
nodes.add(OsmNode(
|
||||
id: nodeId,
|
||||
coord: LatLng(nodeData['lat'], nodeData['lon']),
|
||||
tags: nodeData['tags'] as Map<String, String>,
|
||||
isConstrained: isConstrained,
|
||||
));
|
||||
|
||||
// Respect maxResults limit if set
|
||||
if (maxResults > 0 && nodes.length >= maxResults) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
final constrainedCount = nodes.where((n) => n.isConstrained).length;
|
||||
if (constrainedCount > 0) {
|
||||
debugPrint('[fetchOsmApiNodes] Found $constrainedCount constrained nodes out of ${nodes.length} total');
|
||||
}
|
||||
|
||||
return nodes;
|
||||
}
|
||||
|
||||
/// Check if a node's tags match any of the given profiles
|
||||
bool _nodeMatchesProfiles(Map<String, String> nodeTags, List<NodeProfile> profiles) {
|
||||
for (final profile in profiles) {
|
||||
|
||||
Reference in New Issue
Block a user