city = "Munich, Germany"
graph_file = "munich_bike_network.graphml"
try:
G = ox.load_graphml(graph_file)
except FileNotFoundError:
G = ox.graph_from_place(city, network_type="bike")
ox.save_graphml(G, graph_file)
Как мне выполнить примерку или привязку образца моего дома к карте города. Поскольку networkx выполняет только точное сопоставление, этот подход не работает.
Это моя попытка:
# Step 3: Generate more rotated, scaled, and translated variants of the house shape
def generate_house_variants(rotations=5, scales=[0.8, 1, 1.2], translations=[-0.0005, 0, 0.0005]):
variants = []
base_shape = create_house_shape()
for scale in scales:
for rotation in np.linspace(0, 360, rotations, endpoint=False):
for dx in translations:
for dy in translations:
# Create a variant with transformations
variant = nx.Graph()
for node in base_shape.nodes:
# Transform node positions (set default x, y if not present)
x, y = node % 2, node // 2 # Example positions to get x, y coordinates
x, y = scale * (x + dx), scale * (y + dy)
theta = np.radians(rotation)
x_rot = x * np.cos(theta) - y * np.sin(theta) # Rotate
y_rot = x * np.sin(theta) + y * np.cos(theta)
variant.add_node(node, pos=(x_rot, y_rot))
variant.add_edges_from(base_shape.edges)
variants.append(variant)
return variants
# Step 4: Try to find approximate matches with tolerance
house_variants = generate_house_variants()
matches = []
tolerance = 0.0002 # Latitude/longitude tolerance, adjust based on real-world distances
def node_within_tolerance(n1, n2, tolerance):
pos1 = G.nodes[n1]['x'], G.nodes[n1]['y']
pos2 = n2 # n2 is a tuple (x_rot, y_rot)
return np.sqrt((pos1[0] - pos2[0])**2 + (pos1[1] - pos2[1])**2) < tolerance
for i, H_variant in enumerate(house_variants):
match_found = False
for g_node in G.nodes:
variant_nodes = list(H_variant.nodes(data="pos"))
match = []
for _, pos in variant_nodes:
nearby_nodes = [n for n in G.nodes if node_within_tolerance(n, pos, tolerance)]
if nearby_nodes:
match.extend(nearby_nodes)
else:
break
if len(match) == len(H_variant.nodes):
match_found = True
matches.append((H_variant, match))
break # Stop after finding the first match for this variant
Я работаю на Python. Цель моего проекта — найти закономерность в сети улиц. Например, я определяю «дом» с такими краями и узлами [code]#Step 2: Define the house pattern def create_house_graph(): """Create a house-shaped graph.""" H = nx.Graph() H.add_nodes_from([1, 2, 3, 4, 5]) # 5 nodes H.add_edges_from([(1, 2), (2, 3), (3, 4), (4, 1), (1, 5), (3, 5)]) # Square with triangle top return H
house_graph = create_house_graph() [/code] График моего города определяется как [code]city = "Munich, Germany" graph_file = "munich_bike_network.graphml" try: G = ox.load_graphml(graph_file) except FileNotFoundError: G = ox.graph_from_place(city, network_type="bike") ox.save_graphml(G, graph_file) [/code] Как мне выполнить примерку или привязку образца моего дома к карте города. Поскольку networkx выполняет только точное сопоставление, этот подход не работает. Это моя попытка: [code]# Step 3: Generate more rotated, scaled, and translated variants of the house shape def generate_house_variants(rotations=5, scales=[0.8, 1, 1.2], translations=[-0.0005, 0, 0.0005]): variants = [] base_shape = create_house_shape()
for scale in scales: for rotation in np.linspace(0, 360, rotations, endpoint=False): for dx in translations: for dy in translations: # Create a variant with transformations variant = nx.Graph() for node in base_shape.nodes: # Transform node positions (set default x, y if not present) x, y = node % 2, node // 2 # Example positions to get x, y coordinates x, y = scale * (x + dx), scale * (y + dy) theta = np.radians(rotation) x_rot = x * np.cos(theta) - y * np.sin(theta) # Rotate y_rot = x * np.sin(theta) + y * np.cos(theta) variant.add_node(node, pos=(x_rot, y_rot)) variant.add_edges_from(base_shape.edges) variants.append(variant)
return variants
# Step 4: Try to find approximate matches with tolerance house_variants = generate_house_variants() matches = [] tolerance = 0.0002 # Latitude/longitude tolerance, adjust based on real-world distances
for i, H_variant in enumerate(house_variants): match_found = False for g_node in G.nodes: variant_nodes = list(H_variant.nodes(data="pos")) match = [] for _, pos in variant_nodes: nearby_nodes = [n for n in G.nodes if node_within_tolerance(n, pos, tolerance)] if nearby_nodes: match.extend(nearby_nodes) else: break if len(match) == len(H_variant.nodes): match_found = True matches.append((H_variant, match)) break # Stop after finding the first match for this variant [/code] Кто-нибудь может посоветовать? БР Константин
Я пытаюсь получить данные карты из API OpenStreetMap Overpass, используя Python с функциями Azure. В некоторых случаях (например, при получении всех рек из Турции) ответ на запрос слишком велик, что приводит к сбою моих функций Azure для всех...
Я пытаюсь получить данные карты из API OpenStreetMap Overpass, используя Python с функциями Azure. В некоторых случаях (например, при получении всех рек из Турции) ответ на запрос слишком велик, что приводит к сбою моих функций Azure для всех...
Я пытаюсь получить данные карты из API OpenStreetMap Overpass, используя Python с функциями Azure. В некоторых случаях (например, при получении всех рек из Турции) ответ на запрос слишком велик, что приводит к сбою моих функций Azure для всех...
Я работаю с данными библиотеки OpenStreetMap (osmnx, в Python), чтобы извлечь границу Сальвадора, Бразилия. Код успешно удаляет водоемы и получает материк и острова (я их опускаю для простоты), но есть проблема с искусственной треугольной границей,...
Я пытаюсь получить автобусные линии/маршруты от OpenStreetMap с помощью OSMNX. Например, автобусная линия 284 В NOOTDORP, Netherlands:
Как я получаю точный маршрут (т.е. в качестве списка из узлов) как показано в os os? /> Я попытался использовать...