2025-03-04     2025-03-05     2025-03-06

GU coordinates of postal code centroids are within postal code shapes

number of errors=0
number of warnings=0
country=GU
Group=chkGrpPostalCode

select 'http://qa.geonames.org/qa/kml/' || zip_code.countrycode || zip_code.zip || zip_code.placename || '.kml' as kml,zip_code.countrycode,admincode1,placename,zip, zip_code.geonameid,st_distance(wkb_geometry,st_GeomFromText('Point(' || estlonglat[0] || ' ' || estlonglat[1] || ')',4326)) as distance,st_within(st_GeomFromText('Point(' || estlonglat[0] || ' ' || estlonglat[1] || ')',4326),st_envelope(wkb_geometry)) as inbbox from zip_code,postalcode_shape where zip_code.countrycode=postalcode_shape.countrycode and zip=postalcode and not st_within(st_GeomFromText('Point(' || estlonglat[0] || ' ' || estlonglat[1] || ')',4326),wkb_geometry) and zip_code.countrycode='GU' order by distance desc

Make sure the zip code is unique!
fix with st_centroid or ST_PointOnSurface
 create table tmp as select zip_code.countrycode,admincode1,placename,zip,st_distance(wkb_geometry,st_GeomFromText('Point(' || estlonglat[0] || ' ' || estlonglat[1] || ')',4326)) as distance,st_GeomFromText('Point(' || estlonglat[0] || ' ' || estlonglat[1] || ')',4326) as geom,wkb_geometry as polygon from zip_code,postalcode_shape where zip_code.countrycode=postalcode_shape.countrycode and zip=postalcode and not within(st_GeomFromText('Point(' || estlonglat[0] || ' ' || estlonglat[1] || ')',4326),wkb_geometry) and zip_code.countrycode='CA' order by distance desc;
update tmp set geom=st_centroid(wkb_geometry);
update tmp set geom=ST_PointOnSurface(polygon) where not st_within(geom,polygon);
update zip_code set estlonglat = point(st_x(geom), st_y(geom)) from tmp where tmp.zip=zip_code.zip and tmp.countrycode=zip_code.countrycode

kml	countrycode	admincode1	placename	zip	geonameid	distance	inbbox