1

I tried to read geojson object from .geojson and build search filter rather than set a static geojson_geometry variable. However, it did not work as it used to with the static variable. Win7 pro, Py 2.7.15 32bit. Below are the codes and error.

aoi = r"E:\PLANET_Py\multipart84.geojson"

with open(aoi) as f:
    data = json.load(f)   

for feature in data['features']:
    poly_type = feature['geometry']['type']
    coords = feature['geometry']['coordinates']
    geojson_geometry=json.dumps((feature['geometry']))

geometry_filter = {
  "type": "GeometryFilter",
  "field_name": "geometry",
  "config": geojson_geometry
}

t = dt.now()

y = t - timedelta(days=1)
yy = t - timedelta(days=7)  

# get images acquired within a date range from current date
date_range_filter = {
  "type": "DateRangeFilter",
  "field_name": "acquired",
  "config": {
    "gte": "%sT00:00:00.000Z" % (yy.strftime('%Y-%m-%d')),
    "lte": "%sT00:00:00.000Z" % (y.strftime('%Y-%m-%d'))
  }
}

# only get images which have <50% cloud coverage
cloud_cover_filter = {
  "type": "RangeFilter",
  "field_name": "cloud_cover",
  "config": {
    "lte": 0.5
  }
}

# combine our geo, date, cloud filters
combined_filter = {
  "type": "AndFilter",
  "config": [geometry_filter, date_range_filter, cloud_cover_filter]
}

search_result = requests.post(
    'https://api.planet.com/data/v1/quick-search',
    auth=HTTPBasicAuth('myAPIkey', ''),
    json=search_request)

print(json.dumps(search_result.json(), indent=1))

AND the error is:

>>> print(json.dumps(search_result.json(), indent=1))
{
 "field": {
  "filter.config.0.config": [
   {
    "message": "u'{\"type\": \"MultiPolygon\", \"coordinates\": [[[[176.46349778345987, -38.11473809258582], [176.46250239004382, -38.11601884487258], [176.4572092610443, -38.112539947133165], [176.45354029872024, -38.10990685811454], [176.45037426916238, -38.10994039692455], [176.4481870229376, -38.108336173676946], [176.44620341737033, -38.106249209991965], [176.4505735765721, -38.10296254706899], [176.46137920964145, -38.11140400374731], [176.46349778345987, -38.11473809258582]]], [[[176.45454303454335, -38.08318117683172], [176.45779400238163, -38.091368419149184], [176.45855077571625, -38.09462355135948], [176.45431160331216, -38.09427167466392], [176.4466842623444, -38.095628113517066], [176.4462098387771, -38.09355640167274], [176.4526823046912, -38.09169766808866], [176.45147933287407, -38.08696586891919], [176.4451995665532, -38.08810379555714], [176.44410615101478, -38.0856926990337], [176.45454303454335, -38.08318117683172]]]]}' is not of type 'object'"
   }
  ]
 }, 
 "general": []
}

I suspect that the unicode in python is problem

"u'{\"type\": \"MultiPolygon\", \"coordinates\":..........

But do not know how to avoid it. I thought using json.dumps() was enough.

0

Try changing

geojson_geometry=json.dumps((feature['geometry']))

to

geojson_geometry=feature['geometry']

In other words, don't pass in your geometry as a string but as an actual Python dictionary like the rest of the request.

  • Thanks bosth, I did get it work last night, exactly I do not need json.dumps() to make a string :) Thanks again! – user111189 Oct 29 '18 at 4:45
  • No problem! Please mark the answer as the correct solution if it is right. – bosth Oct 29 '18 at 5:11

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.