Beispiel #1
0
 def test_binary_point_coverage(self):
     binary_coverage_point = pyqgis_analysis.generate_binary_coverage(
         self.demand_point_fl, self.facility_service_areas_fl, "Population",
         "GEOID10", "ORIG_ID")
     binary_coverage_point2 = pyqgis_analysis.generate_binary_coverage(
         self.demand_point_fl, self.facility2_service_areas_fl,
         "Population", "GEOID10", "ORIG_ID")
     self.assertEqual(self.binary_coverage_point, binary_coverage_point)
     self.assertEqual(self.binary_coverage_point2, binary_coverage_point2)
 def test_binary_point_coverage(self):
     binary_coverage_point = pyqgis_analysis.generate_binary_coverage(self.demand_point_fl,
                                                                      self.facility_service_areas_fl,
                                                                      "Population",
                                                                      "GEOID10", "ORIG_ID")
     binary_coverage_point2 = pyqgis_analysis.generate_binary_coverage(self.demand_point_fl,
                                                                       self.facility2_service_areas_fl,
                                                                       "Population",
                                                                       "GEOID10", "ORIG_ID")
     self.assertEqual(self.binary_coverage_point, binary_coverage_point)
     self.assertEqual(self.binary_coverage_point2, binary_coverage_point2)
Beispiel #3
0
def run_example():
    providers = QgsProviderRegistry.instance().providerList()
    for provider in providers:
        print(provider)
    print(QgsApplication.showSettings())
    demand_points = QgsVectorLayer("/Users/andrewlaird/Desktop/QGIS Mapping/points_2.shp", "demand_points", "ogr")
    print(demand_points.isValid())
    service_areas = QgsVectorLayer("/Users/andrewlaird/Desktop/QGIS Mapping/zones.shp", "service_areas", "ogr")
    print(service_areas.isValid())

    binary_coverage_polygon = pyqgis_analysis.generate_binary_coverage(demand_points, service_areas,
                                                                       "od_rate", "point_id", "zone_id")

    print(binary_coverage_polygon)
    # Create the mclp model
    # Maximize the total coverage (binary polygon) using at most 5 out of 8 facilities
    logger.info("Creating MCLP model...")
    mclp = covering.create_threshold_model(binary_coverage_polygon, 100.0)
    # Solve the model using GLPK
    logger.info("Solving MCLP...")
    mclp.solve(pulp.GLPK())
    # Get the unique ids of the 5 facilities chosen
    logger.info("Extracting results")
    ids = utilities.get_ids(mclp, "zones")
    # Generate a query that could be used as a definition query or selection in arcpy
    select_query = pyqgis_analysis.generate_query(ids, unique_field_name="zone_id")
    logger.info("Output query to use to generate maps is: {}".format(select_query))
    # Determine how much demand is covered by the results
    service_areas.setSubsetString(select_query)
    total_coverage = pyqgis_analysis.get_covered_demand(demand_points, "od_rate", "binary",
                                                        service_areas)
    logger.info("{0:.2f}% of demand is covered".format((100 * total_coverage) / binary_coverage_polygon["totalDemand"]))
