Skip to content

Instantly share code, notes, and snippets.

@kueda
Created February 16, 2023 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kueda/722bc5f8abe64059299044f0bc00db5e to your computer and use it in GitHub Desktop.
Save kueda/722bc5f8abe64059299044f0bc00db5e to your computer and use it in GitHub Desktop.
Ruby script for importing a production iNat place into a local dev environment
slug = ARGV[0]
open( "https://api.inaturalist.org/v1/places/#{slug}" ) do |f|
json = JSON.parse( f.read )["results"][0]
if !json
puts "No results"
exit!
end
if existing = Place.where( name: json["name"] ).exists?
puts "Already exists: #{existing}"
exit!
end
unless json["geometry_geojson"]
puts "Place has no boundary"
exit!
end
place = Place.new(
name: json["name"],
display_name: json["display_name"],
place_type: json["place_type"],
admin_level: json["admin_level"],
latitude: json["location"].split( "," )[0],
longitude: json["location"].split( "," )[1]
)
geom = RGeo::GeoJSON.decode( json["geometry_geojson"] )
if geom && geom.geometry_type != RGeo::Feature::MultiPolygon
geom = RGeo::Geos.factory.multi_polygon( [geom] )
end
place.save_geom( geom )
unless place.save
puts "Failed to save place: #{place.errors.full_messages.to_sentence}"
exit!
end
puts "Saved place: #{place}"
if place.valid?
if place.place_geometry.persisted?
puts "Saved geometry: #{place.place_geometry}"
else
puts "Failed to save geometry: #{place.place_geometry.errors.full_messages.to_sentence}"
end
else
puts "Failed to save geometry: #{place.errors.full_messages.to_sentence}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment