本文整理汇总了Python中geotrek.core.models.Topology类的典型用法代码示例。如果您正苦于以下问题:Python Topology类的具体用法?Python Topology怎么用?Python Topology使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Topology类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_overlapping_returned_can_be_filtered
def test_overlapping_returned_can_be_filtered(self):
overlaps = Topology.overlapping(self.topo1)
overlaps = overlaps.exclude(pk=self.topo1.pk)
self.assertEqual(len(overlaps), 4)
overlaps = Topology.overlapping(self.topo1)
overlaps = overlaps.filter(pk__in=[self.point1.pk, self.point2.pk])
self.assertEqual(len(overlaps), 2)
示例2: test_point_at_end_of_path_not_moving_after_mutate
def test_point_at_end_of_path_not_moving_after_mutate(self):
PathFactory.create(geom=LineString((400, 400), (410, 400), srid=settings.SRID))
self.assertEqual(1, len(Path.objects.all()))
father = Topology.deserialize({"lat": -1, "lng": -1})
poi = Point(500, 600, srid=settings.SRID)
poi.transform(settings.API_SRID)
son = Topology.deserialize({"lat": poi.y, "lng": poi.x})
father.mutate(son)
self.assertTrue(almostequal(father.geom.x, 500))
self.assertTrue(almostequal(father.geom.y, 600))
示例3: forwards
def forwards(self, orm):
# Keep track of previous paths associated to trails
by_paths = {}
for path, trail in db.execute("SELECT id, sentier FROM l_t_troncon;"):
by_paths.setdefault(trail, []).append(path)
# Deleting trail reference from path
db.delete_column("l_t_troncon", "sentier")
# Fields are now in topology model
db.delete_column("l_t_sentier", "date_update")
db.delete_column("l_t_sentier", "date_insert")
# Adding field 'Trail.topo_object'
db.add_column(
"l_t_sentier",
"topo_object",
self.gf("django.db.models.fields.related.OneToOneField")(
to=orm["core.Topology"], null=True, db_column="evenement"
),
keep_default=False,
)
# Restore NAIVELY previous trails
from geotrek.core.models import Topology
trails = db.execute("SELECT id FROM l_t_sentier")
print "Migrating %s trails" % len(trails)
for (trail,) in trails:
topo = Topology()
topo.kind = "TRAIL"
topo.save()
print "Created empty topology %s" % topo.pk
db.execute("UPDATE l_t_sentier SET evenement = %s WHERE id = %s", (topo.pk, trail))
trail_paths = by_paths.get(trail, [])
print "%s paths for trail %s" % (len(trail_paths), trail)
for path in trail_paths:
db.execute(
"INSERT INTO e_r_evenement_troncon (troncon, evenement, pk_debut, pk_fin, ordre) VALUES (%s,%s, 0.0, 1.0, 1)",
(path, topo.pk),
)
db.execute("ALTER TABLE l_t_sentier ALTER COLUMN evenement SET NOT NULL")
db.execute("ALTER TABLE l_t_sentier DROP CONSTRAINT l_t_sentier_pkey CASCADE")
db.execute("ALTER TABLE l_t_sentier DROP COLUMN IF EXISTS id")
db.execute("ALTER TABLE l_t_sentier ADD CONSTRAINT l_t_sentier_pkey PRIMARY KEY (evenement)")
示例4: test_point_geom_3d
def test_point_geom_3d(self):
"""
+
/ \
/ X \
+ +
"""
p1 = PathFactory.create(geom=LineString((0, 0, 1000), (4, 4, 2000)))
p2 = PathFactory.create(geom=LineString((4, 4, 2000), (8, 0, 0)))
poi = Point(3, 1, srid=settings.SRID)
position, distance = Path.interpolate(p1, poi)
self.assertTrue(almostequal(0.5, position))
self.assertTrue(almostequal(-1.414, distance))
# Verify that deserializing this, we obtain the same original coordinates
# (use lat/lng as in forms)
poi.transform(settings.API_SRID)
poitopo = Topology.deserialize({'lat': poi.y, 'lng': poi.x})
# Computed topology properties match original interpolation
self.assertTrue(almostequal(0.5, poitopo.aggregations.all()[0].start_position))
self.assertTrue(almostequal(-1.414, poitopo.offset))
# Resulting geometry
self.assertTrue(almostequal(3, poitopo.geom.x))
self.assertTrue(almostequal(1, poitopo.geom.y))
self.assertTrue(almostequal(0, poitopo.geom.z))
示例5: test_return_path_serialized
def test_return_path_serialized(self):
"""
Same as test_return_path() but from deserialization.
"""
p1 = PathFactory.create(geom=LineString((0, 0), (10, 0)))
p2 = PathFactory.create(geom=LineString((5, 0), (5, 10), (10, 10)))
p3 = Path.objects.filter(name=p1.name).exclude(pk=p1.pk)[0] # Was splitted :)
topo = Topology.deserialize(
"""
[{"offset":0,
"positions":{"0":[0.5,1],
"1":[0.0, 0.8]},
"paths":[%(p1)s,%(p2)s]
},
{"offset":0,
"positions":{"0":[0.8,0.0],
"1":[0.0, 0.5]},
"paths":[%(p2)s,%(p3)s]
}
]
"""
% {"p1": p1.pk, "p2": p2.pk, "p3": p3.pk}
)
topo.save()
self.assertEqual(topo.geom, LineString((2.5, 0), (5, 0), (5, 10), (7, 10), (5, 10), (5, 0), (7.5, 0)))
示例6: test_point_geom_not_moving
def test_point_geom_not_moving(self):
"""
Modify path, point not moving
+ +
| |
\ X / X
/ \
| |
+ +
"""
p1 = PathFactory.create(geom=LineString((0, 0, 0),
(0, 5, 0),
(5, 10, 0),
(0, 15, 0),
(0, 20, 0)))
poi = Point(10, 10, srid=settings.SRID)
poi.transform(settings.API_SRID)
poitopo = Topology.deserialize({'lat': poi.y, 'lng': poi.x})
self.assertEqual(0.5, poitopo.aggregations.all()[0].start_position)
self.assertTrue(almostequal(-5, poitopo.offset))
# It should have kept its position !
self.assertTrue(almostequal(10, poitopo.geom.x))
self.assertTrue(almostequal(10, poitopo.geom.y))
# Change path, it should still be in the same position
p1.geom = LineString((0, 0, 0),
(0, 5, 0),
(-5, 10, 0),
(0, 15, 0),
(0, 20, 0))
p1.save()
poitopo.reload()
self.assertTrue(almostequal(10, poitopo.geom.x))
self.assertTrue(almostequal(10, poitopo.geom.y))
示例7: test_spoon_loop_2
def test_spoon_loop_2(self):
"""
=====>====
|| ||
+-------===<===>===+=====<===
"""
p1 = PathFactory.create(geom=LineString((0, 0, 0), (10, 0, 0)))
p2 = PathFactory.create(geom=LineString((10, 0, 0), (10, 5, 0),
(20, 5, 0), (20, 0, 0),
(10, 0, 0)))
topo = TopologyFactory.create(no_path=True)
topo.add_path(p1, start=0.3, end=1)
topo.add_path(p2, start=0, end=0.4)
topo.add_path(p2, start=0.4, end=0.4)
topo.add_path(p2, start=0.4, end=0.8)
topo.add_path(p2, start=0.8, end=0.8)
topo.add_path(p2, start=0.8, end=1.0)
topo.add_path(p1, start=1, end=0.3)
topo.save()
self.assertEqual(topo.geom, LineString((3, 0, 0), (10, 0, 0), (10, 5, 0),
(17, 5, 0), (20, 5, 0), # extra point due middle aggregation
(20, 0, 0), (16, 0, 0), (10, 0, 0), (3, 0, 0)))
# De/Serializing should work too
serialized = """
[{"kind": "TOPOLOGY","positions":{"0":[0.3,1],"1":[0, 0.4]},"paths":[%(pk1)s,%(pk2)s],"offset": 0.0},
{"kind": "TOPOLOGY","positions":{"0":[0.4, 0.8]},"paths":[%(pk2)s],"offset": 0.0},
{"kind": "TOPOLOGY","positions":{"0":[0.8,1],"1":[1,0.3]},"paths":[%(pk2)s,%(pk1)s],"offset": 0.0}]""" % {'pk1': p1.pk, 'pk2': p2.pk}
self.assertEqual(json.loads(serialized), json.loads(topo.serialize()))
topod = Topology.deserialize(serialized)
self.assertEqual(topo.geom, topod.geom)
self.assertEqual(len(topod.aggregations.all()), 7)
示例8: filter
def filter(qs, edges):
"""
This piece of code was moved from core, and should be rewritten nicely
with managers : TODO !
"""
# TODO: this is wrong, land should not depend on maintenance
import geotrek.maintenance as maintenance
overlapping = Topology.overlapping(edges)
# In case, we filter on paths
if qs.model == Path:
paths = []
for o in overlapping:
paths.extend(o.paths.all())
return qs.filter(pk__in=[path.pk for path in set(paths)])
# TODO: This is (amazingly) ugly in terms of OOP. Should refactor overlapping()
elif issubclass(qs.model, maintenance.models.Intervention):
return qs.filter(topology__in=[topo.pk for topo in overlapping])
elif issubclass(qs.model, maintenance.models.Project):
# Find all interventions overlapping those edges
interventions = filter(maintenance.models.Intervention.objects.existing()
.select_related(depth=1)
.filter(project__in=qs),
edges)
# Return only the projects concerned by the interventions
projects = []
for intervention in interventions:
projects.append(intervention.project.pk)
return qs.filter(pk__in=set(projects))
else:
assert issubclass(qs.model, Topology), "%s is not a Topology as expected" % qs.model
return qs.filter(pk__in=[topo.pk for topo in overlapping])
示例9: test_spoon_loop
def test_spoon_loop(self):
"""
=====<====
|| ||
+-------===<===>===+=====>===
"""
p1 = PathFactory.create(geom=LineString((0, 0, 0), (10, 0, 0)))
p2 = PathFactory.create(geom=LineString((10, 0, 0), (10, 5, 0),
(20, 5, 0), (20, 0, 0),
(10, 0, 0)))
topo = TopologyFactory.create(no_path=True)
topo.add_path(p1, start=0.3, end=1)
topo.add_path(p2, start=1, end=0.4)
topo.add_path(p2, start=0.4, end=0.4)
topo.add_path(p2, start=0.4, end=0.2)
topo.add_path(p2, start=0.2, end=0.2)
topo.add_path(p2, start=0.2, end=0)
topo.add_path(p1, start=1, end=0.3)
topo.save()
self.assertEqual(topo.geom, LineString((3, 0, 0), (10, 0, 0), (20, 0, 0), (20, 5, 0),
(17, 5, 0), (11, 5, 0), # extra point due middle aggregation
(10, 5, 0), (10, 0, 0), (3, 0, 0)))
# Deserializing should work too
topod = Topology.deserialize("""
[{"positions":{"0":[0.3,1],"1":[1, 0.4]},"paths":[%(pk1)s,%(pk2)s]},
{"positions":{"0":[0.4, 0.2]},"paths":[%(pk2)s]},
{"positions":{"0":[0.2,0],"1":[1,0.3]},"paths":[%(pk2)s,%(pk1)s]}]""" % {'pk1': p1.pk, 'pk2': p2.pk})
self.assertEqual(topo.geom, topod.geom)
self.assertEqual(len(topod.aggregations.all()), 7)
示例10: test_deserialize_line
def test_deserialize_line(self):
path = PathFactory.create()
topology = Topology.deserialize('[{"paths": [%s], "positions": {"0": [0.0, 1.0]}, "offset": 1}]' % (path.pk))
self.assertEqual(topology.offset, 1)
self.assertEqual(topology.kind, Topology.KIND)
self.assertEqual(len(topology.paths.all()), 1)
self.assertEqual(topology.aggregations.all()[0].path, path)
self.assertEqual(topology.aggregations.all()[0].start_position, 0.0)
self.assertEqual(topology.aggregations.all()[0].end_position, 1.0)
示例11: test_deserialize_multiple_lines
def test_deserialize_multiple_lines(self):
# Multiple paths
p1 = PathFactory.create(geom=LineString((0, 0, 0), (2, 2, 2)))
p2 = PathFactory.create(geom=LineString((2, 2, 2), (2, 0, 0)))
p3 = PathFactory.create(geom=LineString((2, 0, 0), (4, 0, 0)))
pks = [p.pk for p in [p1, p2, p3]]
topology = Topology.deserialize('{"paths": %s, "positions": {"0": [0.0, 1.0], "2": [0.0, 1.0]}, "offset": 1}' % (pks))
for i in range(3):
self.assertEqual(topology.aggregations.all()[i].start_position, 0.0)
self.assertEqual(topology.aggregations.all()[i].end_position, 1.0)
topology = Topology.deserialize('{"paths": %s, "positions": {"0": [0.3, 1.0], "2": [0.0, 0.7]}, "offset": 1}' % (pks))
self.assertEqual(topology.aggregations.all()[0].start_position, 0.3)
self.assertEqual(topology.aggregations.all()[0].end_position, 1.0)
self.assertEqual(topology.aggregations.all()[1].start_position, 0.0)
self.assertEqual(topology.aggregations.all()[1].end_position, 1.0)
self.assertEqual(topology.aggregations.all()[2].start_position, 0.0)
self.assertEqual(topology.aggregations.all()[2].end_position, 0.7)
示例12: test_point_geom_moving
def test_point_geom_moving(self):
p1 = PathFactory.create(geom=LineString((0, 0), (0, 5)))
poi = Point(0, 2.5, srid=settings.SRID)
poi.transform(settings.API_SRID)
poitopo = Topology.deserialize({"lat": poi.y, "lng": poi.x})
self.assertTrue(almostequal(0.5, poitopo.aggregations.all()[0].start_position))
self.assertTrue(almostequal(0, poitopo.offset))
self.assertTrue(almostequal(0, poitopo.geom.x))
self.assertTrue(almostequal(2.5, poitopo.geom.y))
p1.geom = LineString((10, 0), (10, 5))
p1.save()
poitopo.reload()
self.assertTrue(almostequal(10, poitopo.geom.x))
self.assertTrue(almostequal(2.5, poitopo.geom.y))
示例13: test_deserialize_serialize
def test_deserialize_serialize(self):
path = PathFactory.create(geom=LineString((1, 1), (2, 2), (2, 0)))
before = TopologyFactory.create(offset=1, no_path=True)
before.add_path(path, start=0.5, end=0.5)
# Deserialize its serialized version !
after = Topology.deserialize(before.serialize())
self.assertEqual(len(before.paths.all()), len(after.paths.all()))
start_before = before.aggregations.all()[0].start_position
end_before = before.aggregations.all()[0].end_position
start_after = after.aggregations.all()[0].start_position
end_after = after.aggregations.all()[0].end_position
self.assertTrue(almostequal(start_before, start_after), '%s != %s' % (start_before, start_after))
self.assertTrue(almostequal(end_before, end_after), '%s != %s' % (end_before, end_after))
示例14: test_deserialize_point
def test_deserialize_point(self):
PathFactory.create()
# Take a point
p = Point(2, 1, 0, srid=settings.SRID)
p.transform(settings.API_SRID)
closest = Path.closest(p)
# Check closest path
self.assertEqual(closest.geom.coords, ((1.0, 1.0, 0.0), (2.0, 2.0, 0.0)))
# The point has same x as first point of path, and y to 0 :
topology = Topology.deserialize('{"lng": %s, "lat": %s}' % (p.x, p.y))
self.assertAlmostEqual(topology.offset, -0.7071, 3)
self.assertEqual(len(topology.paths.all()), 1)
pagg = topology.aggregations.get()
self.assertTrue(almostequal(pagg.start_position, 0.5))
self.assertTrue(almostequal(pagg.end_position, 0.5))
示例15: test_deserialize_serialize
def test_deserialize_serialize(self):
path = PathFactory.create(geom=LineString((1, 1, 1), (2, 2, 2), (2, 0, 0)))
before = TopologyFactory.create(offset=1, no_path=True)
before.add_path(path, start=0.5, end=0.5)
# Reload from DB
before = Topology.objects.get(pk=before.pk)
# Deserialize its serialized version !
after = Topology.deserialize(before.serialize())
# Reload from DB
after = Topology.objects.get(pk=after.pk)
self.assertEqual(len(before.paths.all()), len(after.paths.all()))
self.assertTrue(almostequal(before.aggregations.all()[0].start_position,
after.aggregations.all()[0].start_position))
self.assertTrue(almostequal(before.aggregations.all()[0].end_position,
after.aggregations.all()[0].end_position))