Beispiel #4
0
    #make a grid layer
    grid_layer = make_point_grid(kerrisdale_layer, county_bounds, GRID_CELL_SIZE)
    print(len(list(grid_layer.getFeatures())))
    grid_layer = delete_points_outside_polygon(grid_layer, kerrisdale_layer)
    QgsVectorFileWriter.writeAsVectorFormat(grid_layer, GRID_LAYER_SHP, "System", grid_layer.crs(), "ESRI Shapefile")

    #make a service area layer
    service_area_layer = make_service_area_layer(grid_layer)
    print(len(list(service_area_layer.getFeatures())))
    QgsVectorFileWriter.writeAsVectorFormat(service_area_layer, SERVICE_AREA_SHP, "System", service_area_layer.crs(), "ESRI Shapefile")

    run_example()
    grid_layer = QgsVectorLayer(GRID_LAYER_SHP, "grid_layer", "ogr")
    service_area_layer = QgsVectorLayer(SERVICE_AREA_SHP, "service_area_layer", "ogr")

    binary_coverage_polygon = pyqgis_analysis.generate_binary_coverage(grid_layer, service_area_layer,
                                                                       "demand", "point_id", "area_id")
    
    print(binary_coverage_polygon)
    # Create the mclp model
    logger.info("Creating MCLP model...")
    mclp = covering.create_threshold_model(binary_coverage_polygon, 100.0)
    # Solve the model using GLPK
    logger.info("Solving MCLP...")
    mclp.solve(pulp.GLPK())
    logger.info("Extracting results")
    ids = utilities.get_ids(mclp, "areas")
    # Generate a query that could be used as a definition query or selection in arcpy
    select_query = pyqgis_analysis.generate_query(ids, unique_field_name="area_id")
    logger.info("Output query to use to generate maps is: {}".format(select_query))
    # Determine how much demand is covered by the results
    service_area_layer.setSubsetString(select_query)
    # Demand polygon shapefile has 212 polygons each where each feature has a demand (population) and unique identifier (GEOID10)
    # Read the layers
    demand_polygon_fl = qgis.core.QgsVectorLayer(
        r"../sample_data/demand_polygon.shp", "demand_polygon_fl", "ogr")
    # Read the facility service area layer
    # Facility service area polygon layer has 8 polygons, where each feature has a unique identifier (ORIG_ID)
    facility_service_areas_fl = qgis.core.QgsVectorLayer(
        r"../sample_data/facility_service_areas.shp",
        "facility_service_areas_fl", "ogr")

    # Create binary coverage (polygon) dictionary structure
    # Use population of each polygon as demand,
    # Use GEOID as the unique field
    # Ue ORIG_ID as the unique id for the facilities
    binary_coverage_polygon = pyqgis_analysis.generate_binary_coverage(
        demand_polygon_fl, facility_service_areas_fl, "Population", "GEOID10",
        "ORIG_ID")

    # Create the mclp model
    # Maximize the total coverage (binary polygon) using at most 5 out of 8 facilities
    logger.info("Creating MCLP model...")
    mclp = covering.create_mclp_model(binary_coverage_polygon, {"total": 5})
    # Solve the model using GLPK
    logger.info("Solving MCLP...")
    mclp.solve(pulp.GLPK())
    # Get the unique ids of the 5 facilities chosen
    logger.info("Extracting results")
    ids = utilities.get_ids(mclp, "facility_service_areas")
    # Generate a query that could be used as a definition query or selection in arcpy
    select_query = pyqgis_analysis.generate_query(ids,
                                                  unique_field_name="ORIG_ID")
    logger.addHandler(sh)

    # Read the demand polygon layer
    # Demand polygon shapefile has 212 polygons each where each feature has a demand (population) and unique identifier (GEOID10)
    # Read the layers
    demand_polygon_fl = qgis.core.QgsVectorLayer(r"../sample_data/demand_polygon.shp", "demand_polygon_fl", "ogr")
    # Read the facility service area layer
    # Facility service area polygon layer has 8 polygons, where each feature has a unique identifier (ORIG_ID)
    facility_service_areas_fl = qgis.core.QgsVectorLayer(r"../sample_data/facility_service_areas.shp",
                                                         "facility_service_areas_fl", "ogr")

    # Create binary coverage (polygon) dictionary structure
    # Use population of each polygon as demand,
    # Use GEOID as the unique field
    # Ue ORIG_ID as the unique id for the facilities
    binary_coverage_polygon = pyqgis_analysis.generate_binary_coverage(demand_polygon_fl, facility_service_areas_fl,
                                                                       "Population", "GEOID10", "ORIG_ID")

    # Create the mclp model
    # Maximize the total coverage (binary polygon) using at most 5 out of 8 facilities
    logger.info("Creating MCLP model...")
    mclp = covering.create_mclp_model(binary_coverage_polygon, {"total": 5})
    # Solve the model using GLPK
    logger.info("Solving MCLP...")
    mclp.solve(pulp.GLPK())
    # Get the unique ids of the 5 facilities chosen
    logger.info("Extracting results")
    ids = utilities.get_ids(mclp, "facility_service_areas")
    # Generate a query that could be used as a definition query or selection in arcpy
    select_query = pyqgis_analysis.generate_query(ids, unique_field_name="ORIG_ID")
    logger.info("Output query to use to generate maps is: {}".format(select_query))
    # Determine how much demand is covered by the results