地理位置包含关系识别:寻求替代GeoNames行政码的技术方案
Hey there! I totally get the frustration of relying on GeoNames admin codes—data gaps can tank the reliability of your location containment logic. Let’s walk through how most mainstream geocoding tools handle this problem, with practical approaches you can adopt right away:
1. Spatial Geometry-Based Containment (Most Reliable)
This is the gold standard for accurate containment checks, and it’s what nearly all enterprise-grade tools rely on. Here’s the breakdown:
- Core Idea: Use polygon boundary data for each location (countries, cities, regions) and run a spatial calculation to confirm if Location A’s geometry (point or polygon) lies entirely within Location B’s polygon.
- Tools & Quick Examples:
- For database-driven projects,
PostGIShas built-in functions likeST_Contains(B_geometry, A_geometry)—it uses spatial indexes (like R-trees) to make these queries lightning-fast even with large datasets. - For Python workflows,
GeoPandaslets you load boundary data (from Shapefiles or GeoJSON) and use the.contains()method on GeoSeries objects to validate containment in just a few lines of code.
- For database-driven projects,
- Why It Fixes Your Issue: It doesn’t depend on administrative code mappings at all. Sources like OpenStreetMap (OSM) have comprehensive, regularly updated boundary datasets for almost every region, eliminating the data gap problem you’re facing.
2. Hierarchical Administrative Level Matching
Most geocoding services (like Nominatim, Google Maps Geocoding, Mapbox) return structured results with explicit hierarchical levels (e.g., country > admin1 (state) > admin2 (county) > locality (city)). You can leverage this hierarchy to validate containment:
- Core Idea: Check if Location B is an ancestor of Location A in the administrative hierarchy. For example:
- If Location A is New York City, its hierarchy would be
United States > New York > New York City. - If Location B is the United States, you can directly confirm containment because the US is the top-level ancestor of NYC.
- If Location A is New York City, its hierarchy would be
- Pro Tip: Many services also return unique IDs for each administrative level (like OSM’s place IDs) that you can use to build a parent-child relationship graph. This makes containment checks even faster and more consistent.
3. Hybrid Approach (Speed + Accuracy)
If you want the best of both worlds, combine administrative codes with spatial checks:
- First, use hierarchical admin codes (e.g., a full path like
US-NY-NYC) to quickly filter out obvious non-matches (e.g., any location with a different country code can’t be contained within the US). - Then, run a spatial geometry check on the remaining candidates to confirm actual containment. This avoids edge cases where admin codes might be duplicated or misassigned, while keeping your queries fast.
Practical Getting-Started Tips
- Boundary Data: Grab free, up-to-date boundary data from OpenStreetMap—you can extract it using tools like
osm2pgsqlfor PostGIS orgeopandas.read_file()for GeoJSON exports. - Third-Party Services: If you don’t want to manage your own data, use APIs like Nominatim (free) or Mapbox/Google Maps (paid) that return both hierarchical metadata and geometry for any geocoded location.
- Ditch Single-Code Checks: As you saw with GeoNames, relying on a single admin code (like country code) isn’t enough—always consider the full hierarchy or geometry to avoid false positives.
内容的提问来源于stack exchange,提问作者user9372858

