10

I have a coordinate, and I want to know what Commons categories are nearby.

For instance, for 40.7576,-73.9857 I would get Category:Times Square and probably Category:Broadway and a few others nearby.

Is there an API that gives this?

If not, is there a way to get the same via several APIs calls? 50% of false positives is OK.

Note: Once again, my question is about Wikimedia Commons categories.

Nicolas Raoul
  • 8,426
  • 5
  • 28
  • 61

4 Answers4

6

There is a more direct way to get this from Wikidata, if you want an API. That's the API used by AutoList. Returns JSON or JSONP.

Example query to retrieve all Commons categories within 0.1 kilometer from 40.7576,-73.9857:

  • http://wdq.wmflabs.org/api?q=claim[373]%20and%20around[625,40.7576,-73.9857,0.1]&props=373
  • Live

Explanation of the request:

  • 373 is the property id of the Commons category item
  • around takes as arguments: PROPERTY,LATITUDE,LONGITUDE,RADIUS
  • 625 is the property id of the coordinate location item
  • The &props=373 part can be removed if only identifiers are needed

Response:

{
    "status": {
        "error": "OK",
        "items": 8,
        "querytime": "1333ms",
        "parsed_query": "(CLAIM[373] AND AROUND[625,40.7576,-73.9857,0.1])"
    },
    "items": [11259, 626926, 659652, 827638, 1756846, 1973105, 3062716, 3363532],
    "props": {
        "373": [
            [1973105, "string", "2010 Times Square car bombing attempt"],
            [827638, "string", "Bertelsmann Building"],
            [1756846, "string", "Lyceum Theatre (Broadway)"],
            [3062716, "string", "Minskoff Theatre"],
            [659652, "string", "One Astor Plaza"],
            [3363532, "string", "Paramount Building (New York City)"],
            [11259, "string", "Times Square"],
            [626926, "string", "V-J Day in Times Square (Alfred Eisenstaedt)"]
        ]
    }
}

The server response is rather slow, and seems to increase with the number of categories returned.

See the full documentation.

Nicolas Raoul
  • 8,426
  • 5
  • 28
  • 61
Magnus Manske
  • 386
  • 2
  • 2
  • Your example return mostly articles, whereas my question asks for Wikimedia Commons categories. By the way, Commons categories are different from Wikipedia categories, they follow different hierarchy/granularity rules. – Nicolas Raoul Oct 05 '15 at 03:21
  • @NicolasRaoul Those are categories - scroll down to the "props":{"373": section, and all the text after that point is category strings. Q655136 is a good demonstration, as the returned P373 label is appended with (Manhattan) while the Wikidata label doesn't have it. – Andrew is gone Oct 05 '15 at 07:40
  • (This is a much more efficient approach than my answer, which generates a human-readable list rather than an API-readable one.) – Andrew is gone Oct 05 '15 at 07:42
  • @Andrew: What directly follows "props":{"373": is 1859121, 209017, 284013, none of which are Commons categories, they are actually just Wikipedia articles. The text "1 Hanover Square"/etc are the same Wikipedia articles. – Nicolas Raoul Oct 05 '15 at 08:11
  • @NicolasRaoul Those numbers are not Wikipedia articles, they're Wikidata entries which are associated with both articles and Commons categories. The text that follows is the name of the category associated with that entry. It happens also to be the name of the associated (English) Wikipedia article in many cases, but that's a handy coincidence... – Andrew is gone Oct 05 '15 at 08:29
  • As a demonstration, here's the area around Moscow: http://wdq.wmflabs.org/api?q=claim[373]%20and%20around[625,55.75,37.6,25]&props=373 which includes (eg) [1829534,"string","Train stations in Moscow"]. Q1829534 does not have either an English Wikipedia entry or an English label on Wikidata; it does, however, have a Commons category with that title... – Andrew is gone Oct 05 '15 at 08:32
  • 1
    @Andrew: I somehow expected a "commons" section and did not think to look for a property, now I understand, thanks! – Nicolas Raoul Oct 05 '15 at 08:49
4

Wikidata can do it!

Using autolist, run the query claim[373] and around[625,40.7576,-73.9857,50] [link].

This will give you all items with a commons category property (P373) and a location (P625) up to 50km from 40.7576,-73.9857.

At the time of writing, this is 1900 entries. Dropping it to 10km is 1146, and 1km is 141.

(This won't get subcategories of these Commons categories unless they also have properties with geocoordinates, but it's certainly a start.)

To get a list of all commons categories from this set, ask it to generate a PagePile listing, then from the next page select item properties, and then property 373 and 625 when it loads the TABernacle tool. This will give you a nice little downloadable text file with item, commons category, & coordinates. For the 1km set (141 places), this will give you the following results.

Andrew is gone
  • 803
  • 4
  • 9
2

You can use WikiData SPARQL service to run it

SELECT ?place ?location ?distance ?placeLabel ?commons WHERE {
  ?place wdt:P373 ?commons.
  SERVICE wikibase:around { 
    ?place wdt:P625 ?location .

    bd:serviceParam wikibase:center "Point(-73.9857 40.7576)"^^geo:wktLiteral .
    bd:serviceParam wikibase:radius "1" . 
    bd:serviceParam wikibase:distance ?distance .
  } 
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} ORDER BY ?distance LIMIT 100

Run it in query service: https://w.wiki/FQ4

Nicolas Raoul
  • 8,426
  • 5
  • 28
  • 61
1

There does not seem to be a Wikipedia API. It looks like geonames.org has some of this functionality though.

For example, take a look at the results of this page: http://www.geonames.org/maps/google_40.7576_-73.9857.html

I took a look at the API requests on that page and it called the following:

http://www.geonames.org/servlet/geonames?&srv=2&lat=40.7576&lng=-73.9857&north=40.762963512194894&east=-73.96087347335816&south=40.752236055031354&west=-74.01052652664185&maxRows=100&type=json&q=&P=1&A=1&V=1&T=1&L=1&R=1&S=1&H=1&U=1

That being said, it looks like this Wikipedia Search with a Bounding Box geonames.org API at http://www.geonames.org/export/wikipedia-webservice.html#wikipediaBoundingBox is what you would want though you may need to programatically filter out anything not a category.

Hope this helps.

Mark Silverberg
  • 5,184
  • 14
  • 25
  • It seems to return only articles, no categories... Also, I am looking for Wikimedia Commons categories. Commons categories are different from Wikipedia categories, they follow different hierarchy/granularity rules. – Nicolas Raoul Oct 02 '15 at 04:31
  • @NicolasRaoul - sorry, didnt understand that nuance. My bad :) – Mark Silverberg Oct 02 '15 at 20:03