2

I have two GeoJSON files. A province.geojson file and a constituencies.geojson file and I would like to know for each district polygon which province it belongs to. That is to say, for each polygon in the first file in in what polygon in the second file is it included?

Here is the file of the constituencies.geojson:

{
  "type": "FeatureCollection",
  "totalFeatures": 1515,
  "features": [
    {
      "type": "Feature",
      "id": "fd597jf1799.1",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                -7.27163887,
                33.24041367
              ],
              [
                -7.27286911,
                33.24623871
              ],
              [
                -7.26732922,
                33.25904083
              ]
            ]
          ]
        ]
      },
      "geometry_name": "geom",
      "properties": {
        "id_0": 152,
        "iso": "MAR",
        "name_0": "Morocco",
        "id_1": 1,
        "name_1": "Chaouia - Ouardigha",
        "id_2": 1,
        "name_2": "Ben Slimane",
        "id_3": 1,
        "name_3": "Ben Slimane",
        "id_4": 1,
        "name_4": "Ahlaf",
        "varname_4": null,
        "ccn_4": 0,
        "cca_4": null,
        "type_4": "Commune Rural",
        "engtype_4": "Rural Commune",
        "bbox": [
          -7.27286911,
          33.22112656,
          -6.93353081,
          33.38970184
        ],
        "swing_count": 1,
        "polling_station_count": 15,
        "turnout": 0.4780299144225693,
        "results": {
          "PI": 187,
          "PJD": 88,
          "PAM": 59,
          "USFP": 1530,
          "APFGD": 2,
          "PPS": 15,
          "RNI": 708,
          "MP": 56,
          "UC": 3,
          "FFD": 0,
          "MDS": 0,
          "AAR": 0,
          "P Neo-Democrates": 8,
          "PEDD": 0,
          "PRD": 2,
          "PRV": 0,
          "PDI": 0,
          "PGVM": 0,
          "PALAMAL": 0,
          "PCS": 0,
          "PUD": 0,
          "PDN": 1,
          "PLJS": 0,
          "PSD": 0,
          "P Annahda": 0,
          "PA": 0,
          "UMD": 0,
          "USAPMD": 10
        },
        "voter_file": {
          "nbre_sieges": 3,
          "nbre_inscrits": 5953,
          "nbre_votants": 2997,
          "nbre_nuls": 328,
          "nbre_exprimees": 2669
        },
        "swing_ratio": 0.06666666666666667
      }
    },
    {
      "type": "Feature",
      "id": "fd597jf1799.2",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                -7.00001287,
                33.63414383
              ],
              [
                -7.00081205,
                33.6269989
              ],
              [
                -6.99825382,
                33.60465622
              ]
            ]
          ]
        ]
      },
      "geometry_name": "geom",
      "properties": {
        "id_0": 152,
        "iso": "MAR",
        "name_0": "Morocco",
        "id_1": 1,
        "name_1": "Chaouia - Ouardigha",
        "id_2": 1,
        "name_2": "Ben Slimane",
        "id_3": 1,
        "name_3": "Ben Slimane",
        "id_4": 2,
        "name_4": "Ain Tizgha",
        "varname_4": null,
        "ccn_4": 0,
        "cca_4": null,
        "type_4": "Commune Rural",
        "engtype_4": "Rural Commune",
        "bbox": [
          -7.12737417,
          33.57954407,
          -6.99144888,
          33.78071213
        ],
        "swing_count": 11,
        "polling_station_count": 23,
        "turnout": 0.3912592182242994,
        "results": {
          "PI": 1837,
          "PJD": 366,
          "PAM": 143,
          "USFP": 22,
          "APFGD": 44,
          "PPS": 773,
          "RNI": 109,
          "MP": 111,
          "UC": 9,
          "FFD": 0,
          "MDS": 0,
          "AAR": 0,
          "P Neo-Democrates": 76,
          "PEDD": 27,
          "PRD": 2,
          "PRV": 0,
          "PDI": 0,
          "PGVM": 0,
          "PALAMAL": 0,
          "PCS": 0,
          "PUD": 0,
          "PDN": 1,
          "PLJS": 0,
          "PSD": 0,
          "P Annahda": 0,
          "PA": 0,
          "UMD": 2,
          "USAPMD": 514
        },
        "voter_file": {
          "nbre_sieges": 3,
          "nbre_inscrits": 8262,
          "nbre_votants": 4479,
          "nbre_nuls": 443,
          "nbre_exprimees": 4036
        },
        "swing_ratio": 0.4782608695652174
      }
    }
  ],
  "crs": {
    "type": "name",
    "properties": {
      "name": "urn:ogc:def:crs:EPSG::4326"
    }
  },
  "bbox": [
    -13.2287693,
    27.62881088,
    -0.93655348,
    35.96390533
  ]
}

