{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"##!pip install utm","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# standard libraries import\nimport os\nimport glob\nimport json\nimport numpy as np\nimport pandas as pd\nfrom pathlib import Path\nfrom typing import List, Tuple, Any\n\nimport cv2\nimport matplotlib.pyplot as plt\n\nfrom shapely.geometry import Point\nfrom shapely.geometry.polygon import Polygon\nimport shapely.ops as so\n\n## reading as df-like format using geopandas library\nimport geopandas as gpd\n\n## transformation to lat, long coordinates\nimport pyproj\n##import utm","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rootDir = Path('../input/indoor-location-navigation/metadata')\nfloor_map = {\"B2\": -2, \"B1\": -1, \"F1\": 0, \"F2\": 1, \"F3\": 2, \"F4\": 3, \"F5\": 4, \"F6\": 5, \"F7\": 6, \"F8\": 7, \"F9\": 8,\n             \"1F\": 0, \"2F\": 1, \"3F\": 2, \"4F\": 3, \"5F\": 4, \"6F\": 5, \"7F\": 6, \"8F\": 7, \"9F\": 8}\nsampleCsvPath = '../input/indoor-location-navigation/sample_submission.csv'\nimageOutputDir = '.'\n\nproj = pyproj.Transformer.from_crs(3857, 4326, always_xy=True)\nmaxFloorDimension = 440.0\nmaxFigureSize = 22.0","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generate_target_buildings() -> List[str]:\n    ssubm = pd.read_csv(sampleCsvPath)\n    ssubm_df = ssubm[\"site_path_timestamp\"].apply(lambda x: pd.Series(x.split(\"_\")))\n    buildingsList = sorted(ssubm_df[0].value_counts().index.tolist()) # type: ignore\n    return buildingsList","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getFloorInfoJson(jsonFilePath):\n    # Opening JSON file \n    f = open(jsonFilePath)\n    # returns JSON object as  a dictionary \n    floorInfo = json.load(f) \n    return np.array([floorInfo['map_info']['width'],floorInfo['map_info']['height']])\n\ndef getFigureSize(floorInfo):\n    figureSize = np.round((floorInfo/maxFloorDimension)  * maxFigureSize)\n    return figureSize","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"buildingsList = generate_target_buildings()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for index,building in enumerate(buildingsList):    \n    building = buildingsList[index]\n    building_path = rootDir / building\n    folders = sorted(building_path.glob('*'))\n    print(f\"{index} bdg - {building}\")\n\n    for i,folder in enumerate(folders):\n        print(folder)\n        ## geoJson boundary points\n        geoJsonPath = sorted(folder.glob(\"*_map.json\"))[0]\n        with open(geoJsonPath, 'rb') as inputFile:\n            geofloor_data = json.load(inputFile)\n\n        ## scale of image\n        floorDataJsonPath = sorted(folder.glob(\"*_info.json\"))[0]\n        floorInfo = getFloorInfoJson(floorDataJsonPath)\n        figureSize = getFigureSize(floorInfo)\n\n        type_poly = geofloor_data['features'][0]['geometry']['type']\n        if type_poly == 'Polygon':\n            polygon = np.array(geofloor_data['features'][0]['geometry']['coordinates'][0])\n        else:\n            polygon = np.array(geofloor_data['features'][0]['geometry']['coordinates'][0][0])\n\n        floor_polygons = Polygon(polygon)\n        store_polygons_l = [Polygon(features['geometry']['coordinates'][0]) for features in geofloor_data['features'][1:]]\n        store_polygons = so.unary_union(store_polygons_l)\n\n        ## plot output\n        plt.ioff()\n        plt.figure(figsize=(figureSize[0],figureSize[1]), frameon=False)\n\n        ## plot floor polygons\n        xs, ys = floor_polygons.exterior.coords.xy \n        plt.plot(xs, ys, 'k')        \n        \n        ## plot store boundaries\n        if isinstance(store_polygons, Polygon):\n            xs, ys = store_polygons.exterior.coords.xy \n            plt.plot(xs, ys, 'k')        \n        else:\n            for geom in store_polygons.geoms:    \n                xs, ys = geom.exterior.xy   \n                plt.plot(xs, ys, 'k')        \n        \n        plt.axis('off')\n        plt.savefig(f\"{imageOutputDir}/{index}_{building}_{folder.name}.png\", dpi=300, bbox_inches='tight', pad_inches=0);\n        plt.close()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"img = np.expand_dims(cv2.imread('5a0546857ecc773753327266_B1.png', cv2.IMREAD_GRAYSCALE), axis=0)\ntemp = np.repeat(img,3,axis=0)\nprint(temp.dtype, temp.shape)","metadata":{}}]}