Search code examples
jsonpandasgeojson

how can I get the following geometery results


I am trying to make the geometry through the following code and I am also getting the correct results except the quotes I provided are the double quotes but in result it gives me the single quotes, any reason? and how can I do it correctly??

geos = []
for idx, longs in  enumerate(uniqueID):
    subV = df_Cleaned[df_Cleaned['subVoyageIDs_subV'] == longs]
    data =  [[lon,lat] for lon,lat in zip(subV.lon ,subV.lat)]
    poly = {
        "type": "LineString",
        "coordinates" : data,
    }
    geos.append(poly)

geometries = {
    "type": "FeatureCollection",
    "features": geos,
}

results:

[{'coordinates': [[-73.226768, 38.79985500000001],
   [-73.341457, 38.71438699999999],
   [-73.313495, 38.715463],
   [-73.9692, 38.51808299999999],
   [-73.964833, 38.51875000000001],
   [-73.960483, 38.519450000000006],
   [-73.956117, 38.52016699999999],
   [-73.950933, 38.520983],
   [-73.946, 38.52180000000001],
   [-73.940733, 38.52263300000001],
   [-73.936367, 38.52333300000001],
   [-73.929967, 38.52436700000001],
   [-73.92475, 38.525217],
   [-73.91895, 38.52616699999999],
   [-73.913133, 38.52706699999999],
   [-73.908783, 38.52776700000001],
   [-73.904417, 38.52844999999999],
   [-73.887883, 38.53106700000001],
   [-73.7623, 38.550983],
   [-73.75295, 38.55256700000001]],
  'type': 'LineString'}]

what i need is

[{"coordinates": [[-73.226768, 38.79985500000001],
       [-73.341457, 38.71438699999999],
       [-73.313495, 38.715463],
       [-73.9692, 38.51808299999999],
       [-73.964833, 38.51875000000001],
       [-73.960483, 38.519450000000006],
       [-73.956117, 38.52016699999999],
       [-73.950933, 38.520983],
       [-73.946, 38.52180000000001],
       [-73.940733, 38.52263300000001],
       [-73.936367, 38.52333300000001],
       [-73.929967, 38.52436700000001],
       [-73.92475, 38.525217],
       [-73.91895, 38.52616699999999],
       [-73.913133, 38.52706699999999],
       [-73.908783, 38.52776700000001],
       [-73.904417, 38.52844999999999],
       [-73.887883, 38.53106700000001],
       [-73.7623, 38.550983],
       [-73.75295, 38.55256700000001]],
      "type": "LineString"}]

how can I do that???


Solution

  • geos = []
    for idx, number in  enumerate(iddata):
        subV = rawData[rawData['id'] == number]
        geoRawData =  [[lon,lat] for lon,lat in zip(subV.lon ,subV.lat)]
        poly = {
                "type": "Feature",
                "properties":{"weight": 300 , "sample": len(subV.id), 'id ': float(subV.id.unique())},            
                "geometry" : {
                "type" : "LineString",
                "coordinates" : geoRawData,
            }
    
        }
        geos.append(poly)
    
    geometry = FeatureCollection(geos)     
    # geometry
    
    json.dump(geometry, open("rawGeometery.geojson","w"))
    

    well i used this way and used the FeatureCollection library which is imported from the Json library, and it worked and very easy way to do it..