And here is province.json:

{
  "type": "FeatureCollection",
  "features": [
    {
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                -7.18458319,
                33.81124878
              ],
              [
                -7.18458319,
                33.81097412
              ],
              [
                -7.18319511,
                33.81097412
              ]
            ]
          ]
        ]
      },
      "type": "Feature",
      "id": "md898kw3185.1",
      "properties": {
        "name": "Ben Slimane",
        "type": "Province",
        "segments": {
          "UND": {
            "I don't know yet": 16,
            "No": 3,
            "Yes": 5,
            "total": 24,
            "intention_rate": 20.83
          },
          "ABS": {
            "I don't know yet": 1,
            "No": 10,
            "Yes": 1,
            "total": 12,
            "intention_rate": 8.33
          },
          "PJD": {
            "I don't know yet": 1,
            "Yes": 3,
            "total": 4,
            "intention_rate": 75
          },
          "PAM": {
            "I don't know yet": 1,
            "Yes": 1,
            "total": 2,
            "intention_rate": 50
          },
          "OTH": {
            "I don't know yet": 1,
            "No": 4,
            "Yes": 4,
            "total": 9,
            "intention_rate": 44.44
          },
          "RNI": {
            "Yes": 2,
            "total": 2,
            "intention_rate": 100
          },
          "IST": {
            "I don't know yet": 1,
            "Yes": 1,
            "total": 2,
            "intention_rate": 50
          }
        },
        "sample_size": 55
      }
    },
    {
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                -6.3649292,
                33.22292328
              ],
              [
                -6.38369083,
                33.21116257
              ],
              [
                -6.39487886,
                33.19342422
              ]
            ]
          ]
        ]
      },
      "type": "Feature",
      "id": "md898kw3185.2",
      "properties": {
        "name": "Khouribga",
        "type": "Province",
        "segments": {
          "UND": {
            "I don't know yet": 46,
            "No": 12,
            "Yes": 13,
            "total": 71,
            "intention_rate": 18.31
          },
          "ABS": {
            "I don't know yet": 4,
            "No": 79,
            "Yes": 1,
            "total": 84,
            "intention_rate": 1.19
          },
          "PJD": {
            "I don't know yet": 14,
            "No": 1,
            "Yes": 4,
            "total": 19,
            "intention_rate": 21.05
          },
          "PAM": {
            "I don't know yet": 12,
            "No": 1,
            "Yes": 7,
            "total": 20,
            "intention_rate": 35
          },
          "OTH": {
            "I don't know yet": 3,
            "No": 3,
            "Yes": 2,
            "total": 8,
            "intention_rate": 25
          },
          "RNI": {
            "I don't know yet": 3,
            "Yes": 3,
            "total": 6,
            "intention_rate": 50
          },
          "IST": {
            "I don't know yet": 5,
            "Yes": 1,
            "total": 6,
            "intention_rate": 16.67
          }
        },
        "sample_size": 214
      }
    },
    {
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                -3.77662611,
                34.86683655
              ],
              [
                -3.7705431,
                34.86468506
              ],
              [
                -3.75482011,
                34.86924362
              ]
            ]
          ]
        ]
      },
      "type": "Feature",
      "id": "md898kw3185.57",
      "properties": {
        "name": "Taza",
        "type": "Province",
        "segments": {
          "UND": {
            "I don't know yet": 16,
            "No": 28,
            "Yes": 14,
            "total": 58,
            "intention_rate": 24.14
          },
          "ABS": {
            "I don't know yet": 2,
            "No": 29,
            "Yes": 1,
            "total": 32,
            "intention_rate": 3.12
          },
          "PJD": {
            "I don't know yet": 9,
            "No": 4,
            "Yes": 23,
            "total": 36,
            "intention_rate": 63.89
          },
          "PAM": {
            "I don't know yet": 4,
            "No": 1,
            "Yes": 1,
            "total": 6,
            "intention_rate": 16.67
          },
          "OTH": {
            "I don't know yet": 3,
            "No": 3,
            "Yes": 5,
            "total": 11,
            "intention_rate": 45.45
          },
          "RNI": {
            "total": 0,
            "intention_rate": 0
          },
          "IST": {
            "I don't know yet": 2,
            "No": 2,
            "Yes": 5,
            "total": 9,
            "intention_rate": 55.56
          }
        },
        "sample_size": 152
      }
    }
  ]
}

