def test_convex_hull(self): # the convex hull of a square should be the same as the square squares = GeoSeries([self.sq for i in range(3)]) assert_geoseries_equal(squares, squares.convex_hull)
def to_geopandas(self): from geopandas import GeoSeries return GeoSeries(self.array.to_geopandas(), index=self.index)
def test_where(s): res = s.where(np.array([True, False, True])) exp = GeoSeries([Point(0, 0), None, Point(2, 2)]) assert_series_equal(res, exp)
def test_difference_series2(self): expected = GeoSeries([GeometryCollection(), self.t2]) self._test_binary_operator('__sub__', expected, self.g1, self.g2)
def assert_geoseries_equal(left, right, check_dtype=False, check_index_type=False, check_series_type=True, check_less_precise=False, check_geom_type=False, check_crs=True): """Test util for checking that two GeoSeries are equal. Parameters ---------- left, right : two GeoSeries check_dtype : bool, default False if True, check geo dtype [only included so it's a drop-in replacement for assert_series_equal] check_index_type : bool, default False check that index types are equal check_series_type : bool, default True check that both are same type (*and* are GeoSeries). If False, will attempt to convert both into GeoSeries. check_less_precise : bool, default False if True, use geom_almost_equals. if False, use geom_equals. check_geom_type : bool, default False if True, check that all the geom types are equal. check_crs: bool, default True if check_series_type is True, then also check that the crs matches """ assert len(left) == len(right), "%d != %d" % (len(left), len(right)) if check_index_type: assert isinstance(left.index, type(right.index)) if check_dtype: assert left.dtype == right.dtype, "dtype: %s != %s" % (left.dtype, right.dtype) if check_series_type: assert isinstance(left, GeoSeries) assert isinstance(left, type(right)) if check_crs: assert (left.crs == right.crs) else: if not isinstance(left, GeoSeries): left = GeoSeries(left) if not isinstance(right, GeoSeries): right = GeoSeries(right, index=left.index) assert left.index.equals( right.index), "index: %s != %s" % (left.index, right.index) if check_geom_type: assert (left.type == right.type).all(), "type: %s != %s" % (left.type, right.type) if check_less_precise: assert geom_almost_equals(left, right) else: assert geom_equals(left, right)
def setup_method(self): self.t1 = Polygon([(0, 0), (1, 0), (1, 1)]) self.t2 = Polygon([(0, 0), (1, 1), (0, 1)]) self.t3 = Polygon([(2, 0), (3, 0), (3, 1)]) self.sq = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]) self.inner_sq = Polygon([(0.25, 0.25), (0.75, 0.25), (0.75, 0.75), (0.25, 0.75)]) self.nested_squares = Polygon(self.sq.boundary, [self.inner_sq.boundary]) self.p0 = Point(5, 5) self.g0 = GeoSeries([ self.t1, self.t2, self.sq, self.inner_sq, self.nested_squares, self.p0 ]) self.g1 = GeoSeries([self.t1, self.sq]) self.g2 = GeoSeries([self.sq, self.t1]) self.g3 = GeoSeries([self.t1, self.t2]) self.g3.crs = {'init': 'epsg:4326', 'no_defs': True} self.g4 = GeoSeries([self.t2, self.t1]) self.na = GeoSeries([self.t1, self.t2, Polygon()]) self.na_none = GeoSeries([self.t1, None]) self.a1 = self.g1.copy() self.a1.index = ['A', 'B'] self.a2 = self.g2.copy() self.a2.index = ['B', 'C'] self.esb = Point(-73.9847, 40.7484) self.sol = Point(-74.0446, 40.6893) self.landmarks = GeoSeries([self.esb, self.sol], crs={ 'init': 'epsg:4326', 'no_defs': True }) self.l1 = LineString([(0, 0), (0, 1), (1, 1)]) self.l2 = LineString([(0, 0), (1, 0), (1, 1), (0, 1)]) self.g5 = GeoSeries([self.l1, self.l2]) self.g6 = GeoSeries([self.p0, self.t3]) # Crossed lines self.l3 = LineString([(0, 0), (1, 1)]) self.l4 = LineString([(0, 1), (1, 0)]) self.crossed_lines = GeoSeries([self.l3, self.l4]) # Placeholder for testing, will just drop in different geometries # when needed self.gdf1 = GeoDataFrame({ 'geometry': self.g1, 'col0': [1.0, 2.0], 'col1': ['geo', 'pandas'] }) self.gdf2 = GeoDataFrame({ 'geometry': self.g1, 'col3': [4, 5], 'col4': ['rand', 'string'] })
def test_exterior(self): exp_exterior = GeoSeries([LinearRing(p.boundary) for p in self.g3]) for expected, computed in zip(exp_exterior, self.g3.exterior): assert computed.equals(expected)
def populate_scan_environment(): print "[-] Loading accounts" accounts = get_available_accounts_queue() print "[-] Loading accounts - Done" print "[-] Loading proxies" proxies = get_available_proxies_queue() print "[-] Loading proxies - Done" print "[-] Loading shapefiles" cities = GeoDataFrame.from_file('./geodata/ca_cities/Cities2015.shp') water = GeoDataFrame.from_file('./geodata/baywater/bayarea_allwater.shp') print "[-] Loading shapefiles - Done" print "[-] Reduce + crop geometry" #lng1 = -122.6 #lat1 = 37.1 #lng2 = -121.5 #lat2 = 38 lng1 = -122.457 lat1 = 37.76 lng2 = -122.402 lat2 = 37.80 # This is the cropped area - full geojson is all cities in CA bayarea = Polygon([(lng1, lat1), (lng2,lat1), (lng2,lat2), (lng1,lat1)]) bayarea_crop_frame = GeoDataFrame(geometry=GeoSeries([bayarea]),crs={'init': 'epsg:4269'}) ca_cities = cities.intersection(bayarea_crop_frame.geometry.unary_union) ca_water = water.to_crs(ca_cities.crs) ca_cities_clean = ca_cities.difference(ca_water.geometry.unary_union) simple_ca = GeoDataFrame(geometry=GeoSeries(ca_cities_clean.buffer(0).geometry.unary_union), crs={'init': 'epsg:4269'}) # Buffer and reduce complexity of geometry - covers costal regions + improves performance simple_ca = simple_ca.buffer(0.003).simplify(0.01, preserve_topology=True) print "[-] Reduce + crop geometry - Done" print "[-] Generate coverings" circle_covering = create_circle_covering(70, 50, 65) norm_covering = transform_and_normalize_circle_covering(circle_covering, ca_cities_clean.crs) search_rects = chunked_rect_covering(lng1 - 0.05, lat1 - 0.1, 0.038, 30, 30, (simple_ca)) print "[-] Generate coverings - Done" with open('scanallocation.csv', 'wb') as csvfile: spamwriter = csv.writer(csvfile, delimiter=',') allocated_proxy_count = 0 allocated_region_count = 0 allocated_account_count = 0 for i, r in enumerate(tqdm(search_rects.geometry, position=0)): regions = get_scan_region_covering(i, r, ca_cities_clean, norm_covering).geometry # one ip for every 40*45 scans (40 workers per ip) for region_group_ips in chunks(regions, 40 * 45): try: allocated_proxy_count += 1 proxy = proxies.pop() except Exception as e: print "\nRan out of --- proxies --- {} regions, using {} accounts, using {} proxies".\ format(allocated_region_count, allocated_account_count, allocated_proxy_count) return # one worker for every 45 scans for i, region_group in enumerate(chunks(region_group_ips, 45)): try: allocated_account_count += 1 allocate_account = accounts.pop() except Exception as e: print "\nRan out of --- accounts --- {} regions, using {} accounts, using {} proxies".\ format(allocated_region_count, allocated_account_count, allocated_proxy_count) return allocated_region_count += 1 for region in region_group: # proxy, username, password, region_lat, region_lng spamwriter.writerow(["https://"+proxy, allocate_account["username"], allocate_account["password"], region.centroid.x, region.centroid.y]) print "\nDone!: {} regions, using {} accounts, using {} proxies".\ format(allocated_region_count, allocated_account_count, allocated_proxy_count)
def test_empty_geoseries(self): assert GeoSeries().sindex is None
def test_unique(): s = GeoSeries([Point(0, 0), Point(0, 0), Point(2, 2)]) exp = from_shapely([Point(0, 0), Point(2, 2)]) # TODO should have specialized GeometryArray assert method assert_array_equal(s.unique(), exp)
def test_value_counts(): # each object is considered unique s = GeoSeries([Point(0, 0), Point(1, 1), Point(0, 0)]) res = s.value_counts() exp = pd.Series([2, 1], index=[Point(0, 0), Point(1, 1)]) assert_series_equal(res, exp)
def test_buffer_distance_wrong_index(self): original = GeoSeries([self.p0, self.p0], index=[0, 1]) distances = Series(data=[1, 2], index=[99, 98]) with pytest.raises(ValueError): original.buffer(distances)
def test_buffer_distance_wrong_length(self): original = GeoSeries([self.p0, self.p0]) distances = np.array([1, 2, 3]) with pytest.raises(ValueError): original.buffer(distances)
def test_buffer(self): original = GeoSeries([Point(0, 0)]) expected = GeoSeries( [Polygon(((5, 0), (0, -5), (-5, 0), (0, 5), (5, 0)))]) calculated = original.buffer(5, resolution=1) assert geom_almost_equals(expected, calculated)
def test_difference_series(self): expected = GeoSeries([GeometryCollection(), self.t2]) self._test_binary_topological('difference', expected, self.g1, self.g2)
def test_empty_point(self): s = GeoSeries([Point()]) assert s.sindex is None assert s._sindex_generated is True
def test_difference_poly(self): expected = GeoSeries([self.t1, self.t1]) self._test_binary_topological('difference', expected, self.g1, self.t2)
def test_polygons(self): t1 = Polygon([(0, 0), (1, 0), (1, 1)]) t2 = Polygon([(0, 0), (1, 1), (0, 1)]) sq = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]) s = GeoSeries([t1, t2, sq]) assert s.sindex.size == 3
def test_boundary(self): l1 = LineString([(0, 0), (1, 0), (1, 1), (0, 0)]) l2 = LineString([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]) expected = GeoSeries([l1, l2], index=self.g1.index, crs=self.g1.crs) self._test_unary_topological('boundary', expected, self.g1)
def test_lazy_build(self): s = GeoSeries([Point(0, 0)]) assert s._sindex is None assert s.sindex.size == 1 assert s._sindex is not None
def test_interiors(self): square_series = GeoSeries(self.nested_squares) exp_interiors = GeoSeries([LinearRing(self.inner_sq.boundary)]) for expected, computed in zip(exp_interiors, square_series.interiors): assert computed[0].equals(expected)
def draw_union(): fig, ax = plt.subplots(figsize=(20, 16)) pu = cascaded_union([shapely.wkt.loads(x["footprint"]) for x in L]) GeoSeries(pu).plot(ax=ax, edgecolor='k', linewidth=1)
def test_difference_poly2(self): expected = GeoSeries([self.t1, self.t1]) self._test_binary_operator('__sub__', expected, self.g1, self.t2)
def test_set_geometry_inplace(self): geom = [Point(x, y) for x, y in zip(range(5), range(5))] ret = self.df.set_geometry(geom, inplace=True) assert ret is None geom = GeoSeries(geom, index=self.df.index, crs=self.df.crs) assert_geoseries_equal(self.df.geometry, geom)
def setUp(self): N = self.N = 10 r = 0.5 self.pts = GeoSeries([Point(x, y) for x, y in zip(range(N), range(N))]) self.polys = self.pts.buffer(r)
power_location_df.Latitude)) power_location_gdf = power_location_gdf.set_crs('epsg:4326') # power_location_gdf.crs = {'init': 'epsg:4326'} # print(power_location_gdf.crs) # %%create power transmission lines geodataframe lst = [] branch_list = list(zip(branch[:, 0], branch[:, 1])) for x in branch_list: a = int(x[0]) b = int(x[1]) transline = LineString(power_location_gdf.loc[a, 'geometry'].coords[:] + power_location_gdf.loc[b, 'geometry'].coords[:]) lst.append(transline) power_geoseries = GeoSeries(lst) # print(power_geoseries) # power_geoseries.plot() # %%pipeline geodataframe belg = {} with open('belgian.json', 'r') as f: belg = json.load(f) # create the list of pipes as well as a dictionary with keys of numberings and values of pipe names c = 1 pipe_dict = {} pipe_list = [] for idx, component in belg['pipe'].items(): print(c, idx, (component['f_junction'], component['t_junction'])) pipe_dict[c] = idx
def s(): return GeoSeries([Point(x, y) for x, y in zip(range(3), range(3))])
def test_symmetric_difference_poly(self): expected = GeoSeries([GeometryCollection(), self.sq], crs=self.g3.crs) self._test_binary_topological('symmetric_difference', expected, self.g3, self.t1)
def test_dropna(): s2 = GeoSeries([Point(0, 0), None, Point(2, 2)]) res = s2.dropna() exp = s2.loc[[0, 2]] assert_geoseries_equal(res, exp)
def test_centroid(self): polygon = Polygon([(-1, -1), (1, -1), (1, 1), (-1, 1)]) point = Point(0, 0) polygons = GeoSeries([polygon for i in range(3)]) points = GeoSeries([point for i in range(3)]) assert_geoseries_equal(polygons.centroid, points)