Worked Example- GeoJSON API (Chapter 13)
And so I just take that service URL for Google Maps API, I found that somewhere in the documentation. We're going to read the whole document and because it's UTF-8 coming from the outside world we want it turned into Unicode inside our application, we say dot decode. Next thing we're going to do is call json dump s, which is the opposite of load s which takes this dictionary that includes the arrays and we're going to pretty print it with an indent of four.
- زمان مطالعه 7 دقیقه
- سطح متوسط
دانلود اپلیکیشن «زوم»
این درس را میتوانید به بهترین شکل و با امکانات عالی در اپلیکیشن «زوم» بخوانید
متن انگلیسی درس
Hello and welcome to Python for Everybody, we are doing some code samples here. If you want to follow along you can download the sample code, all is in a big .zip file, I’ve got it. We are going to be working with the Google Maps API. In the old days, this Maps API was free and did 2,500 requests per day. But now they’ve made it so that parts of it are behind API keys and you start having to use OAuth and stuff. But they haven’t put it all behind.this one address service that we’ve been using. That continues to work and the basically idea of an API is you go read the documentation. You find a URL and this is going to Google servers. And you pass in the address. And we have to pass in the address using what’s called URL encoding. So spaces are pluses, that’s a comma and then that’s a space. And so we have to pass this in a certain way, but if we do it right, we hit this, we’re going to get ourselves some json back and that’s really cool. And so deep inside here we get the real address, a good address, we get geometry, we have the location. We got the latitude and longitude and we can extract stuff out of here. And so we’re talking, and this one here is still rate limited to 2,500. But it’s one of the few parts of the Google Maps API that is not hidden behind an API key. In a later chapter we’ll show you how to actually talk with the API key in the geodata code, the geolode shows you how to use an API key, if you want to jump ahead and take a look at that. But for now, we’re just going to take a look at geo-json which is going to retrieve one page and tear it apart. So let’s take a look. So we’re going to grab the URL web stuff and import json, so now we’re going to use json but we’re actually going to pull the data out of the internet. And so I just take that service URL for Google Maps API, I found that somewhere in the documentation. And then I’m going to have a loop, it’s going to run forever. I’m going to ask for the add the location and then if I hit enter that’s what this is saying, get out of the loop. And then what I’m going to do is I’m going to concatenate the serviceurl, which is this, and this urllib.parse.urlencode gives a dictionary of address equals and this bit right here. Gives me the string that leads to putting this address=, but then coding these spaces the right way. So if you type a space, that bit of code turns it into the plus. So that’s important. And I’ve got the question mark sitting here at the end of that. Then what we’re going to do is we’re just going to do a urlopen, to get a handle. We’re going to read the whole document and because it’s UTF-8 coming from the outside world we want it turned into Unicode inside our application, we say dot decode. We can ask how many characters we got and we put our json load s. Now up to now we’ve been doing load s’s for internal strings, but this is now a string that came from the outside world and we’ll put a try except in and we’ll set js to be none and that will be our little trigger. Now we can look for, they give us, if we take a look at the output, they give us this okay and that status can be a problem and it can complain about things so we have to check to see if we got a good status. So, at this point, if you look at the outer bit of this, the outer bit that we get is a curly brace, so it’s a dictionary. Then there is, within that dictionary, a key results which is a list. But then the second thing in the outer dictionary is status. And so we can ask. If we got a false, if we got nothing that will quit. If we don’t have a status key in the object, or that dictionary. Or it’s not equal to okay, any number of those things. If this or this or this are all, either of those are true, we’re going to quit, failure to retrieve and print the data out. And when you’re starting to read stuff off the net you often have to put debugging in here like this. Aw, something quit, I’ve got to figure out so debugging. Next thing we’re going to do is call json dump s, which is the opposite of load s which takes this dictionary that includes the arrays and we’re going to pretty print it with an indent of four. And then we’re going to print that out. And so if you look at my code you’ll see that the first thing we do, once we’ve parsed it, is we print it back out so we can see it. And then we’re going to dig into it. So let’s go ahead and run this code. Python geojson.py. One of these days I will always type python3. Ann Arbor comma Michigan. Okay so it ran and so you see that it retrieved this URL, this URL was constructed. And retrieved 1736 characters. And it’s json pretty printed with an indent of four. And this is that json dump s, all the way down to here. So that’s just json dump s. And then it starts extracting. So it’s going to pull things out. Now when you write this code, it’s really easy to look at this and say, great, it’s easy. I tend to have to print this stuff out over and over and over as I kind of construct this expression. But if we look at it, the outer dictionary, the outer dictionary, sub results, leads to this array. And if you go look at this array carefully, you find there is only one thing in it. And so that the results is an array, sub zero gets us this dictionary. I keep wanting to say object, because that’s what it’s called, and that goes all the way down to here. So that’s what we get there. And then, within that, we now have an object and we look for geometry within that object. Where is geometry? Right there, geometry. Geometry goes from there to there, there’s geometry in there. You gotta get used to it, that’s why it’s nice to have this stuff indented. Geometry sub low, oops, come back, come back, and then we go to location within that. Location within geometry, and then within location we have lat and long. And so this is pulling out this 42 and 83. And then, so we print that out, take a look. And that prints that out, pulls that right out of the json. These are tricky to write, but after a while you win and you get it right and it’s just fine, okay? And so we do the same thing. Results sub zero formatted address, get’s us this. And so that’s how we print the location out. And so that’s a real quick look at how we would do that with the json talking to the Google Maps API. Okay, hope this helps.
مشارکت کنندگان در این صفحه
تا کنون فردی در بازسازی این صفحه مشارکت نداشته است.
🖊 شما نیز میتوانید برای مشارکت در ترجمهی این صفحه یا اصلاح متن انگلیسی، به این لینک مراجعه بفرمایید.