I started a script to try to make this match on the name_2 attribute in circumscription.geojson which seems to be the same as name in province.geojson but it didn't work. That's why I'm trying to do it from GeoJSON.

import json
import pandas as pd


def find_segment(province_queried):
    with open('research.geojson', encoding='utf-8-sig') as f:
        dct_research = json.load(f)
        for feature in dct_research['feature']:
            for key in feature.get("properties", {}).get("results", {}):
                province = feature.get("properties", {}).get("name")
                segments = feature.get("properties", {}).get("segments")
                if province == province_queried:
                    return segments

def main():
    with open('constituencies.json') as f:
        dct_constituencies = json.load(f)

    for feature in dct_constituencies['features']:
        for key in feature.get("properties", {}).get("results", {}):
            province = feature.get("properties", {}).get("name_2")
            constituency = feature.get("properties", {}).get("name_4", {})
            segments = find_segment(province)
            d.append({"Party Affiliation": key,
                      "Province": province,
                      "Constituency Name": constituency,
                      "segments": segments})

    column_names = ["Province", "Constituency Name", "Party Affiliation", "segments"]
    df = pd.DataFrame(d, columns=column_names)

    df.to_csv("constituencies_with_segments.csv")


if __name__ == '__main__':
    main()

However the preceding code doesn't give back the semgents in the new file. :/

Update

So I tried Devin Simmons idea to install geopandas and do a join but got some issues. Either nothing appens when installing with condas:

(ele_env) C:\Users\antoi\Documents\Programming\electoral-prediction-model-pk\data\Morocco>conda install geopandas
Collecting package metadata (current_repodata.json): done
Solving environment: done

Or some issues when collecting Fiona

(ele_env) C:\Users\antoi\Documents\Programming\electoral-prediction-model-pk\data\Morocco>python3 -m pip install geopandas
Collecting geopandas
  Using cached geopandas-0.7.0-py2.py3-none-any.whl (928 kB)
Collecting pyproj>=2.2.0
  Downloading pyproj-2.6.1.post1-cp37-cp37m-win_amd64.whl (17.2 MB)
     |████████████████████████████████| 17.2 MB 6.4 MB/s
Collecting fiona
  Using cached Fiona-1.8.13.post1.tar.gz (1.2 MB)
    ERROR: Command errored out with exit status 1:
     command: 'C:\Users\antoi\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\antoi\\AppData\\L
ocal\\Temp\\pip-install-toghrb3q\\fiona\\setup.py'"'"'; __file__='"'"'C:\\Users\\antoi\\AppData\\Local\\Temp\\pip-install-toghrb3q\\fiona\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.r
ead().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\antoi\AppData\Local\Temp\pip-install-toghrb3q\fiona\pip-egg-info'
         cwd: C:\Users\antoi\AppData\Local\Temp\pip-install-toghrb3q\fiona\
    Complete output (1 lines):
    A GDAL API version must be specified. Provide a path to gdal-config using a GDAL_CONFIG environment variable or use a GDAL_VERSION environment variable.
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
WARNING: You are using pip version 20.0.2; however, version 20.1.1 is available.
You should consider upgrading via the 'C:\Users\antoi\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\python.exe -m pip install --upgrade pip' command.

So following Stefan's comment I am now trying to install Fiona following this steps. However, I will need to find the include files and the library files for gdal. You must also specify the version of the GDAL API on the command line. If I know I'm using gdal 3.0.4 as I downloaded from here I don't know what and where are the include files and the library files for gdal.

As I'm using python 3.7.7 I tried to install Fiona-1.8.13-cp37-cp37m-win_amd64.whl but got the following error:

