Home > Enterprise >  Removing specific key value pairs from geoJSON object
Removing specific key value pairs from geoJSON object

Time:12-02

The following is a subset of my geoJSON object which has a combination of Multipolygons and GeometryCollections in its features. The GeometryCollections include multiple types of geometries.

json_str = '{"type": "FeatureCollection", 
"features": [
    {"id": "0", 
    "type": "Feature", 
    "properties": {"Date": "2019/07/10", "PID": "P1"}, 
    "geometry": {"type": "GeometryCollection", 
        "geometries": [
            {"type": "MultiPolygon", 
                "coordinates": [[[[138.5765, -35.0101], [138.5764, -35.0113], [138.5776, -35.0119], [138.5757, -35.0123], [138.5744, -35.013], [138.5739, -35.0119], [138.574, -35.0115], [138.5746, -35.0101], [138.5757, -35.0097], [138.5773, -35.0088], [138.5765, -35.0101]]], [[[138.6124, -35.0016], [138.612, -35.0011], [138.613, -35.0006], [138.6134, -35.0008], [138.6143, -35.0011], [138.613, -35.0024], [138.6124, -35.0016]]]]}, 
            {"type": "MultiLineString",
                "coordinates": [[[138.5625, -34.9778], [138.5609, -34.9791]], [[138.6042, -34.9885], [138.6042, -34.9886]]]},
            {"type": "Point",
                "coordinates": [138.6656, -34.8842]}]}},
    {"id": "1",
    "type": "Feature",
    "properties": {"Date": "2019/07/10", "PID": "P2"},
    "geometry": {
        "type": "MultiPolygon", 
            "coordinates": [[[[138.5731, -34.9273], [138.5741, -34.9281], [138.5752, -34.9273], [138.5763, -34.9266], [138.5779, -34.9269], [138.5785, -34.9268], [138.5797, -34.9272], [138.5807, -34.928], [138.5814, -34.9276], [138.5809, -34.9282], [138.5807, -34.9285], [138.5794, -34.9292], [138.5785, -34.9299], [138.5766, -34.93], [138.5783, -34.9302], [138.5785, -34.9303], [138.5793, -34.9312], [138.5796, -34.9318], [138.579, -34.9332], [138.5795, -34.9336], [138.5803, -34.934], [138.5807, -34.9341], [138.5816, -34.9347], [138.5821, -34.9354], [138.5822, -34.9359], [138.5829, -34.9368], [138.5831, -34.937], [138.5836, -34.9372], [138.5843, -34.9379], [138.5843, -34.939], [138.5829, -34.9394], [138.5823, -34.9395], [138.5817, -34.939], [138.5823, -34.9377], [138.5807, -34.9385], [138.5792, -34.939], [138.5785, -34.9396], [138.5771, -34.9408], [138.5769, -34.9421], [138.5769, -34.9426], [138.5768, -34.944], [138.5785, -34.9437], [138.5788, -34.9441], [138.5789, -34.9444], [138.579, -34.9458], [138.5797, -34.9462], [138.5798, -34.9469], [138.5797, -34.948], [138.5798, -34.9487], [138.5793, -34.9498], [138.5785, -34.9507], [138.577, -34.9516], [138.5763, -34.9523], [138.5752, -34.9534], [138.5741, -34.9543], [138.5727, -34.9545], [138.5719, -34.954], [138.5713, -34.9552], [138.5697, -34.9567], [138.5692, -34.957], [138.5675, -34.9579], [138.5664, -34.9588], [138.5653, -34.9604], [138.5646, -34.9606], [138.5631, -34.9608], [138.5612, -34.9624], [138.5609, -34.9629], [138.56, -34.9642], [138.5597, -34.9652], [138.5609, -34.9655], [138.5613, -34.9657], [138.5618, -34.966], [138.5609, -34.9664], [138.56, -34.9667], [138.5587, -34.967], [138.5565, -34.9678], [138.5565, -34.9678], [138.5565, -34.9678], [138.5565, -34.9678], [138.5565, -34.9678], [138.5579, -34.9685], [138.5587, -34.9688], [138.5592, -34.9692], [138.5609, -34.9695], [138.561, -34.9695], [138.5631, -34.9695], [138.5632, -34.9695], [138.5653, -34.9693], [138.5656, -34.9694], [138.566, -34.9696], [138.5667, -34.9702], [138.5675, -34.9705], [138.5688, -34.9703], [138.5697, -34.9709], [138.5701, -34.971], [138.5711, -34.9714], [138.5697, -34.972], [138.5692, -34.9732], [138.5675, -34.975], [138.5697, -34.9738], [138.5705, -34.9743], [138.5719, -34.975], [138.5732, -34.9732], [138.5734, -34.972], [138.5723, -34.9714], [138.5741, -34.9709], [138.5744, -34.9711], [138.5763, -34.9714], [138.5763, -34.9714], [138.5763, -34.9714], [138.5782, -34.9716], [138.5785, -34.9717], [138.5798, -34.9714], [138.5807, -34.9713], [138.581, -34.9711], [138.5829, -34.9708], [138.5837, -34.9707], [138.5835, -34.9714], [138.5836, -34.9726], [138.5838, -34.9732], [138.5841, -34.974], [138.5842, -34.975], [138.5844, -34.9755], [138.5847, -34.9768], [138.5847, -34.9771], [138.5851, -34.9785], [138.5851, -34.9786], [138.5851, -34.9786], [138.5856, -34.98], [138.5858, -34.9804], [138.5857, -34.9816], [138.5859, -34.9822], [138.5864, -34.9829], [138.5865, -34.984], [138.5864, -34.9847], [138.5869, -34.9858], [138.587, -34.986], [138.5872, -34.9876], [138.5856, -34.989], [138.5873, -34.988], [138.5875, -34.9892], [138.5874, -34.9894], [138.5873, -34.9897], [138.586, -34.9912], [138.5856, -34.9925], [138.5858, -34.993], [138.5854, -34.9945], [138.5873, -34.994], [138.5877, -34.9944], [138.5879, -34.9948], [138.5876, -34.9963], [138.5875, -34.9966], [138.5873, -34.9971], [138.5866, -34.9984], [138.5864, -34.999], [138.5863, -35.0002], [138.5851, -35.0007], [138.5837, -35.0013], [138.5829, -35.0002], [138.5829, -35.0001], [138.5829, -35.0001], [138.5828, -35.0002], [138.5828, -35.0002], [138.582, -35.002], [138.5823, -35.0024], [138.5825, -35.0038], [138.5807, -35.0053], [138.5789, -35.0056], [138.5802, -35.006], [138.5797, -35.0074], [138.5785, -35.0084], [138.5767, -35.0088], [138.5764, -35.0074], [138.5782, -35.0058], [138.5763, -35.0073], [138.5761, -35.0074], [138.5762, -35.0075], [138.5759, -35.0092], [138.5741, -35.0097], [138.5739, -35.011], [138.5719, -35.0126], [138.5705, -35.0121], [138.5697, -35.0119], [138.5695, -35.0128], [138.5693, -35.0131], [138.568, -35.0146], [138.5675, -35.0163], [138.5675, -35.0164], [138.5678, -35.0179], [138.5697, -35.0178], [138.5701, -35.0178], [138.5719, -35.0177], [138.5727, -35.0175], [138.5741, -35.0176], [138.5759, -35.0167], [138.5763, -35.0165], [138.5766, -35.0164], [138.5776, -35.0153], [138.5783, -35.0146], [138.5785, -35.0145], [138.5786, -35.0145], [138.58, -35.0146], [138.5806, -35.0146], [138.5807, -35.0158], [138.5809, -35.0146], [138.5829, -35.0142], [138.5834, -35.0141], [138.5851, -35.0137], [138.5868, -35.0132], [138.5873, -35.0133], [138.5878, -35.0128], [138.5894, -35.0122], [138.5904, -35.012], [138.5916, -35.0123], [138.5925, -35.011], [138.5936, -35.0093], [138.5937, -35.0092], [138.5938, -35.0091], [138.5939, -35.0091], [138.5942, -35.0092], [138.5955, -35.0096], [138.596, -35.0097], [138.5965, -35.0092], [138.5982, -35.0082], [138.5997, -35.0079], [138.6004, -35.0083], [138.6012, -35.0074], [138.6026, -35.0063], [138.6042, -35.0061], [138.6048, -35.007], [138.6069, -35.0056], [138.6053, -35.0051], [138.6048, -35.0045], [138.6042, -35.0043], [138.6045, -35.0038], [138.6048, -35.0034], [138.605, -35.0036], [138.607, -35.0032], [138.6075, -35.0034], [138.6092, -35.0022], [138.6106, -35.0026], [138.6114, -35.0033], [138.6119, -35.0033], [138.6126, -35.0038], [138.6134, -35.004], [138.6136, -35.004], [138.6148, -35.0046], [138.6158, -35.0051], [138.6175, -35.0041], [138.618, -35.004], [138.6181, -35.0038], [138.6202, -35.002], [138.6203, -35.002], [138.6202, -35.0019], [138.6202, -35.0017], [138.6195, -35.0008], [138.618, -35.0004], [138.6158, -35.002], [138.6158, -35.002], [138.6156, -35.0021], [138.6157, -35.002], [138.6143, -35.0013], [138.6136, -35.0008], [138.6128, -35.0008], [138.6118, -35.0002], [138.6115, -35.0001], [138.6114, -35.0], [138.61, -34.9995], [138.6092, -34.9988], [138.6087, -34.9988], [138.6085, -34.9984], [138.6092, -34.998], [138.6111, -34.9966], [138.6114, -34.9958], [138.612, -34.9961], [138.6136, -34.9957], [138.6143, -34.996], [138.6158, -34.9963], [138.617, -34.9948], [138.6167, -34.994], [138.6163, -34.993], [138.6163, -34.9925], [138.6165, -34.9912], [138.6172, -34.99], [138.6175, -34.9894], [138.6179, -34.9877], [138.6178, -34.9876], [138.6176, -34.9861], [138.6176, -34.9858], [138.618, -34.9855], [138.619, -34.9849], [138.6202, -34.9842], [138.6219, -34.984], [138.6224, -34.9839], [138.6225, -34.9839], [138.6226, -34.984], [138.6241, -34.9844], [138.6246, -34.9845], [138.6252, -34.984], [138.6268, -34.9824], [138.6273, -34.9822], [138.6289, -34.9809], [138.6293, -34.9804], [138.6311, -34.9789], [138.6316, -34.9786], [138.6316, -34.9782], [138.6316, -34.9768], [138.6317, -34.9763], [138.6321, -34.975], [138.633, -34.9735], [138.633, -34.9732], [138.6333, -34.973], [138.6345, -34.9714], [138.6342, -34.9707], [138.6333, -34.9704], [138.6323, -34.9704], [138.6314, -34.9696], [138.6332, -34.9679], [138.6311, -34.9695], [138.6292, -34.9694], [138.6289, -34.9692], [138.6279, -34.9686], [138.628, -34.9678], [138.6286, -34.9663], [138.6286, -34.966], [138.6289, -34.9656], [138.6293, -34.9657], [138.6311, -34.9656], [138.6321, -34.9652], [138.6317, -34.966], [138.6326, -34.9666], [138.6333, -34.9678], [138.6334, -34.9678], [138.6334, -34.9678], [138.6349, -34.9683], [138.6355, -34.9681], [138.6359, -34.9678], [138.636, -34.9674], [138.6363, -34.966], [138.6366, -34.9651], [138.6364, -34.9642], [138.636, -34.9638], [138.6355, -34.9637], [138.6338, -34.9638], [138.6333, -34.9635], [138.6326, -34.963], [138.6326, -34.9624], [138.6323, -34.9614], [138.6321, -34.9606], [138.6317, -34.9601], [138.6311, -34.9593], [138.6307, -34.9592], [138.6289, -34.9593], [138.6286, -34.9591], [138.6282, -34.9588], [138.6288, -34.9572], [138.6287, -34.957], [138.6281, -34.9559], [138.6283, -34.9552], [138.6289, -34.9544], [138.6298, -34.9534], [138.6311, -34.9527], [138.6317, -34.953], [138.632, -34.9534], [138.6325, -34.9541], [138.6333, -34.9546], [138.6339, -34.9547], [138.6341, -34.9552], [138.6347, -34.9559], [138.6355, -34.9561], [138.6365, -34.9562], [138.6368, -34.957], [138.6372, -34.9574], [138.6377, -34.9581], [138.6387, -34.958], [138.6383, -34.9588], [138.6391, -34.9595], [138.6398, -34.9606], [138.6398, -34.9607], [138.6399, -34.9616], [138.6401, -34.9623], [138.6403, -34.9624], [138.6399, -34.9625], [138.6385, -34.9642], [138.6388, -34.9651], [138.6391, -34.966], [138.6392, -34.9666], [138.6399, -34.9667], [138.642, -34.966], [138.6421, -34.966], [138.6422, -34.966], [138.6443, -34.9647], [138.6453, -34.9642], [138.6453, -34.9634], [138.6457, -34.9624], [138.6451, -34.9618], [138.6443, -34.9611], [138.6431, -34.9616], [138.6436, -34.9606], [138.6429, -34.9599], [138.6422, -34.9588], [138.6422, -34.9588], [138.6421, -34.9587], [138.6411, -34.9579], [138.6407, -34.957], [138.6404, -34.9566], [138.6399, -34.9559], [138.6391, -34.9558], [138.6394, -34.9552], [138.6388, -34.9543], [138.6385, -34.9534], [138.6385, -34.9528], [138.6384, -34.9516], [138.6381, -34.9513], [138.6377, -34.951], [138.6366, -34.9507], [138.636, -34.9498], [138.6358, -34.9496], [138.6355, -34.9494], [138.6343, -34.949], [138.6333, -34.948], [138.6333, -34.948], [138.6333, -34.948], [138.6321, -34.9472], [138.6319, -34.9462], [138.6315, -34.9459], [138.6311, -34.9455], [138.6301, -34.9453], [138.6289, -34.9449], [138.6284, -34.9449], [138.6284, -34.9444], [138.6278, -34.9435], [138.6275, -34.9426], [138.6283, -34.9413], [138.6285, -34.9408], [138.6283, -34.9395], [138.6278, -34.939], [138.6271, -34.9388], [138.6268, -34.9386], [138.6257, -34.9381], [138.6254, -34.9372], [138.6254, -34.9365], [138.6246, -34.9355], [138.6245, -34.9354], [138.6245, -34.9354], [138.6238, -34.9342], [138.623, -34.9336], [138.6233, -34.9329], [138.6237, -34.9318], [138.6229, -34.9314], [138.6224, -34.9311], [138.6219, -34.9304], [138.6216, -34.93], [138.6221, -34.9284], [138.6221, -34.9282], [138.6215, -34.9271], [138.6211, -34.9264], [138.6214, -34.9255], [138.6217, -34.9246], [138.6224, -34.9239], [138.6234, -34.9238], [138.6246, -34.9238], [138.6258, -34.9236], [138.6268, -34.9236], [138.6285, -34.9232], [138.6289, -34.9231], [138.6304, -34.9228], [138.6311, -34.9226], [138.6315, -34.9225], [138.6333, -34.922], [138.6343, -34.921], [138.6339, -34.9205], [138.6333, -34.9203], [138.6322, -34.9202], [138.6311, -34.9202], [138.6302, -34.92], [138.6289, -34.9197], [138.6284, -34.9197], [138.6268, -34.9196], [138.6264, -34.9196], [138.6246, -34.9196], [138.6239, -34.9198], [138.6224, -34.9199], [138.6205, -34.921], [138.6202, -34.9215], [138.6191, -34.9228], [138.618, -34.9231], [138.6175, -34.9232], [138.6163, -34.9228], [138.6159, -34.9227], [138.6159, -34.921], [138.6161, -34.9208], [138.6168, -34.9192], [138.6178, -34.9175], [138.6158, -34.9183], [138.614, -34.9192], [138.6136, -34.9199], [138.6135, -34.9193], [138.6114, -34.9196], [138.6111, -34.9195], [138.6099, -34.9192], [138.6095, -34.919], [138.6092, -34.9189], [138.6085, -34.918], [138.6079, -34.9174], [138.6081, -34.9166], [138.6081, -34.9156], [138.6078, -34.915], [138.6079, -34.9138], [138.6083, -34.9128], [138.6081, -34.912], [138.6085, -34.9108], [138.6085, -34.9102], [138.6092, -34.9097], [138.6099, -34.9097], [138.6114, -34.9094], [138.6135, -34.9084], [138.6136, -34.9084], [138.6145, -34.9066], [138.6156, -34.905], [138.6157, -34.9048], [138.6154, -34.9033], [138.6152, -34.903], [138.6147, -34.9021], [138.6146, -34.9012], [138.6156, -34.8996], [138.6156, -34.8994], [138.6158, -34.8993], [138.6173, -34.8976], [138.618, -34.8971], [138.619, -34.8958], [138.6188, -34.8952], [138.6184, -34.894], [138.6182, -34.8939], [138.618, -34.8937], [138.6162, -34.8937], [138.6158, -34.8937], [138.6147, -34.894], [138.6136, -34.8945], [138.613, -34.8945], [138.6114, -34.8956], [138.6103, -34.8949], [138.6092, -34.8942], [138.6087, -34.8945], [138.607, -34.8952], [138.6063, -34.8958], [138.6056, -34.897], [138.607, -34.8964], [138.608, -34.8969], [138.608, -34.8976], [138.6076, -34.8989], [138.6073, -34.8994], [138.607, -34.8998], [138.6051, -34.901], [138.6048, -34.9011], [138.6047, -34.9012], [138.6034, -34.9024], [138.6048, -34.9027], [138.6052, -34.9027], [138.6052, -34.903], [138.6048, -34.9034], [138.6042, -34.9035], [138.6026, -34.9038], [138.6019, -34.9036], [138.6004, -34.904], [138.5994, -34.9039], [138.5982, -34.9033], [138.598, -34.9032], [138.596, -34.9031], [138.5959, -34.9031], [138.5958, -34.903], [138.5951, -34.902], [138.5947, -34.9012], [138.5943, -34.9009], [138.5938, -34.9008], [138.5931, -34.9], [138.5927, -34.8994], [138.593, -34.8984], [138.5916, -34.8982], [138.5913, -34.8979], [138.5906, -34.8976], [138.5911, -34.8963], [138.5894, -34.8972], [138.5883, -34.8968], [138.5876, -34.8958], [138.5885, -34.8948], [138.5873, -34.8955], [138.5857, -34.8953], [138.5851, -34.895], [138.5829, -34.8958], [138.5841, -34.8967], [138.5834, -34.8976], [138.5829, -34.8992], [138.5815, -34.8994], [138.5815, -34.9005], [138.5829, -34.8996], [138.5838, -34.9004], [138.5851, -34.901], [138.5852, -34.9011], [138.5853, -34.9012], [138.5855, -34.9027], [138.5857, -34.903], [138.5851, -34.9047], [138.5849, -34.9048], [138.5829, -34.9056], [138.5819, -34.9056], [138.5807, -34.9058], [138.5796, -34.9066], [138.5798, -34.9074], [138.5807, -34.9083], [138.5808, -34.9083], [138.5829, -34.9076], [138.5848, -34.9066], [138.5851, -34.9054], [138.5852, -34.9065], [138.5852, -34.9066], [138.5851, -34.9068], [138.5837, -34.9084], [138.583, -34.9101], [138.5851, -34.9094], [138.5858, -34.9096], [138.5857, -34.9102], [138.5856, -34.9116], [138.5861, -34.912], [138.5866, -34.9126], [138.5873, -34.9129], [138.5882, -34.9131], [138.5894, -34.9132], [138.59, -34.9134], [138.5902, -34.9138], [138.5901, -34.9151], [138.5908, -34.9156], [138.5905, -34.9166], [138.5899, -34.9174], [138.5894, -34.9183], [138.5884, -34.9183], [138.5873, -34.9175], [138.5872, -34.9175], [138.5872, -34.9174], [138.5859, -34.9168], [138.5851, -34.9174], [138.5845, -34.9174], [138.5829, -34.9175], [138.5828, -34.9175], [138.5827, -34.9174], [138.582, -34.9164], [138.5809, -34.9156], [138.5827, -34.9139], [138.5828, -34.9138], [138.5823, -34.9125], [138.5807, -34.9136], [138.58, -34.9126], [138.5797, -34.912], [138.5797, -34.911], [138.5785, -34.9113], [138.5773, -34.9112], [138.5763, -34.9108], [138.5751, -34.9112], [138.5741, -34.9116], [138.5719, -34.912], [138.5719, -34.912], [138.5718, -34.912], [138.5697, -34.913], [138.568, -34.9134], [138.5675, -34.9137], [138.567, -34.9138], [138.5672, -34.9141], [138.5675, -34.9142], [138.5688, -34.9146], [138.5697, -34.9151], [138.5699, -34.9155], [138.5708, -34.9156], [138.5702, -34.9171], [138.5719, -34.9157], [138.5737, -34.916], [138.5741, -34.9161], [138.575, -34.9167], [138.575, -34.9174], [138.5747, -34.9187], [138.5755, -34.9192], [138.5741, -34.9196], [138.5736, -34.9196], [138.5719, -34.9196], [138.5703, -34.921], [138.5697, -34.9213], [138.5693, -34.9214], [138.568, -34.921], [138.5693, -34.9195], [138.5675, -34.9208], [138.5656, -34.9208], [138.5653, -34.9208], [138.5648, -34.921], [138.5631, -34.9217], [138.5624, -34.9216], [138.5609, -34.9214], [138.5605, -34.9214], [138.5587, -34.9215], [138.5579, -34.9217], [138.5565, -34.9222], [138.5546, -34.9226], [138.5543, -34.9227], [138.5538, -34.9228], [138.5521, -34.9232], [138.5503, -34.9243], [138.55, -34.9245], [138.548, -34.9246], [138.5487, -34.9256], [138.55, -34.9249], [138.5505, -34.926], [138.5509, -34.9264], [138.551, -34.9274], [138.5513, -34.9282], [138.5516, -34.9287], [138.5516, -34.93], [138.5517, -34.9304], [138.5521, -34.9314], [138.5529, -34.93], [138.5528, -34.9295], [138.5529, -34.9282], [138.5537, -34.9269], [138.554, -34.9264], [138.5543, -34.9263], [138.5546, -34.9262], [138.5565, -34.9262], [138.5568, -34.9262], [138.5573, -34.9264], [138.5584, -34.9267], [138.5587, -34.9269], [138.5603, -34.9269], [138.5609, -34.9274], [138.5623, -34.9271], [138.5631, -34.9269], [138.5646, -34.927], [138.5653, -34.927], [138.5662, -34.9275], [138.5675, -34.9282], [138.5664, -34.9291], [138.5675, -34.9294], [138.5694, -34.9282], [138.5677, -34.9281], [138.5693, -34.9264], [138.5697, -34.9263], [138.5699, -34.9263], [138.5719, -34.9261], [138.5722, -34.9262], [138.5724, -34.9264], [138.5731, -34.9273]], [[138.6201, -34.9678], [138.6201, -34.9678], [138.6195, -34.9665], [138.6194, -34.966], [138.6202, -34.9653], [138.6209, -34.9654], [138.622, -34.966], [138.6208, -34.9673], [138.6224, -34.9665], [138.6245, -34.966], [138.6246, -34.966], [138.6246, -34.966], [138.6246, -34.966], [138.6246, -34.966], [138.6227, -34.9678], [138.6224, -34.9681], [138.622, -34.9681], [138.6202, -34.9678], [138.6201, -34.9678]], [[138.642, -34.9643], [138.642, -34.9642], [138.6421, -34.964], [138.6422, -34.9641], [138.6423, -34.9642], [138.6421, -34.9652], [138.642, -34.9643]], [[138.5719, -34.9643], [138.5718, -34.9643], [138.5697, -34.9645], [138.5692, -34.9646], [138.5675, -34.9648], [138.5663, -34.9652], [138.5659, -34.9642], [138.5675, -34.963], [138.5686, -34.9633], [138.5697, -34.9637], [138.5706, -34.9624], [138.5719, -34.9613], [138.5731, -34.9606], [138.5741, -34.9602], [138.5745, -34.9603], [138.5753, -34.9606], [138.5745, -34.9621], [138.5763, -34.9624], [138.5763, -34.9624], [138.5763, -34.9624], [138.5763, -34.9624], [138.5745, -34.9642], [138.5741, -34.9644], [138.5739, -34.9643], [138.5719, -34.9643]], [[138.5824, -34.9628], [138.5822, -34.9624], [138.5818, -34.9614], [138.5812, -34.9606], [138.5814, -34.96], [138.5811, -34.9588], [138.5829, -34.9583], [138.5838, -34.958], [138.5838, -34.9588], [138.5839, -34.9597], [138.5838, -34.9606], [138.5834, -34.9619], [138.5832, -34.9624], [138.5829, -34.9627], [138.5824, -34.9628]], [[138.6047, -34.9985], [138.6047, -34.9984], [138.6048, -34.9982], [138.605, -34.9982], [138.6055, -34.9984], [138.6048, -34.9985], [138.6047, -34.9985]]], [[[138.6132, -34.916], [138.6136, -34.9163], [138.6156, -34.9156], [138.6143, -34.9151], [138.6144, -34.9138], [138.6146, -34.913], [138.615, -34.912], [138.6148, -34.911], [138.6142, -34.9102], [138.6139, -34.91], [138.6136, -34.9085], [138.6124, -34.9102], [138.6114, -34.9112], [138.6108, -34.912], [138.611, -34.9123], [138.6114, -34.9125], [138.6123, -34.9131], [138.6127, -34.9138], [138.6129, -34.9144], [138.6131, -34.9156], [138.6132, -34.916]]]]}}]}'

