def test_read_usborder(self): z = zones.GetUsBorder() self.assertTrue(z.is_valid) us_area = 9.8e6 approx_area = z.area * 110**2 * np.cos(44 * np.pi / 180) self.assertTrue(approx_area > us_area * 0.9 and approx_area < us_area * 1.1)
def _DefaultProtectionPoints(dpa_name, dpa_geometry, num_pts_front_border=25, num_pts_back_border=10, num_pts_front_zone=10, num_pts_back_zone=5, front_us_border_buffer_km=40, min_dist_front_border_pts_km=0.2, min_dist_back_border_pts_km=1., min_dist_front_zone_pts_km=0.5, min_dist_back_zone_pts_km=3.): """Returns a default list of DPA protection points. This creates a default set of points by regular sampling of the contour and gridding the interior of the DPA zone. The contour and zone are separated in front and back, using the US border (with a buffer) as the delimitation. Approximate minimum distance can be specified to avoid too many points for small inland DPAs. Single point DPA are obviously protected by a single point. Args: dpa_geometry: The DPA geometry as a |shapely.Polygon/MultiPolygon or Point|. num_pts_front_border: Number of points in the front border. num_pts_back_border: Number of points in the back border. num_pts_front_zone: Number of points in the front zone. num_pts_back_zone: Number of points in the back zone. front_us_border_buffer_km: Buffering of US border for delimiting front/back. min_dist_front_border_pts_km: Minimum distance between front border points (km). min_dist_back_border_pts_km: Minimum distance between back border points (km). min_dist_front_zone_pts_km: Minimum distance between front zone points (km). min_dist_back_zone_pts_km: Minimum distance between back zone points (km). """ def SampleLine(line, num_points, min_step_arcsec): step = line.length / float(num_points) min_step = min_step_arcsec / 3600. if step < min_step: num_points = max(1, int(line.length / min_step)) step = line.length / float(num_points) return [ line.interpolate(dist) for dist in np.arange(0, line.length - step / 2., step) ] def CvtKmToArcSec(dist_km, ref_geo): EQUATORIAL_RADIUS_KM = 6378. return dist_km / (EQUATORIAL_RADIUS_KM * 2 * np.pi / 360. * np.cos(ref_geo.centroid.y * np.pi / 180.)) * 3600. # Case of DPA points if isinstance(dpa_geometry, sgeo.Point): return [ ProtectionPoint(longitude=dpa_geometry.x, latitude=dpa_geometry.y) ] # Sanity checks num_pts_front_border = max(num_pts_front_border, 1) # at least one # Case of Polygon/MultiPolygon global _us_border_ext global _us_border_ext_buffer if _us_border_ext is None or _us_border_ext_buffer != front_us_border_buffer_km: us_border = zones.GetUsBorder() _us_border_ext = us_border.buffer( CvtKmToArcSec(front_us_border_buffer_km, us_border) / 3600.) _us_border_ext_buffer = front_us_border_buffer_km front_border = dpa_geometry.boundary.intersection(_us_border_ext) back_border = dpa_geometry.boundary.difference(_us_border_ext) front_zone = dpa_geometry.intersection(_us_border_ext) back_zone = dpa_geometry.difference(_us_border_ext) # Obtain an approximate grid step, insuring a minimum separation between zone points step_front_dpa_arcsec = CvtKmToArcSec(min_dist_front_zone_pts_km, dpa_geometry) if num_pts_front_zone != 0: step_front_dpa_arcsec = max( np.sqrt(front_zone.area / num_pts_front_zone) * 3600., step_front_dpa_arcsec) step_back_dpa_arcsec = CvtKmToArcSec(min_dist_back_zone_pts_km, dpa_geometry) if num_pts_back_zone != 0: step_back_dpa_arcsec = max( np.sqrt(back_zone.area / num_pts_back_zone) * 3600., step_back_dpa_arcsec) # Obtain the number of points in the border, insuring a minimum separation min_step_front_border = CvtKmToArcSec(min_dist_front_border_pts_km, dpa_geometry) min_step_back_border = CvtKmToArcSec(min_dist_back_border_pts_km, dpa_geometry) front_border_pts = [] if not front_border else [ ProtectionPoint(longitude=pt.x, latitude=pt.y) for pt in SampleLine( front_border, num_pts_front_border, min_step_front_border) ] back_border_pts = [] if ( not back_border or num_pts_back_border == 0) else [ ProtectionPoint(longitude=pt.x, latitude=pt.y) for pt in SampleLine(back_border, num_pts_back_border, min_step_back_border) ] front_zone_pts = [] if (not front_zone or num_pts_front_zone == 0) else [ ProtectionPoint(longitude=pt[0], latitude=pt[1]) for pt in utils.GridPolygon(front_zone, step_front_dpa_arcsec) ] back_zone_pts = [] if (not back_zone or num_pts_back_zone == 0) else [ ProtectionPoint(longitude=pt[0], latitude=pt[1]) for pt in utils.GridPolygon(back_zone, step_back_dpa_arcsec) ] return front_border_pts + front_zone_pts + back_border_pts + back_zone_pts
def PrepareSimulation(): # Read the DPA zone print 'Preparing Zones' dpa_zone = zones.GetCoastalDpaZones()[dpa_name] dpa_geometry = dpa_zone.geometry us_border = zones.GetUsBorder() urban_areas = zones.GetUrbanAreas() if do_inside_urban_area else None protection_zone = zones.GetCoastalProtectionZone() # Distribute random CBSD of various types around the FSS. print 'Distributing random CBSDs in DPA neighborhood' # - Find the zone where to distribute the CBSDs typical_lat = dpa_geometry.centroid.y km_per_lon_deg = 111. * np.cos(typical_lat * np.pi / 180) extend_cata_deg = max_dist_cat_a / km_per_lon_deg extend_catb_deg = max_dist_cat_b / km_per_lon_deg zone_cata = dpa_geometry.buffer(extend_cata_deg).intersection(us_border) zone_catb = dpa_geometry.buffer(extend_catb_deg).intersection(us_border) if urban_areas is not None: # simplify the huge urban_areas for quicker inclusion tests urban_areas = urban_areas.intersection(zone_catb) # - Distribute the CBSDs print ' - Cat A indoor' cbsds_cat_a_indoor = entities.GenerateCbsdsInPolygon( num_sites * ratio_cat_a_indoor, entities.CBSD_TEMPLATE_CAT_A_INDOOR, zone_cata, drive.nlcd_driver, urban_areas) print ' - Cat A outdoor' cbsds_cat_a_outdoor = entities.GenerateCbsdsInPolygon( num_sites * ratio_cat_a_outdoor, entities.CBSD_TEMPLATE_CAT_A_OUTDOOR, zone_cata, drive.nlcd_driver, urban_areas) print ' - Cat B' cbsds_cat_b = entities.GenerateCbsdsInPolygon(num_sites * ratio_cat_b, entities.CBSD_TEMPLATE_CAT_B, zone_catb, drive.nlcd_driver, urban_areas) all_cbsds = cbsds_cat_a_indoor + cbsds_cat_a_outdoor + cbsds_cat_b # - Convert them into proper registration and grant requests reg_requests = [ entities.GetCbsdRegistrationRequest(cbsd) for cbsd in all_cbsds ] grant_requests = [ entities.GetCbsdGrantRequest(cbsd, fmin, fmax) for cbsd in all_cbsds ] # Plot on screen the elements of calculation ax = plt.axes(projection=ccrs.PlateCarree()) margin = 0.1 box = zone_catb.union(dpa_geometry) ax.axis([ box.bounds[0] - margin, box.bounds[2] + margin, box.bounds[1] - margin, box.bounds[3] + margin ]) ax.coastlines() ax.stock_img() ax.plot(*dpa_geometry.exterior.xy, color='r') ax.plot(*zone_cata.exterior.xy, color='b', linestyle='--') ax.plot(*zone_catb.exterior.xy, color='g', linestyle='--') ax.plot(*protection_zone[0].exterior.xy, color='m', linestyle=':') ax.plot(*protection_zone[1].exterior.xy, color='m', linestyle=':') ax.scatter([cbsd.longitude for cbsd in all_cbsds], [cbsd.latitude for cbsd in all_cbsds], color='g', marker='.') ax.set_title('DPA: %s' % dpa_name) plt.show(block=False) return (all_cbsds, reg_requests, grant_requests, protection_zone, (len(cbsds_cat_a_indoor), len(cbsds_cat_a_outdoor), len(cbsds_cat_b)), ax)