def test_pp_from_single_libpysal_point(self): # network instantiated from a single libpysal.cg.Chain known_dist = 1.4142135623730951 self.ntw_from_chain = self.spaghetti.Network(in_data=P11P22_CHAIN) self.ntw_from_chain.snapobservations(P00, synth_obs) snap_dist = self.ntw_from_chain.pointpatterns[synth_obs].dist_snapped[ 0] self.assertAlmostEqual(snap_dist, known_dist, places=10) # network instantiated from a single vertical (up) libpysal.cg.Chain chain = cg.Chain([P11, P12]) known_dist = 1.0 self.ntw_from_chain = self.spaghetti.Network(in_data=chain) self.ntw_from_chain.snapobservations(cg.Point((0, 1.5)), synth_obs) snap_dist = self.ntw_from_chain.pointpatterns[synth_obs].dist_snapped[ 0] self.assertEqual(snap_dist, known_dist) # network instantiated from a single vertical (down) libpysal.cg.Chain chain = cg.Chain([cg.Point((5, 5)), cg.Point((5, 4))]) known_dist = 1.5 self.ntw_from_chain = self.spaghetti.Network(in_data=chain) self.ntw_from_chain.snapobservations(cg.Point((6.5, 4.5)), synth_obs) snap_dist = self.ntw_from_chain.pointpatterns[synth_obs].dist_snapped[ 0] self.assertEqual(snap_dist, known_dist)
def test_network_from_vertical_libpysal_chains(self): vert_up = cg.Chain([P0505, P052]) self.ntw_up_chain = self.spaghetti.Network(in_data=vert_up) self.assertEqual(len(self.ntw_up_chain.arcs), len(vert_up.segments)) vert_down = cg.Chain([P052, P0505]) self.ntw_down_chain = self.spaghetti.Network(in_data=vert_down) self.assertEqual(len(self.ntw_down_chain.arcs), len(vert_down.segments))
def setUp(self): # synthetic test data bounds, h, v = (0, 0, 3, 3), 2, 2 lattice = self.spaghetti.regular_lattice(bounds, h, nv=v, exterior=True) self.ntw = self.spaghetti.Network(in_data=lattice) chains = [ cg.Chain([ cg.Point(self.ntw.vertex_coords[p1]), cg.Point(self.ntw.vertex_coords[p2]), ]) for (p1, p2) in self.ntw.arcs ] midpoints = [] for chain in chains: (v1x, v1y), (v2x, v2y) = chain.vertices mid = cg.Point(((v1x + v2x) / 2.0, (v1y + v2y) / 2.0)) midpoints.append(mid) self.mids = "mids" self.ntw.snapobservations(midpoints, self.mids) npts = self.ntw.pointpatterns[self.mids].npoints self.test_permutations = 99 self.test_steps = 10 # empirical test_data self.ntw_shp = self.spaghetti.Network(in_data=STREETS) self.ntw_shp.snapobservations(CRIMES, crimes, attribute=True)
def setUp(self): bounds, h, v = (0, 0, 3, 3), 2, 2 lattice = self.spaghetti.regular_lattice(bounds, h, nv=v, exterior=True) self.ntw = self.spaghetti.Network(in_data=lattice) chains = [ cg.Chain([ cg.Point(self.ntw.vertex_coords[p1]), cg.Point(self.ntw.vertex_coords[p2]), ]) for (p1, p2) in self.ntw.arcs ] midpoints = [] for chain in chains: (v1x, v1y), (v2x, v2y) = chain.vertices mid = cg.Point(((v1x + v2x) / 2.0, (v1y + v2y) / 2.0)) midpoints.append(mid) self.mids = "mids" self.ntw.snapobservations(midpoints, self.mids) npts = self.ntw.pointpatterns[self.mids].npoints numpy.random.seed(0) self.ntw.simulate_observations(npts) self.test_permutations = 99 self.test_steps = 10
def _chain_constr(_vcoords, _vs): """Construct a libpysal.cg.Chain object. Parameters ---------- _vcoords : {dict, None} See ``vcoords`` in ``get_chains()``. _vs : tuple Start and end vertex IDs of arc. Returns ------- chain : libpysal.cg.Chain Spatial representation of the arc. """ if _vcoords: chain_vtx_points = [cg.Point((_vcoords[v])) for v in _vs] else: chain_vtx_points = _vs chain = cg.Chain(chain_vtx_points) return chain
def setUp(self): # empirical network instantiated from shapefile self.ntw_shp = self.spaghetti.Network(in_data=STREETS, weightings=True) self.n_known_shp_arcs, self.n_known_shp_vertices = 303, 230 # native pysal geometries self.chains_from_shp = [ cg.Chain([ cg.Point(self.ntw_shp.vertex_coords[vertex]) for vertex in arc ]) for arc in self.ntw_shp.arcs ] # lattice and ring + extension bounds, h, v = (0, 0, 2, 2), 1, 1 self.lattice = self.spaghetti.regular_lattice(bounds, h, nv=v) self.lines = self.lattice + RING + EXTENSION self.ntw_from_lattice_ring = self.spaghetti.Network(in_data=self.lines) self.ntw_from_lattice_ring.snapobservations([P0505, P052], "points")
# native pysal geometries ------------------------------------------------------ P00 = cg.Point((0, 0)) P03 = cg.Point((0, 3)) P030001 = cg.Point((0, 3.0001)) P01 = cg.Point((0, 1)) P10 = cg.Point((1, 0)) P11 = cg.Point((1, 1)) P12 = cg.Point((1, 2)) P21 = cg.Point([2, 1]) P22 = cg.Point((2, 2)) P33 = cg.Point((3, 3)) P34 = cg.Point((3, 4)) P40 = cg.Point((4, 0)) P400010 = cg.Point((4.0001, 0)) P11P22_CHAIN = cg.Chain([P11, P22]) P0505 = cg.Point([0.5, 0.5]) P052 = cg.Point([0.5, 2.0]) P0525 = cg.Point([0.5, 2.5]) P2505 = cg.Point([2.5, 0.5]) P2525 = cg.Point([2.5, 2.5]) P31 = cg.Point([3, 1]) P325125 = cg.Point([3.25, 1.25]) P3375125 = cg.Point([3.375, 1.25]) P35125 = cg.Point([3.5, 1.25]) P3751 = cg.Point([3.75, 1]) P35075 = cg.Point([3.5, 0.75]) P3375075 = cg.Point([3.375, 0.75]) P325075 = cg.Point([3.25, 0.75])
def build_chains(space_h, space_v, exterior, bounds, h=True): """Generate line segments for a lattice. Parameters ---------- space_h : list Horizontal spacing. space_v : list Vertical spacing. exterior : bool Flag for including the outer bounding box segments. bounds : list Area bounds in the form - <minx,miny,maxx,maxy>. h : bool Generate horizontal line segments. Default is ``True``. ``False`` generates vertical segments. Returns ------- chains : list All horizontal or vertical line segments in the lattice. """ # Initialize starting and ending indices start_h, end_h, start_v, end_v = 0, len(space_h), 0, len(space_v) # set inital index track back to 0 minus_y, minus_x = 0, 0 if h: # start track back at 1 for horizontal lines minus_x = 1 if not exterior: # do not include borders start_v += 1 end_v -= 1 else: # start track back at 1 for vertical lines minus_y = 1 if not exterior: # do not include borders start_h += 1 end_h -= 1 # Create empty line list and fill chains = [] # for element in the horizontal index for plus_h in range(start_h, end_h): # for element in the vertical index for plus_v in range(start_v, end_v): # ignore if a -1 index if plus_h - minus_x == -1 or plus_v - minus_y == -1: continue else: # Point 1 (start point + previous slot in # horizontal or vertical space index) p1x = bounds[0] + space_h[plus_h - minus_x] p1y = bounds[1] + space_v[plus_v - minus_y] p1 = cg.Point((p1x, p1y)) # Point 2 (start point + current slot in # horizontal or vertical space index) p2x = bounds[0] + space_h[plus_h] p2y = bounds[1] + space_v[plus_v] p2 = cg.Point((p2x, p2y)) # libpysal.cg.Chain chains.append(cg.Chain([p1, p2])) return chains