(ele_env) C:\Users\antoi\Documents\Programming\electoral-prediction-model-pk\data\Morocco>python3 -m pip install C:\Programs\Fiona-1.8.13-cp37-cp37m-win_amd64.whl
Processing c:\programs\fiona-1.8.13-cp37-cp37m-win_amd64.whl
Requirement already satisfied: click>=4.0 in c:\users\antoi\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages (from Fiona==1.8.13) (7.1.1)
Requirement already satisfied: attrs>=17 in c:\users\antoi\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages (from Fiona==1.8.13) (19.3.0)
Collecting cligj>=0.5
  Downloading cligj-0.5.0-py3-none-any.whl (5.7 kB)
Collecting click-plugins>=1.0
  Downloading click_plugins-1.1.1-py2.py3-none-any.whl (7.5 kB)
Requirement already satisfied: six>=1.7 in c:\users\antoi\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages (from Fiona==1.8.13) (1.14.0)
Collecting munch
  Downloading munch-2.5.0-py2.py3-none-any.whl (10 kB)
Collecting gdal~=3.0.2
  Downloading GDAL-3.0.4.tar.gz (577 kB)
     |████████████████████████████████| 577 kB 6.4 MB/s
Building wheels for collected packages: gdal
  Building wheel for gdal (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: 'C:\Users\antoi\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\antoi\\AppData\\
Local\\Temp\\pip-install-fop6zq14\\gdal\\setup.py'"'"'; __file__='"'"'C:\\Users\\antoi\\AppData\\Local\\Temp\\pip-install-fop6zq14\\gdal\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.re
ad().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d 'C:\Users\antoi\AppData\Local\Temp\pip-wheel-hlyd33hy'
       cwd: C:\Users\antoi\AppData\Local\Temp\pip-install-fop6zq14\gdal\
  Complete output (32 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build\lib.win-amd64-3.7
  copying gdal.py -> build\lib.win-amd64-3.7
  copying ogr.py -> build\lib.win-amd64-3.7
  copying osr.py -> build\lib.win-amd64-3.7
  copying gdalconst.py -> build\lib.win-amd64-3.7
  copying gdalnumeric.py -> build\lib.win-amd64-3.7
  creating build\lib.win-amd64-3.7\osgeo
  copying osgeo\gdal.py -> build\lib.win-amd64-3.7\osgeo
  copying osgeo\gdalconst.py -> build\lib.win-amd64-3.7\osgeo
  copying osgeo\gdalnumeric.py -> build\lib.win-amd64-3.7\osgeo
  copying osgeo\gdal_array.py -> build\lib.win-amd64-3.7\osgeo
  copying osgeo\gnm.py -> build\lib.win-amd64-3.7\osgeo
  copying osgeo\ogr.py -> build\lib.win-amd64-3.7\osgeo
  copying osgeo\osr.py -> build\lib.win-amd64-3.7\osgeo
  copying osgeo\__init__.py -> build\lib.win-amd64-3.7\osgeo
  Fixing build\lib.win-amd64-3.7\gdal.py build\lib.win-amd64-3.7\ogr.py build\lib.win-amd64-3.7\osr.py build\lib.win-amd64-3.7\gdalconst.py build\lib.win-amd64-3.7\gdalnumeric.py build\lib.win-amd64-3.7\osgeo\gdal
.py build\lib.win-amd64-3.7\osgeo\gdalconst.py build\lib.win-amd64-3.7\osgeo\gdalnumeric.py build\lib.win-amd64-3.7\osgeo\gdal_array.py build\lib.win-amd64-3.7\osgeo\gnm.py build\lib.win-amd64-3.7\osgeo\ogr.py bui
ld\lib.win-amd64-3.7\osgeo\osr.py build\lib.win-amd64-3.7\osgeo\__init__.py
  Skipping optional fixer: ws_comma
  Fixing build\lib.win-amd64-3.7\gdal.py build\lib.win-amd64-3.7\ogr.py build\lib.win-amd64-3.7\osr.py build\lib.win-amd64-3.7\gdalconst.py build\lib.win-amd64-3.7\gdalnumeric.py build\lib.win-amd64-3.7\osgeo\gdal
.py build\lib.win-amd64-3.7\osgeo\gdalconst.py build\lib.win-amd64-3.7\osgeo\gdalnumeric.py build\lib.win-amd64-3.7\osgeo\gdal_array.py build\lib.win-amd64-3.7\osgeo\gnm.py build\lib.win-amd64-3.7\osgeo\ogr.py bui
ld\lib.win-amd64-3.7\osgeo\osr.py build\lib.win-amd64-3.7\osgeo\__init__.py
  Skipping optional fixer: ws_comma
  running build_ext
  building 'osgeo._gdal' extension
  creating build\temp.win-amd64-3.7
  creating build\temp.win-amd64-3.7\Release
  creating build\temp.win-amd64-3.7\Release\extensions
  C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -I../../port -I../../gcore -I../../alg -I../../ogr/ -I../../ogr/
ogrsf_frmts -I../../gnm -I../../apps "-IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2032.0_x64__qbz5n2kfra8p0\include" "-IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3
.7.2032.0_x64__qbz5n2kfra8p0\include" -IC:\Users\antoi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\numpy\core\include "-IC:\Program Fil
es (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include" "-IC:\Program Files (x8
6)\Windows Kits\10\include\10.0.18362.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um" "-IC:\Program Files (x86)\Win
dows Kits\10\include\10.0.18362.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt" /EHsc /Tpextensions/gdal_wrap.cpp /Fobuild\temp.win-amd64-3.7\Release\extensions/gdal_wrap.obj
  gdal_wrap.cpp
  extensions/gdal_wrap.cpp(3173): fatal error C1083: Cannot open include file: 'cpl_port.h': No such file or directory
  error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.24.28314\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2
  ----------------------------------------
  ERROR: Failed building wheel for gdal
  Running setup.py clean for gdal
Failed to build gdal
Installing collected packages: cligj, click-plugins, munch, gdal, Fiona
    Running setup.py install for gdal ... error
    ERROR: Command errored out with exit status 1:
     command: 'C:\Users\antoi\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\antoi\\AppData
\\Local\\Temp\\pip-install-fop6zq14\\gdal\\setup.py'"'"'; __file__='"'"'C:\\Users\\antoi\\AppData\\Local\\Temp\\pip-install-fop6zq14\\gdal\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.
read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\antoi\AppData\Local\Temp\pip-record-oynpkn_v\install-record.txt' --single-version-ex
ternally-managed --user --prefix= --compile --install-headers 'C:\Users\antoi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\Include\gdal'
         cwd: C:\Users\antoi\AppData\Local\Temp\pip-install-fop6zq14\gdal\
    Complete output (32 lines):
    running install
    running build
    running build_py
    creating build
    creating build\lib.win-amd64-3.7
    copying gdal.py -> build\lib.win-amd64-3.7
    copying ogr.py -> build\lib.win-amd64-3.7
    copying osr.py -> build\lib.win-amd64-3.7
    copying gdalconst.py -> build\lib.win-amd64-3.7
    copying gdalnumeric.py -> build\lib.win-amd64-3.7
    creating build\lib.win-amd64-3.7\osgeo
    copying osgeo\gdal.py -> build\lib.win-amd64-3.7\osgeo
    copying osgeo\gdalconst.py -> build\lib.win-amd64-3.7\osgeo
    copying osgeo\gdalnumeric.py -> build\lib.win-amd64-3.7\osgeo
    copying osgeo\gdal_array.py -> build\lib.win-amd64-3.7\osgeo
    copying osgeo\gnm.py -> build\lib.win-amd64-3.7\osgeo
    copying osgeo\ogr.py -> build\lib.win-amd64-3.7\osgeo
    copying osgeo\osr.py -> build\lib.win-amd64-3.7\osgeo
    copying osgeo\__init__.py -> build\lib.win-amd64-3.7\osgeo
    Fixing build\lib.win-amd64-3.7\gdal.py build\lib.win-amd64-3.7\ogr.py build\lib.win-amd64-3.7\osr.py build\lib.win-amd64-3.7\gdalconst.py build\lib.win-amd64-3.7\gdalnumeric.py build\lib.win-amd64-3.7\osgeo\gd
al.py build\lib.win-amd64-3.7\osgeo\gdalconst.py build\lib.win-amd64-3.7\osgeo\gdalnumeric.py build\lib.win-amd64-3.7\osgeo\gdal_array.py build\lib.win-amd64-3.7\osgeo\gnm.py build\lib.win-amd64-3.7\osgeo\ogr.py b
uild\lib.win-amd64-3.7\osgeo\osr.py build\lib.win-amd64-3.7\osgeo\__init__.py
    Skipping optional fixer: ws_comma
    Fixing build\lib.win-amd64-3.7\gdal.py build\lib.win-amd64-3.7\ogr.py build\lib.win-amd64-3.7\osr.py build\lib.win-amd64-3.7\gdalconst.py build\lib.win-amd64-3.7\gdalnumeric.py build\lib.win-amd64-3.7\osgeo\gd
al.py build\lib.win-amd64-3.7\osgeo\gdalconst.py build\lib.win-amd64-3.7\osgeo\gdalnumeric.py build\lib.win-amd64-3.7\osgeo\gdal_array.py build\lib.win-amd64-3.7\osgeo\gnm.py build\lib.win-amd64-3.7\osgeo\ogr.py b
uild\lib.win-amd64-3.7\osgeo\osr.py build\lib.win-amd64-3.7\osgeo\__init__.py
    Skipping optional fixer: ws_comma
    running build_ext
    building 'osgeo._gdal' extension
    creating build\temp.win-amd64-3.7
    creating build\temp.win-amd64-3.7\Release
    creating build\temp.win-amd64-3.7\Release\extensions
    C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -I../../port -I../../gcore -I../../alg -I../../ogr/ -I../../og
r/ogrsf_frmts -I../../gnm -I../../apps "-IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2032.0_x64__qbz5n2kfra8p0\include" "-IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7
_3.7.2032.0_x64__qbz5n2kfra8p0\include" -IC:\Users\antoi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\numpy\core\include "-IC:\Program F
iles (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include" "-IC:\Program Files (
x86)\Windows Kits\10\include\10.0.18362.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um" "-IC:\Program Files (x86)\W
indows Kits\10\include\10.0.18362.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt" /EHsc /Tpextensions/gdal_wrap.cpp /Fobuild\temp.win-amd64-3.7\Release\extensions/gdal_wrap.obj
    gdal_wrap.cpp
    extensions/gdal_wrap.cpp(3173): fatal error C1083: Cannot open include file: 'cpl_port.h': No such file or directory
    error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.24.28314\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'C:\Users\antoi\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0]
 = '"'"'C:\\Users\\antoi\\AppData\\Local\\Temp\\pip-install-fop6zq14\\gdal\\setup.py'"'"'; __file__='"'"'C:\\Users\\antoi\\AppData\\Local\\Temp\\pip-install-fop6zq14\\gdal\\setup.py'"'"';f=getattr(tokenize, '"'"'o
pen'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\antoi\AppData\Local\Temp\pip-record-oynpkn_v\instal
l-record.txt' --single-version-externally-managed --user --prefix= --compile --install-headers 'C:\Users\antoi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Pyt
hon37\Include\gdal' Check the logs for full command output.
WARNING: You are using pip version 20.0.2; however, version 20.1.1 is available.
You should consider upgrading via the 'C:\Users\antoi\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\python.exe -m pip install --upgrade pip' command.
6
  • On Windows, you must install the dependencies using the prebuilt Wheels: lfd.uci.edu/~gohlke/pythonlibs. Use this to install a Wheel: pip install <path-to-fiona-wheel>.whl May 26 '20 at 10:00
  • @Stefan Thanks for this! Do you know which one I should install for Windows10? I tried three already and got Fiona-1.8.13-cp38-cp38-win_amd64.whl is not a supported wheel on this platform May 26 '20 at 10:02
  • Depends on your Python version (38 stands for Python 3.8). Find out your python version: stackoverflow.com/a/8917907/9778755 From your log, I'd say you have Python 3.7, so this should be the correct one: Fiona‑1.8.13‑cp37‑cp37m‑win_amd64.whl May 26 '20 at 10:32
  • @Stefan Yes, I'm using python 3.7.7, thanks for the reference. So I tried with the file you provided and got another error. I think that the main one is: extensions/gdal_wrap.cpp(3173): fatal error C1083: I've updated my question with the logs for this attempt. May 26 '20 at 12:55
  • This won't directly address the issue you're currently facing but it may be a good idea to start a fresh environment and just install Geopandas for the purposes of the original question. Personally I've had a lot of similar issues with the package May 26 '20 at 13:21
2

The concept you are describing is what's known as a "spatial join". If you're open to using geopandas, the sjoin function accomplishes this easily:

import geopandas as gpd

constituencies = gpd.read_file('constituencies.geojson')
provinces = gpd.read_file('provinces.geojson')

join = gpd.sjoin(constituencies, provinces, how='inner',
             op='within', lsuffix='left', rsuffix='right')

In the above code, the join variable would contain a new Geopandas Dataframe where each feature is a constituency and it will contain attributes describing the province that it lies entirely within.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.