I'm trying to iterate over all my features and remove all geometries that are not Multipolygons or Polygons from my GeometryCollections. And converting all the GeomeetryCollections into Multipolygons as well.

I have tried the following in Python, with no luck

import json

geojson_obj = json.loads(json_str)
features = geojson_obj ['features']

for feature in features:
    if feature['geometry']['type'] == 'GeometryCollection':
        for geometry in feature['geometry']['geometries']:
            if geometry['type'] != 'MultiPolygon':
                geometry = None #maybe?
                #print(geometry)

CodePudding user response:

You could create a new list of just the things you want to keep and substitute it into the original data structure. In the example case, this could be done with a list comprehension, but I've written a procedural example because of the other work you'd like to do.

import json

geojson_obj = json.loads(json_str)
features = geojson_obj ['features']

for feature in features:
    if feature['geometry']['type'] == 'GeometryCollection':
        keep_geometries = []
        for geometry in feature['geometry']['geometries']:
            if geometry['type'] == 'MultiPolygon':
                keep_geometries.append(geometry)
        feature['geometry']['geometries'] = keep_geometries

    

CodePudding user response:

It's generally easier to assemble a new list that has only the items you want, instead of individually deleting the unwanted items from the old list.

And then, if you like, you can replace the old list with the new list.

A simple example of this:

numbers = [0,1,2,3,4,5,6,7,8,9]

# replace numbers with a version of itself that only contains
# numbers that are odd (i.e. n%2 is nonzero)
numbers = [n for n in numbers if n%2]

# numbers is now [1,3,5,7,9]

Applying this concept to your code would look something like this:

for feature in mydict["features"]:
    if feature["geometry"]["type"] == "GeometryCollection":
        feature["geometry"]["geometries"] = [geom for geom in feature["geometry"]["geometries"] if geom["type"] == "Polygon" or geom["type"] == "Multipolygon"]
  • Related